22// The .NET Foundation licenses this file to you under the MIT license.
33
44using System . Text . Json . Serialization ;
5+ using System . Text . Json . Serialization . Metadata ;
56using Microsoft . AspNetCore . Http . Json ;
67using Microsoft . AspNetCore . Mvc ;
78using Microsoft . Extensions . DependencyInjection ;
@@ -103,7 +104,7 @@ public void AddProblemDetails_CombinesProblemDetailsContext()
103104 }
104105
105106 [ Fact ]
106- public void AddProblemDetails_CombinesProblemDetailsContext_ForReadOnlyJsonOptions ( )
107+ public void AddProblemDetails_Throws_ForReadOnlyJsonOptions ( )
107108 {
108109 // Arrange
109110 var collection = new ServiceCollection ( ) ;
@@ -120,10 +121,7 @@ public void AddProblemDetails_CombinesProblemDetailsContext_ForReadOnlyJsonOptio
120121 var services = collection . BuildServiceProvider ( ) ;
121122 var jsonOptions = services . GetService < IOptions < JsonOptions > > ( ) ;
122123
123- Assert . NotNull ( jsonOptions . Value ) ;
124- Assert . NotNull ( jsonOptions . Value . SerializerOptions . TypeInfoResolver ) ;
125- Assert . NotNull ( jsonOptions . Value . SerializerOptions . TypeInfoResolver . GetTypeInfo ( typeof ( ProblemDetails ) , jsonOptions . Value . SerializerOptions ) ) ;
126- Assert . NotNull ( jsonOptions . Value . SerializerOptions . TypeInfoResolver . GetTypeInfo ( typeof ( TypeA ) , jsonOptions . Value . SerializerOptions ) ) ;
124+ Assert . Throws < InvalidOperationException > ( ( ) => jsonOptions . Value ) ;
127125 }
128126
129127 [ Fact ]
@@ -166,6 +164,28 @@ public void AddProblemDetails_DoesNotCombineProblemDetailsContext_WhenNullTypeIn
166164 Assert . Null ( jsonOptions . Value . SerializerOptions . TypeInfoResolver ) ;
167165 }
168166
167+ [ Fact ]
168+ public void AddProblemDetails_CombineProblemDetailsContext_WhenDefaultTypeInfoResolver ( )
169+ {
170+ // Arrange
171+ var collection = new ServiceCollection ( ) ;
172+ collection . AddOptions < JsonOptions > ( ) ;
173+ collection . ConfigureAll < JsonOptions > ( options => options . SerializerOptions . TypeInfoResolver = new DefaultJsonTypeInfoResolver ( ) ) ;
174+
175+ // Act
176+ collection . AddProblemDetails ( ) ;
177+
178+ // Assert
179+ var services = collection . BuildServiceProvider ( ) ;
180+ var jsonOptions = services . GetService < IOptions < JsonOptions > > ( ) ;
181+
182+ Assert . NotNull ( jsonOptions . Value ) ;
183+ Assert . NotNull ( jsonOptions . Value . SerializerOptions . TypeInfoResolver ) ;
184+ Assert . IsNotType < DefaultJsonTypeInfoResolver > ( jsonOptions . Value . SerializerOptions . TypeInfoResolver ) ;
185+ Assert . NotNull ( jsonOptions . Value . SerializerOptions . TypeInfoResolver . GetTypeInfo ( typeof ( ProblemDetails ) , jsonOptions . Value . SerializerOptions ) ) ;
186+ Assert . NotNull ( jsonOptions . Value . SerializerOptions . TypeInfoResolver . GetTypeInfo ( typeof ( TypeA ) , jsonOptions . Value . SerializerOptions ) ) ;
187+ }
188+
169189 [ JsonSerializable ( typeof ( TypeA ) ) ]
170190 internal partial class TestExtensionsJsonContext : JsonSerializerContext
171191 { }
0 commit comments