Is there an existing issue for this?
Describe the bug
using Microsoft.AspNetCore.Http.Json;
using System.ComponentModel.DataAnnotations;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddValidation();
builder.Services.AddProblemDetails();
builder.Services.Configure<JsonOptions>(options =>
{
options.SerializerOptions.PropertyNamingPolicy = System.Text.Json.JsonNamingPolicy.SnakeCaseUpper;
});
builder.Services.Configure<Microsoft.AspNetCore.Mvc.JsonOptions>(options =>
{
options.JsonSerializerOptions.PropertyNamingPolicy = System.Text.Json.JsonNamingPolicy.SnakeCaseUpper;
});
// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
builder.Services.AddOpenApi();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.MapOpenApi();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.MapGet("/hello", () => Results.Problem("My detail", extensions: new Dictionary<string, object?>()
{
["myValue"] = "val1",
["MyValue"] = "val2",
}));
app.Run();
The outcome of /hello is:
{"type":"https://tools.ietf.org/html/rfc9110#section-15.6.1","title":"An error occurred while processing your request.","status":500,"detail":"My detail","myValue":"val1","MyValue":"val2","TRACE_ID":"00-a74fd2d11d0157992cf80f10c719e31a-773fb3a0c7d22b4c-00"}
Notes that STJ doesn't respect the naming policy when:
- An explicit JsonPropertyName attribute is used (this is the case everything in ProblemDetails)
- For JsonExtensionData dictionary (which is part of ProblemDetails as well)
The special handling of traceId is happening here:
|
var traceIdKeyName = _serializerOptions.PropertyNamingPolicy?.ConvertName("traceId") ?? "traceId"; |
Note that traceId is also set in a different code path, but not following the naming policy:
|
problemDetails.Extensions["traceId"] = traceId; |
In case it's possible to hit both code path, we will end up with traceId set twice. Once with camelCase and once with whatever other naming policy set via JsonOptions.
Is there an existing issue for this?
Describe the bug
The outcome of
/hellois:Notes that STJ doesn't respect the naming policy when:
The special handling of traceId is happening here:
aspnetcore/src/Http/Http.Extensions/src/DefaultProblemDetailsWriter.cs
Line 59 in bad3dff
Note that traceId is also set in a different code path, but not following the naming policy:
aspnetcore/src/Mvc/Mvc.Core/src/Infrastructure/DefaultProblemDetailsFactory.cs
Line 108 in bad3dff
In case it's possible to hit both code path, we will end up with traceId set twice. Once with camelCase and once with whatever other naming policy set via JsonOptions.