-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Description
Is there an existing issue for this?
- I have searched the existing issues
Describe the bug
In .NET 7, Microsoft.AspNetCore.Mvc.ProblemDetails always properly serialized to JSON using camelCase properties which complies with RFC 7807. However, in .NET 8, the properties are serialized using whatever JsonSerializerOptions happen to be in use, which may result in incorrect casing of the property names.
One can set the overall JSON Serializer Options Naming Policy for their entire Web API to force camelCase but that may not be appropriate for ALL models being returned in one's controllers. The RFC defines the casing for this and it should be respected.
This was changed in #46492
Expected Behavior
RFC 7807 defines the JSON field names as camelCase and Microsoft.AspNetCore.Mvc.ProblemDetails is clearly intended to be used for returning errors and must therefore always use the case defined in the RFC regardless of other JsonSerializerOptions set. Alternatively, some straightforward mechanism must be made available to allow this class to be serialized in an RFC-compliant manner while the overall JsonSerializer used for Web API Controllers can be set however developers desire.
Steps To Reproduce
If one calls services.AddControllers() and sets their JSON Serializer Options PropertyNamingPolicy to null (meaning leave property names unchanged) like so:
services.AddControllers()
.AddJsonOptions(options =>
{
options.JsonSerializerOptions.PropertyNamingPolicy = null;
});And then in their controller returns Problem(...) like so:
return Problem(detail: "Error Detail", instance: "/Instance", statusCode: StatusCodes.Status500InternalServerError, title: "Error Titlle", type: "Some Type");The resulting JSON response will contain TitleCase properties like this:
{"Type":"Some Type","Title":"Error Title","Status":500,"Detail":"Error Detail","Instance":"/Instance"}When the JSON representation should be:
{"type":"Some Type","title":"Error Title","status":500,"detail":"Error Detail","instance":"/Instance"}Exceptions (if any)
No response
.NET Version
8.0.100
Anything else?
$ dotnet --info
.NET SDK:
Version: 8.0.100
Commit: 57efcf1350
Workload version: 8.0.100-manifests.6c33ef20
Runtime Environment:
OS Name: ubuntu
OS Version: 22.04
OS Platform: Linux
RID: linux-x64
Base Path: /usr/share/dotnet/sdk/8.0.100/
.NET workloads installed:
Workload version: 8.0.100-manifests.6c33ef20
There are no installed workloads to display.
Host:
Version: 8.0.0
Architecture: x64
Commit: 5535e31a71
.NET SDKs installed:
6.0.417 [/usr/share/dotnet/sdk]
7.0.405 [/usr/share/dotnet/sdk]
8.0.100 [/usr/share/dotnet/sdk]
.NET runtimes installed:
Microsoft.AspNetCore.App 6.0.25 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 7.0.15 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 8.0.0 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.NETCore.App 6.0.25 [/usr/share/dotnet/shared/Microsoft.NETCore.App]
Microsoft.NETCore.App 7.0.15 [/usr/share/dotnet/shared/Microsoft.NETCore.App]
Microsoft.NETCore.App 8.0.0 [/usr/share/dotnet/shared/Microsoft.NETCore.App]
Other architectures found:
None
Environment variables:
DOTNET_ROOT [/usr/share/dotnet/]
global.json file:
Not found
Learn more:
https://aka.ms/dotnet/info
Download .NET:
https://aka.ms/dotnet/download