Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes Problem Details casing bug (#59396) #59876

Merged
merged 2 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ public ValueTask WriteAsync(ProblemDetailsContext context)
ProblemDetailsDefaults.Apply(context.ProblemDetails, httpContext.Response.StatusCode);

var traceId = Activity.Current?.Id ?? httpContext.TraceIdentifier;
context.ProblemDetails.Extensions["traceId"] = traceId;
var tradeIdKeyName = _serializerOptions.PropertyNamingPolicy?.ConvertName("traceId") ?? "traceId";
captainsafia marked this conversation as resolved.
Show resolved Hide resolved
context.ProblemDetails.Extensions[tradeIdKeyName] = traceId;
captainsafia marked this conversation as resolved.
Show resolved Hide resolved

_options.CustomizeProblemDetails?.Invoke(context);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public async Task WriteAsync_Works_ProperCasing()
//Assert
stream.Position = 0;
var result = await JsonSerializer.DeserializeAsync<Dictionary<string, object>>(stream, JsonSerializerOptions.Default);
Assert.Equal(result.Keys, new(new() { { "type", 0 }, { "title", 1 }, { "status", 2 }, { "detail", 3 }, { "instance", 4 }, { "extensionKey", 5 }, {"traceId", expectedTraceId } }));
Assert.Equal(result.Keys, new(new() { { "type", 0 }, { "title", 1 }, { "status", 2 }, { "detail", 3 }, { "instance", 4 }, { "extensionKey", 5 }, { "traceId", expectedTraceId } }));
}

[Fact]
Expand Down Expand Up @@ -117,7 +117,7 @@ public async Task WriteAsync_Works_ProperCasing_ValidationProblemDetails()
//Assert
stream.Position = 0;
var result = await JsonSerializer.DeserializeAsync<Dictionary<string, object>>(stream, JsonSerializerOptions.Default);
Assert.Equal(result.Keys, new(new() { { "type", 0 }, { "title", 1 }, { "status", 2 }, { "detail", 3 }, { "instance", 4 }, { "errors", 5 }, {"traceId", expectedTraceId } }));
Assert.Equal(result.Keys, new(new() { { "type", 0 }, { "title", 1 }, { "status", 2 }, { "detail", 3 }, { "instance", 4 }, { "errors", 5 }, { "traceId", expectedTraceId } }));
}

[Fact]
Expand Down Expand Up @@ -689,6 +689,57 @@ private static HttpContext CreateContext(
return context;
}

[Theory]
[InlineData("SnakeCaseLower", "trace_id")]
[InlineData("CamelCase", "traceId")]
[InlineData("KebabCaseLower", "trace-id")]
[InlineData("KebabCaseUpper", "TRACE-ID")]
[InlineData("SnakeCaseUpper", "TRACE_ID")]
public async Task TestPropertyNamingPolicyChanges(string caseSelection, string extensionVariableName)
{
// Arrange
JsonNamingPolicy propertyNamingPolicy = caseSelection switch
{
"CamelCase" => JsonNamingPolicy.CamelCase,
"KebabCaseLower" => JsonNamingPolicy.KebabCaseLower,
"KebabCaseUpper" => JsonNamingPolicy.KebabCaseUpper,
"SnakeCaseLower" => JsonNamingPolicy.SnakeCaseLower,
"SnakeCaseUpper" => JsonNamingPolicy.SnakeCaseUpper,
_ => JsonNamingPolicy.KebabCaseLower
};

var options = new JsonOptions();
options.SerializerOptions.PropertyNamingPolicy = propertyNamingPolicy;

var writer = GetWriter(jsonOptions: options);
var stream = new MemoryStream();
var context = CreateContext(stream);

var expectedTraceId = Activity.Current?.Id ?? context.TraceIdentifier;
var expectedProblem = new ProblemDetails()
{
Detail = "Custom Bad Request",
Instance = "Custom Bad Request",
Status = StatusCodes.Status400BadRequest,
Type = "https://tools.ietf.org/html/rfc9110#section-15.5.1-custom",
Title = "Custom Bad Request",
};
var problemDetailsContext = new ProblemDetailsContext()
{
HttpContext = context,
ProblemDetails = expectedProblem
};

//Act
await writer.WriteAsync(problemDetailsContext);
stream.Position = 0;
using var reader = new StreamReader(stream);
var json = await reader.ReadToEndAsync();

//Assert
Assert.Contains($"\"{extensionVariableName}\":\"{expectedTraceId}\"", json);
}

private static IServiceProvider CreateServices()
{
var services = new ServiceCollection();
Expand Down
Loading