From 3d7bf4def7e2ef73670a8228fd6da9b43b40a447 Mon Sep 17 00:00:00 2001 From: Eirik Tsarpalis Date: Tue, 6 Oct 2026 14:39:37 +0300 Subject: [PATCH] Fix schema export for extension data with global Disallow Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- .../Text/Json/Schema/JsonSchemaExporter.cs | 3 +- .../tests/Common/JsonSchemaExporterTests.cs | 28 +++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Schema/JsonSchemaExporter.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Schema/JsonSchemaExporter.cs index 5a6542947366d7..4b5d173c69556d 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Schema/JsonSchemaExporter.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Schema/JsonSchemaExporter.cs @@ -219,8 +219,7 @@ private static JsonSchema MapJsonSchemaCore( List? 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(); } diff --git a/src/libraries/System.Text.Json/tests/Common/JsonSchemaExporterTests.cs b/src/libraries/System.Text.Json/tests/Common/JsonSchemaExporterTests.cs index 9e58181a2652b4..25bd296b94e8a5 100644 --- a/src/libraries/System.Text.Json/tests/Common/JsonSchemaExporterTests.cs +++ b/src/libraries/System.Text.Json/tests/Common/JsonSchemaExporterTests.cs @@ -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 typeInfo = Serializer.GetTypeInfo(options); + + PocoWithExtensionDataProperty? value = JsonSerializer.Deserialize(Json, typeInfo); + Assert.NotNull(value); + Assert.NotNull(value.ExtensionData); + Assert.Equal(42, Assert.IsType(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() {