Skip to content

JsonSchemaExporter forbids extension data with global Disallow #135258

Closed

Description

@alexaka1

Description

With JsonSerializerOptions.UnmappedMemberHandling = JsonUnmappedMemberHandling.Disallow,
JsonSchemaExporter.GetJsonSchemaAsNode emits additionalProperties: false for a
type that has a [JsonExtensionData] property. The same serializer options accept
extra JSON properties, store them in the extension-data dictionary, and write them
back during serialization.

Consequently, the exported schema rejects JSON that the serializer both reads and
writes. The Disallow setting is global; it is not applied as a type-level attribute.

Reproduction Steps

Reproduction repository: alexaka1/repro-dotnet--runtime-json-extension-data-schema.

Install the .NET 11 RC1 SDK and the .NET 9 and .NET 10 runtimes. Create a console
application and set its target frameworks:

dotnet new console --framework net11.0 --name Repro
cd Repro

Replace <TargetFramework>net11.0</TargetFramework> in Repro.csproj with:

<TargetFrameworks>net9.0;net10.0;net11.0</TargetFrameworks>

Replace Program.cs with:

using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Schema;
using System.Text.Json.Serialization;

var options = new JsonSerializerOptions(JsonSerializerOptions.Default)
{
    UnmappedMemberHandling = JsonUnmappedMemberHandling.Disallow
};

const string json = """{"Id":1,"Extra":42}""";
var value = JsonSerializer.Deserialize<Payload>(json, options)!;

Console.WriteLine($"Captured Extra: {value.ExtensionData["Extra"].GetInt32()}");
string serialized = JsonSerializer.Serialize(value, options);
Console.WriteLine($"Serialized JSON: {serialized}");
if (JsonNode.Parse(serialized)?["Extra"]?.GetValue<int>() != 42)
{
    Console.Error.WriteLine("Unexpected: Extra did not round-trip.");
    return 2;
}

var schema = options.GetJsonSchemaAsNode(typeof(Payload));
Console.WriteLine("Generated schema:");
Console.WriteLine(schema);

bool reproduced = schema["additionalProperties"]?.GetValueKind() is JsonValueKind.False;
Console.WriteLine(reproduced
    ? "BUG: The schema forbids Extra, although the serializer accepts and writes it."
    : "The schema allows extension data.");
return reproduced ? 1 : 0;

public sealed class Payload
{
    public int Id { get; set; }

    [JsonExtensionData]
    public Dictionary<string, JsonElement> ExtensionData { get; set; } = [];
}

Run each target separately:

dotnet run --framework net9.0
dotnet run --framework net10.0
dotnet run --framework net11.0

No additional packages are required. Each target uses its bundled System.Text.Json.
On each target, exit code 1 means the schema/runtime mismatch reproduced;
0 means the schema allows extension data. Exit code 2 means the round-trip
precondition failed.

Expected behavior

The exporter should omit additionalProperties for this type, as it does without
the global Disallow setting. This allows the properties captured by
[JsonExtensionData], consistent with serialization and deserialization. It must
not emit additionalProperties: false.

The global Disallow option should continue to apply to types without extension data.

Actual behavior

Target Runtime (bundled System.Text.Json) Result
.NET 9 9.0.20 Mismatch reproduced. Exit 1.
.NET 10 10.0.12 Mismatch reproduced. Exit 1.
.NET 11 RC1 11.0.0-rc.1.26425.128 Mismatch reproduced. Exit 1.

All three targets produce the same output. Deserialization succeeds and
serialization retains Extra, but the exported schema forbids that property:

Captured Extra: 42
Serialized JSON: {"Id":1,"Extra":42}
Generated schema:
{
  "type": [
    "object",
    "null"
  ],
  "properties": {
    "Id": {
      "type": "integer"
    }
  },
  "additionalProperties": false
}
BUG: The schema forbids Extra, although the serializer accepts and writes it.

Regression?

Reproduced on .NET 9.0.20, .NET 10.0.12, and .NET 11 RC1
11.0.0-rc.1.26425.128. The first affected servicing version is unknown.
.NET 8 has no built-in schema exporter; the exporter was introduced in .NET 9.

Known Workarounds

Adding [JsonUnmappedMemberHandling(JsonUnmappedMemberHandling.Skip)] to Payload
causes the exporter to omit additionalProperties. Verified on .NET 10.0.12 with the same
reproduction: extension data still round-trips and the application exits with
code 0. This type-level setting is redundant for runtime extension-data handling,
but makes the exported schema agree with it.

Alternatively, a resolver modifier can apply the same override to extension-data
contracts without annotating each type. Add
using System.Text.Json.Serialization.Metadata; and replace the options setup
in the reproduction with:

var source = JsonSerializerOptions.Default;
var options = new JsonSerializerOptions(source)
{
    UnmappedMemberHandling = JsonUnmappedMemberHandling.Disallow,
    TypeInfoResolver = source.TypeInfoResolver!.WithAddedModifier(info =>
    {
        if (info.Kind == JsonTypeInfoKind.Object &&
            info.UnmappedMemberHandling is null &&
            info.Properties.Any(property => property.IsExtensionData))
        {
            info.UnmappedMemberHandling = JsonUnmappedMemberHandling.Skip;
        }
    })
};

Verified on .NET 9.0.20, .NET 10.0.12, and .NET 11 RC1
11.0.0-rc.1.26425.128:

  • Types with [JsonExtensionData] capture and write back unknown properties,
    and their schema no longer forbids additional properties.
  • Ordinary DTOs still reject unknown properties and emit additionalProperties: false.

The modifier makes the runtime's existing extension-data override explicit in
JsonTypeInfo, so the exporter sees it. Skip does not discard properties captured
by [JsonExtensionData].

Configuration

  • Build SDK: 11.0.100-rc.1.26425.128. All three targets build with zero warnings and errors.
  • Runtimes: Microsoft.NETCore.App 9.0.20, 10.0.12, and 11.0.0-rc.1.26425.128.
  • Each target uses its bundled System.Text.Json, with no NuGet package references.
  • Latest .NET 9 servicing release and latest .NET 11 prerelease verified on
    October 6, 2026 against Microsoft's 9.0 release metadata
    and 11.0 release metadata.
  • OS: macOS 26.6.2.
  • Architecture: ARM64.
  • Other operating systems and architectures have not been tested.

Other information

The runtime computes EffectiveUnmappedMemberHandling by allowing extension data
to override the global option in
JsonTypeInfo.ConfigureProperties.
The exporter instead reads
typeInfo.UnmappedMemberHandling ?? typeInfo.Options.UnmappedMemberHandling in
JsonSchemaExporter,
which does not account for that extension-data override.

Microsoft Learn describes the exported schema as the
serialization and deserialization contract
and documents how
extension data captures and round-trips additional properties.

Activity

  1. added
    untriaged
    on Oct 6, 2026
  2. dotnet-policy-service commented on Oct 6, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @dotnet/area-system-text-json
    See info in area-owners.md if you want to be subscribed.

  3. added this to the 11.0.0 milestone on Oct 6, 2026
  4. added and removed
    untriaged
    on Oct 6, 2026
  5. added a commit that references this issue on Oct 6, 2026
    14fb767
  6. locked and limited conversation to collaborators on Nov 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions