@@ -85,7 +85,7 @@ public async Task WriteAsync_Works_ProperCasing()
85
85
//Assert
86
86
stream . Position = 0 ;
87
87
var result = await JsonSerializer . DeserializeAsync < Dictionary < string , object > > ( stream , JsonSerializerOptions . Default ) ;
88
- Assert . Equal ( result . Keys , new ( new ( ) { { "type" , 0 } , { "title" , 1 } , { "status" , 2 } , { "detail" , 3 } , { "instance" , 4 } , { "extensionKey" , 5 } , { "traceId" , expectedTraceId } } ) ) ;
88
+ Assert . Equal ( result . Keys , new ( new ( ) { { "type" , 0 } , { "title" , 1 } , { "status" , 2 } , { "detail" , 3 } , { "instance" , 4 } , { "extensionKey" , 5 } , { "traceId" , expectedTraceId } } ) ) ;
89
89
}
90
90
91
91
[ Fact ]
@@ -117,7 +117,7 @@ public async Task WriteAsync_Works_ProperCasing_ValidationProblemDetails()
117
117
//Assert
118
118
stream . Position = 0 ;
119
119
var result = await JsonSerializer . DeserializeAsync < Dictionary < string , object > > ( stream , JsonSerializerOptions . Default ) ;
120
- Assert . Equal ( result . Keys , new ( new ( ) { { "type" , 0 } , { "title" , 1 } , { "status" , 2 } , { "detail" , 3 } , { "instance" , 4 } , { "errors" , 5 } , { "traceId" , expectedTraceId } } ) ) ;
120
+ Assert . Equal ( result . Keys , new ( new ( ) { { "type" , 0 } , { "title" , 1 } , { "status" , 2 } , { "detail" , 3 } , { "instance" , 4 } , { "errors" , 5 } , { "traceId" , expectedTraceId } } ) ) ;
121
121
}
122
122
123
123
[ Fact ]
@@ -689,6 +689,57 @@ private static HttpContext CreateContext(
689
689
return context ;
690
690
}
691
691
692
+ [ Theory ]
693
+ [ InlineData ( "SnakeCaseLower" , "trace_id" ) ]
694
+ [ InlineData ( "CamelCase" , "traceId" ) ]
695
+ [ InlineData ( "KebabCaseLower" , "trace-id" ) ]
696
+ [ InlineData ( "KebabCaseUpper" , "TRACE-ID" ) ]
697
+ [ InlineData ( "SnakeCaseUpper" , "TRACE_ID" ) ]
698
+ public async Task TestPropertyNamingPolicyChanges ( string caseSelection , string extensionVariableName )
699
+ {
700
+ // Arrange
701
+ JsonNamingPolicy propertyNamingPolicy = caseSelection switch
702
+ {
703
+ "CamelCase" => JsonNamingPolicy . CamelCase ,
704
+ "KebabCaseLower" => JsonNamingPolicy . KebabCaseLower ,
705
+ "KebabCaseUpper" => JsonNamingPolicy . KebabCaseUpper ,
706
+ "SnakeCaseLower" => JsonNamingPolicy . SnakeCaseLower ,
707
+ "SnakeCaseUpper" => JsonNamingPolicy . SnakeCaseUpper ,
708
+ _ => JsonNamingPolicy . KebabCaseLower
709
+ } ;
710
+
711
+ var options = new JsonOptions ( ) ;
712
+ options . SerializerOptions . PropertyNamingPolicy = propertyNamingPolicy ;
713
+
714
+ var writer = GetWriter ( jsonOptions : options ) ;
715
+ var stream = new MemoryStream ( ) ;
716
+ var context = CreateContext ( stream ) ;
717
+
718
+ var expectedTraceId = Activity . Current ? . Id ?? context . TraceIdentifier ;
719
+ var expectedProblem = new ProblemDetails ( )
720
+ {
721
+ Detail = "Custom Bad Request" ,
722
+ Instance = "Custom Bad Request" ,
723
+ Status = StatusCodes . Status400BadRequest ,
724
+ Type = "https://tools.ietf.org/html/rfc9110#section-15.5.1-custom" ,
725
+ Title = "Custom Bad Request" ,
726
+ } ;
727
+ var problemDetailsContext = new ProblemDetailsContext ( )
728
+ {
729
+ HttpContext = context ,
730
+ ProblemDetails = expectedProblem
731
+ } ;
732
+
733
+ //Act
734
+ await writer . WriteAsync ( problemDetailsContext ) ;
735
+ stream . Position = 0 ;
736
+ using var reader = new StreamReader ( stream ) ;
737
+ var json = await reader . ReadToEndAsync ( ) ;
738
+
739
+ //Assert
740
+ Assert . Contains ( $ "\" { extensionVariableName } \" :\" { expectedTraceId } \" ", json ) ;
741
+ }
742
+
692
743
private static IServiceProvider CreateServices ( )
693
744
{
694
745
var services = new ServiceCollection ( ) ;
0 commit comments