Skip to content

Commit

Permalink
Fix JsonSchemaExporter support for global UnmappedMemberHandling sett…
Browse files Browse the repository at this point in the history
…ings. (dotnet#107545)

* Fix JsonSchemaExporter support for global UnmappedMemberHandling settings.

* Pass correct parameter in unit test.
  • Loading branch information
eiriktsarpalis authored and sirntar committed Sep 30, 2024
1 parent a4ef627 commit 744a0bd
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,8 @@ private static JsonSchema MapJsonSchemaCore(
List<string>? required = null;
JsonSchema? additionalProperties = null;

if (typeInfo.UnmappedMemberHandling is JsonUnmappedMemberHandling.Disallow)
JsonUnmappedMemberHandling effectiveUnmappedMemberHandling = typeInfo.UnmappedMemberHandling ?? typeInfo.Options.UnmappedMemberHandling;
if (effectiveUnmappedMemberHandling is JsonUnmappedMemberHandling.Disallow)
{
additionalProperties = JsonSchema.False;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,24 @@ public static IEnumerable<ITestData> GetTestDataCore()
}
""");

// Global setting for JsonUnmappedMemberHandling.Disallow
yield return new TestData<SimplePoco>(
Value: new() { String = "string", StringNullable = "string", Int = 42, Double = 3.14, Boolean = true },
ExpectedJsonSchema: """
{
"type": ["object","null"],
"properties": {
"String": { "type": "string" },
"StringNullable": { "type": ["string", "null"] },
"Int": { "type": "integer" },
"Double": { "type": "number" },
"Boolean": { "type": "boolean" }
},
"additionalProperties": false,
}
""",
SerializerOptions: new() { UnmappedMemberHandling = JsonUnmappedMemberHandling.Disallow });

yield return new TestData<PocoWithNullableAnnotationAttributes>(
Value: new() { MaybeNull = null!, AllowNull = null, NotNull = null, DisallowNull = null!, NotNullDisallowNull = "str" },
ExpectedJsonSchema: """
Expand Down Expand Up @@ -1446,7 +1464,8 @@ public record TestData<T>(
T? Value,
string ExpectedJsonSchema,
IEnumerable<T?>? AdditionalValues = null,
JsonSchemaExporterOptions? Options = null)
JsonSchemaExporterOptions? Options = null,
JsonSerializerOptions? SerializerOptions = null)
: ITestData
{
public Type Type => typeof(T);
Expand Down Expand Up @@ -1481,6 +1500,8 @@ public interface ITestData

JsonSchemaExporterOptions? Options { get; }

JsonSerializerOptions? SerializerOptions { get; }

IEnumerable<ITestData> GetTestDataForAllValues();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,24 @@ protected JsonSchemaExporterTests(JsonSerializerWrapper serializer) : base(seria
[ActiveIssue("https://github.com/dotnet/runtime/issues/103694", TestRuntimes.Mono)]
public void TestTypes_GeneratesExpectedJsonSchema(ITestData testData)
{
JsonNode schema = Serializer.DefaultOptions.GetJsonSchemaAsNode(testData.Type, testData.Options);
JsonSerializerOptions options = testData.SerializerOptions is { } opts
? new(opts) { TypeInfoResolver = Serializer.DefaultOptions.TypeInfoResolver }
: Serializer.DefaultOptions;

JsonNode schema = options.GetJsonSchemaAsNode(testData.Type, testData.Options);
AssertValidJsonSchema(testData.Type, testData.ExpectedJsonSchema, schema);
}

[Theory]
[MemberData(nameof(GetTestDataUsingAllValues))]
public void TestTypes_SerializedValueMatchesGeneratedSchema(ITestData testData)
{
JsonNode schema = Serializer.DefaultOptions.GetJsonSchemaAsNode(testData.Type, testData.Options);
JsonNode? instance = JsonSerializer.SerializeToNode(testData.Value, testData.Type, Serializer.DefaultOptions);
JsonSerializerOptions options = testData.SerializerOptions is { } opts
? new(opts) { TypeInfoResolver = Serializer.DefaultOptions.TypeInfoResolver }
: Serializer.DefaultOptions;

JsonNode schema = options.GetJsonSchemaAsNode(testData.Type, testData.Options);
JsonNode? instance = JsonSerializer.SerializeToNode(testData.Value, testData.Type, options);
AssertDocumentMatchesSchema(schema, instance);
}

Expand Down

0 comments on commit 744a0bd

Please sign in to comment.