dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.46k stars 10.03k forks source link

Default ASP JSON error response ignores JSON setting and is always in CamelCase #29589

Closed svdHero closed 3 years ago

svdHero commented 3 years ago

Describe the bug

I want my API to return PascalCase JSON. I can achieve that either through

services
    .AddControllers()
    .AddJsonOptions(options => options.JsonSerializerOptions.PropertyNamingPolicy = null);

or, using Newtonsoft, through

services
    .AddControllers()
    .AddNewtonsoftJson(options =>((options.SerializerSettings.ContractResolver as DefaultContractResolver)!).NamingStrategy = new DefaultNamingStrategy() );

No matter which way I go, ASP.NET seems to ignore this when it comes to the default error response, e.g. for model validation errors. There I always get CamelCase such as:

{
    "errors": {
        "myValue": [
            "The value 'foo' is not valid."
        ]
    },
    "type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
    "title": "One or more validation errors occurred.",
    "status": 400,
    "traceId": "00-1958ab77e30b474b9b3fb63a9644756f-3731b87868e4d345-00"
}

To Reproduce

  1. Download and run this example Web API: https://github.com/svdHero/aspnet-camelcase-error-app
  2. GET this resource: https://localhost:5001/WeatherForecast/temperature which returns PascalCase.
  3. GET this resource: https://localhost:5001/WeatherForecast/error which returns CamelCase.

Further technical details

.NET SDK (reflecting any global.json):
 Version:   5.0.101
 Commit:    d05174dc5a

Runtime Environment:
 OS Name:     Windows
 OS Version:  6.3.9600
 OS Platform: Windows
 RID:         win81-x64
 Base Path:   C:\Program Files\dotnet\sdk\5.0.101\

Host (useful for support):
  Version: 5.0.1
  Commit:  b02e13abab

.NET SDKs installed:
  2.2.203 [C:\Program Files\dotnet\sdk]
  2.2.207 [C:\Program Files\dotnet\sdk]
  2.2.402 [C:\Program Files\dotnet\sdk]
  3.1.100 [C:\Program Files\dotnet\sdk]
  5.0.101 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.All 2.1.23 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.2.4 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.2.7 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.2.8 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.App 2.1.23 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.2.4 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.2.7 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.2.8 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.1.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.1.10 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 5.0.1 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 2.1.23 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.2.4 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.2.7 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.2.8 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.1.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.1.10 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 5.0.1 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.WindowsDesktop.App 3.1.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 3.1.10 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 5.0.1 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

I am using Visual Studio 2019 on Windows.

javiercn commented 3 years ago

@svdHero thanks for contacting us.

@pranavkm do you have any thoughts on what's going on here?

mkArtakMSFT commented 3 years ago

Thanks for contacting us. ProblemDetails follow the RFC 7807 spec which calls out for these to be in lower case. That's why we ignore your choice of NamingStrategy options.

mkArtakMSFT commented 3 years ago

@Rick-Anderson can we get my last response above added to the documentation for ProblemDetails please?

svdHero commented 3 years ago

@mkArtakMSFT Thanks for clarifying.

Although, I personally think that for an API-consumer it probably would be more convenient if ALL reponses from a given API have the same case. Also, it makes an API look odd when it returns mixed case.

javiercn commented 3 years ago

@svdHero the problem is that if we did that, we would not be following the spec and standard clients could fail to interop with the API because of the casing.

Rick-Anderson commented 3 years ago

@Rick-Anderson can we get my last response above added to the documentation for ProblemDetails please?

https://github.com/dotnet/AspNetCore.Docs/issues/21318