question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

SuppressUseValidationProblemDetailsForInvalidModelStateResponses not working

See original GitHub issue

Describe 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

  1. Create a new default ASP NET Core 2.2 API project from VS2017
  2. In startup.cs add to AddMvc():
	.ConfigureApiBehaviorOptions(options =>
	{
		options.SuppressUseValidationProblemDetailsForInvalidModelStateResponses = true;
	})
  1. 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:closed
  • Created 5 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
pranavkmcommented, Jan 14, 2019

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.

0reactions
yiarnecommented, Aug 26, 2019

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();

Read more comments on GitHub >

github_iconTop Results From Across the Web

Suppress validation errors in ASP.NET Core's automatic ...
SuppressUseValidationProblemDetailsForInvalidModelStateResponses does not cover specifically the returning of the actual validation errors, ...
Read more >
Suppress validation errors in ASP.NET Core's automatic 400 ...
SuppressUseValidationProblemDetailsForInvalidModelStateResponses does not cover specifically the returning of the actual validation errors, ...
Read more >
In this article
Gets or sets a value that determines if controllers annotated with ApiControllerAttribute respond using ValidationProblemDetails in ...
Read more >
Breaking changes in .NET Core 3.0
SuppressUseValidationProblemDetailsForInvalidModelStateResponses; Microsoft. ... Automatically compacting the cache caused problems.
Read more >
Database.Migrate() doesn't work with Alpine Linux
Migrate() doesn't work with Alpine Linux. Discussion of open issues, suggestions and bugs regarding ADO.NET provider for Oracle. 5 posts • Page ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found