Skip to content

Commit 4935319

Browse files
authored
Add tests for contract customization per feedback in #70435 (#71529)
1 parent 32670bd commit 4935319

2 files changed

Lines changed: 321 additions & 34 deletions

File tree

src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Serialization/MetadataTests/DefaultJsonTypeInfoResolverTests.JsonTypeInfo.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,22 @@ public static void TypeInfoKindNoneNumberHandling()
218218
Assert.Equal(testObj.IntProp, deserialized.IntProp);
219219
}
220220

221+
[Theory]
222+
[InlineData(typeof(List<int>), JsonTypeInfoKind.Enumerable)]
223+
[InlineData(typeof(Dictionary<string, int>), JsonTypeInfoKind.Dictionary)]
224+
[InlineData(typeof(object), JsonTypeInfoKind.None)]
225+
[InlineData(typeof(string), JsonTypeInfoKind.None)]
226+
public static void AddingPropertyToNonObjectJsonTypeInfoKindThrows(Type type, JsonTypeInfoKind expectedKind)
227+
{
228+
JsonSerializerOptions options = new();
229+
DefaultJsonTypeInfoResolver resolver = new();
230+
JsonTypeInfo typeInfo = resolver.GetTypeInfo(type, options);
231+
Assert.Equal(expectedKind, typeInfo.Kind);
232+
233+
JsonPropertyInfo property = typeInfo.CreateJsonPropertyInfo(typeof(int), "test");
234+
Assert.Throws<InvalidOperationException>(() => typeInfo.Properties.Add(property));
235+
}
236+
221237
[Fact]
222238
public static void RecursiveTypeNumberHandling()
223239
{

0 commit comments

Comments
 (0)