SuppressUseValidationProblemDetailsForInvalidModelStateResponses not working
See original GitHub issueDescribe the bug
I am unsure if defaulting to Problem Details is a safe choice right now. After upgrading to 2.2, some of my tests failed because my http client was unable to handle “application/problem+json” (it works fine with “application/json” responses). It is trivial to fix my client, but this made me think if it’s wise to change this behavior now since it may cause troubles to existing clients out in the wild (Bonus Question: any considerations on this?).
So I’ve tried to disable the behavior but keep 2.2 features in this way:
services.AddMvc()
.SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
.ConfigureApiBehaviorOptions(options =>
{
options.SuppressUseValidationProblemDetailsForInvalidModelStateResponses = true;
})
.AddJsonOptions(options =>
{
options.SerializerSettings.ContractResolver = new DefaultContractResolver();
options.SerializerSettings.Converters.Add(new StringEnumConverter());
});
This is also explained in the official documentation here, but nothing changes. Setting the flag does nothing regarding handling of 4xx responses.
To Reproduce
- Create a new default ASP NET Core 2.2 API project from VS2017
- In startup.cs add to AddMvc():
.ConfigureApiBehaviorOptions(options =>
{
options.SuppressUseValidationProblemDetailsForInvalidModelStateResponses = true;
})
- Make a POST request to /api/values endpoint with some invalid json
Expected behavior
Should return a the old json validation error format with “application/json” as content type.
Additional context
.NET Core SDK (reflecting any global.json):
Version: 2.2.100
Commit: b9f2fa0ca8
Runtime Environment:
OS Name: Windows
OS Version: 10.0.17134
OS Platform: Windows
RID: win10-x64
Base Path: C:\Program Files\dotnet\sdk\2.2.100\
Host (useful for support):
Version: 2.2.0
Commit: 1249f08fed
.NET Core SDKs installed:
1.1.10 [C:\Program Files\dotnet\sdk]
1.1.11 [C:\Program Files\dotnet\sdk]
2.1.202 [C:\Program Files\dotnet\sdk]
2.1.302 [C:\Program Files\dotnet\sdk]
2.1.400 [C:\Program Files\dotnet\sdk]
2.1.401 [C:\Program Files\dotnet\sdk]
2.1.402 [C:\Program Files\dotnet\sdk]
2.1.403 [C:\Program Files\dotnet\sdk]
2.1.500 [C:\Program Files\dotnet\sdk]
2.1.502 [C:\Program Files\dotnet\sdk]
2.2.100 [C:\Program Files\dotnet\sdk]
.NET Core runtimes installed:
Microsoft.AspNetCore.All 2.1.2 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.1.4 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.1.5 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.1.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.2.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.App 2.1.2 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.1.4 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.1.5 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.1.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.2.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 1.0.12 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 1.0.13 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 1.1.9 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 1.1.10 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.0.9 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.1.2 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.1.3-servicing-26724-03 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.1.4 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.1.5 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.1.6 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.2.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Issue Analytics
- State:
- Created 5 years ago
- Comments:7 (3 by maintainers)
Unfortunately this does not meet the patch requirements for 2.2. Here are the suggested workaounds
ApiBehaviorOptions.SuppressMapClientErrors = true
or changeApiBehaviorOptions.InvalidModelStateFactory
as follows:The flag no longer exists in 3.0, so no additional work is required here.
A bit late but for anyone else that might read this post like I did, that’s the ActionContext parameter, you could also make the response body even less chatty if that’s preferred.
The LINQ stuff could be put in an extension
public static List<string> GetErrorMessages(this ModelStateDictionary modelStateDictionary)
to just do context.ModelState.GetErrorMessages();