Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,7 @@ private static JsonSchema MapJsonSchemaCore(
List<string>? required = null;
JsonSchema? additionalProperties = null;

JsonUnmappedMemberHandling effectiveUnmappedMemberHandling = typeInfo.UnmappedMemberHandling ?? typeInfo.Options.UnmappedMemberHandling;
if (effectiveUnmappedMemberHandling is JsonUnmappedMemberHandling.Disallow)
if (typeInfo.EffectiveUnmappedMemberHandling is JsonUnmappedMemberHandling.Disallow)
{
additionalProperties = JsonSchema.CreateFalseSchema();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,34 @@ public void TypeWithDisallowUnmappedMembers_AdditionalPropertiesFailValidation()
AssertDoesNotMatchSchema(schema, jsonWithUnmappedProperties);
}

[Theory]
[InlineData(JsonUnmappedMemberHandling.Skip)]
[InlineData(JsonUnmappedMemberHandling.Disallow)]
public void TypeWithExtensionData_GlobalUnmappedMemberHandling_AllowsAdditionalProperties(JsonUnmappedMemberHandling unmappedMemberHandling)
{
const string Json = """{"Name":"name","x":42}""";
JsonSerializerOptions options = new(Serializer.DefaultOptions) { UnmappedMemberHandling = unmappedMemberHandling };
JsonTypeInfo<PocoWithExtensionDataProperty> typeInfo = Serializer.GetTypeInfo<PocoWithExtensionDataProperty>(options);

PocoWithExtensionDataProperty? value = JsonSerializer.Deserialize(Json, typeInfo);
Assert.NotNull(value);
Assert.NotNull(value.ExtensionData);
Assert.Equal(42, Assert.IsType<JsonElement>(value.ExtensionData["x"]).GetInt32());

string serialized = JsonSerializer.Serialize(value, typeInfo);
JsonTestHelper.AssertJsonEqual(Json, serialized);

foreach (JsonNode schema in new[]
{
options.GetJsonSchemaAsNode(typeof(PocoWithExtensionDataProperty)),
typeInfo.GetJsonSchemaAsNode()
})
{
Assert.False(schema.AsObject().ContainsKey("additionalProperties"));
AssertDocumentMatchesSchema(schema, JsonNode.Parse(serialized));
}
}

[Fact]
public void GetJsonSchemaAsNode_NullInputs_ThrowsArgumentNullException()
{
Expand Down
Loading