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.
.ConfigureApiBehaviorOptions(options =>
{
options.SuppressUseValidationProblemDetailsForInvalidModelStateResponses = true;
})
Should return a the old json validation error format with "application/json" as content type.
.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]
Thanks for contacting us, @bragma.
@pranavkm, can you please look into this when you'll get time? Thanks!
@bragma, looks like a bug. The actual switch is incorrectly mapped to SuppressMapClientErrors. Changing to it should get you the expected behavior, but would also turn off the filter that converts error status codes to problem details.
@mkArtakMSFT the flag is no longer present in 3.0, so we need to determine if this meets the 2.2 patch bar.
I've just encountered this too: using 2.2.102.
Any further update?
Unfortunately this does not meet the patch requirements for 2.2. Here are the suggested workaounds
ApiBehaviorOptions.SuppressMapClientErrors = true or change ApiBehaviorOptions.InvalidModelStateFactory as follows:```C#
services.AddMvc()
.ConfigureApiBehaviorOptions(options =>
{
options.InvalidModelStateResponseFactory =
{
var result = new BadRequestObjectResult(context.ModelState);
result.ContentTypes.Add("application/json");
result.ContentTypes.Add("application/xml");
return result;
}
});
```
The flag no longer exists in 3.0, so no additional work is required here.
@pranavkm And about json javascript camelcase json keys?
Unfortunately this does not meet the patch requirements for 2.2. Here are the suggested workaounds
* Continue using the 2.1 compatibility version * If upgrading to 2.2 compatibility version, either set `ApiBehaviorOptions.SuppressMapClientErrors = true` or change `ApiBehaviorOptions.InvalidModelStateFactory` as follows:services.AddMvc() .ConfigureApiBehaviorOptions(options => { options.InvalidModelStateResponseFactory = { var result = new BadRequestObjectResult(context.ModelState); result.ContentTypes.Add("application/json"); result.ContentTypes.Add("application/xml"); return result; } });The flag no longer exists in 3.0, so no additional work is required here.
may I know where does that context came from ? I trying to fix this problem also in my .Net Core 2.2 webapi
may I know where does that context came from ? I trying to fix this problem also in my .Net Core 2.2 webapi
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.
services.AddMvc()
.SetCompatibilityVersion(CompatibilityVersion.Version_2_2)
.ConfigureApiBehaviorOptions(options =>
{
options.InvalidModelStateResponseFactory = context =>
{
var result = new BadRequestObjectResult(context.ModelState
.SelectMany(m => m.Value.Errors)
.Select(m => m.ErrorMessage)
.ToList());
result.ContentTypes.Add(MediaTypeNames.Application.Json);
return result;
};
});
The LINQ stuff could be put in an extension public static List<string> GetErrorMessages(this ModelStateDictionary modelStateDictionary) to just do context.ModelState.GetErrorMessages();
Most helpful comment
Unfortunately this does not meet the patch requirements for 2.2. Here are the suggested workaounds
ApiBehaviorOptions.SuppressMapClientErrors = trueor changeApiBehaviorOptions.InvalidModelStateFactoryas follows:```C#
services.AddMvc()
.ConfigureApiBehaviorOptions(options =>
{
options.InvalidModelStateResponseFactory =
{
var result = new BadRequestObjectResult(context.ModelState);
});
```
The flag no longer exists in 3.0, so no additional work is required here.