Skip to content

Inconsistent handling of traceId serializing when using non camelCase json serialization #66603

@Youssef1313

Description

@Youssef1313

Is there an existing issue for this?

  • I have searched the existing issues

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-minimalIncludes minimal APIs, endpoint filters, parameter binding, request delegate generator etcbugThis issue describes a behavior which is not expected - a bug.feature-problem-details

    Type

    No fields configured for Bug.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions