Skip to content

Commit a709d73

Browse files
Fix JsonSchemaExporter support for global UnmappedMemberHandling settings. (#107545)
* Fix JsonSchemaExporter support for global UnmappedMemberHandling settings. * Pass correct parameter in unit test.
1 parent 4878fb3 commit a709d73

File tree

3 files changed

+35
-5
lines changed

3 files changed

+35
-5
lines changed

src/libraries/System.Text.Json/src/System/Text/Json/Schema/JsonSchemaExporter.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,8 @@ private static JsonSchema MapJsonSchemaCore(
204204
List<string>? required = null;
205205
JsonSchema? additionalProperties = null;
206206

207-
if (typeInfo.UnmappedMemberHandling is JsonUnmappedMemberHandling.Disallow)
207+
JsonUnmappedMemberHandling effectiveUnmappedMemberHandling = typeInfo.UnmappedMemberHandling ?? typeInfo.Options.UnmappedMemberHandling;
208+
if (effectiveUnmappedMemberHandling is JsonUnmappedMemberHandling.Disallow)
208209
{
209210
additionalProperties = JsonSchema.False;
210211
}

src/libraries/System.Text.Json/tests/Common/JsonSchemaExporterTests.TestTypes.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -677,6 +677,24 @@ public static IEnumerable<ITestData> GetTestDataCore()
677677
}
678678
""");
679679

680+
// Global setting for JsonUnmappedMemberHandling.Disallow
681+
yield return new TestData<SimplePoco>(
682+
Value: new() { String = "string", StringNullable = "string", Int = 42, Double = 3.14, Boolean = true },
683+
ExpectedJsonSchema: """
684+
{
685+
"type": ["object","null"],
686+
"properties": {
687+
"String": { "type": "string" },
688+
"StringNullable": { "type": ["string", "null"] },
689+
"Int": { "type": "integer" },
690+
"Double": { "type": "number" },
691+
"Boolean": { "type": "boolean" }
692+
},
693+
"additionalProperties": false,
694+
}
695+
""",
696+
SerializerOptions: new() { UnmappedMemberHandling = JsonUnmappedMemberHandling.Disallow });
697+
680698
yield return new TestData<PocoWithNullableAnnotationAttributes>(
681699
Value: new() { MaybeNull = null!, AllowNull = null, NotNull = null, DisallowNull = null!, NotNullDisallowNull = "str" },
682700
ExpectedJsonSchema: """
@@ -1446,7 +1464,8 @@ public record TestData<T>(
14461464
T? Value,
14471465
string ExpectedJsonSchema,
14481466
IEnumerable<T?>? AdditionalValues = null,
1449-
JsonSchemaExporterOptions? Options = null)
1467+
JsonSchemaExporterOptions? Options = null,
1468+
JsonSerializerOptions? SerializerOptions = null)
14501469
: ITestData
14511470
{
14521471
public Type Type => typeof(T);
@@ -1481,6 +1500,8 @@ public interface ITestData
14811500

14821501
JsonSchemaExporterOptions? Options { get; }
14831502

1503+
JsonSerializerOptions? SerializerOptions { get; }
1504+
14841505
IEnumerable<ITestData> GetTestDataForAllValues();
14851506
}
14861507

src/libraries/System.Text.Json/tests/Common/JsonSchemaExporterTests.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,24 @@ protected JsonSchemaExporterTests(JsonSerializerWrapper serializer) : base(seria
2929
[ActiveIssue("https://github.com/dotnet/runtime/issues/103694", TestRuntimes.Mono)]
3030
public void TestTypes_GeneratesExpectedJsonSchema(ITestData testData)
3131
{
32-
JsonNode schema = Serializer.DefaultOptions.GetJsonSchemaAsNode(testData.Type, testData.Options);
32+
JsonSerializerOptions options = testData.SerializerOptions is { } opts
33+
? new(opts) { TypeInfoResolver = Serializer.DefaultOptions.TypeInfoResolver }
34+
: Serializer.DefaultOptions;
35+
36+
JsonNode schema = options.GetJsonSchemaAsNode(testData.Type, testData.Options);
3337
AssertValidJsonSchema(testData.Type, testData.ExpectedJsonSchema, schema);
3438
}
3539

3640
[Theory]
3741
[MemberData(nameof(GetTestDataUsingAllValues))]
3842
public void TestTypes_SerializedValueMatchesGeneratedSchema(ITestData testData)
3943
{
40-
JsonNode schema = Serializer.DefaultOptions.GetJsonSchemaAsNode(testData.Type, testData.Options);
41-
JsonNode? instance = JsonSerializer.SerializeToNode(testData.Value, testData.Type, Serializer.DefaultOptions);
44+
JsonSerializerOptions options = testData.SerializerOptions is { } opts
45+
? new(opts) { TypeInfoResolver = Serializer.DefaultOptions.TypeInfoResolver }
46+
: Serializer.DefaultOptions;
47+
48+
JsonNode schema = options.GetJsonSchemaAsNode(testData.Type, testData.Options);
49+
JsonNode? instance = JsonSerializer.SerializeToNode(testData.Value, testData.Type, options);
4250
AssertDocumentMatchesSchema(schema, instance);
4351
}
4452

0 commit comments

Comments
 (0)