-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Document breaking change: System.Text.Json validates metadata property conflicts #49880
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Copilot
wants to merge
6
commits into
main
Choose a base branch
from
copilot/fix-json-metadata-conflict
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+71
−0
Open
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
275dd73
Initial plan
Copilot c0691a6
Add breaking change documentation for System.Text.Json metadata prope…
Copilot a54af5d
Add references to original runtime issue and PR
Copilot 853dad5
human edits
gewarren 22f206a
Update docs/core/compatibility/serialization/10/property-name-validat…
gewarren 762a035
Update docs/core/compatibility/serialization/10/property-name-validat…
gewarren File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
docs/core/compatibility/serialization/10/property-name-validation.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| --- | ||
| title: "Breaking change: System.Text.Json checks for property name conflicts" | ||
| description: "Learn about the breaking change in .NET 10 where System.Text.Json validates that user-defined property names don't conflict with reserved metadata property names." | ||
| ms.date: 11/13/2025 | ||
| ai-usage: ai-assisted | ||
| --- | ||
| # System.Text.Json checks for property name conflicts | ||
|
|
||
| Under certain contexts, such as polymorphism and reference preservation, <xref:System.Text.Json> reserves specific property names (for example, `$type`, `$id`, and `$ref`) for emitting metadata. Previously, the serializer didn't perform validation on whether these property names conflicted with user-defined contracts, which could result in duplicate properties and produce JSON that was ambiguous or failed to round-trip. Starting with .NET 10, System.Text.Json enables validation to prevent such configurations and provides early warning to users. | ||
|
|
||
| ## Version introduced | ||
|
|
||
| .NET 10 | ||
|
|
||
| ## Previous behavior | ||
|
|
||
| Previously, the following code produced an invalid JSON object with duplicate `Type` properties and failed to deserialize with a <xref:System.Text.Json.JsonException>: | ||
|
|
||
| ```csharp | ||
| using System.Text.Json; | ||
| using System.Text.Json.Serialization; | ||
|
|
||
| string json = JsonSerializer.Serialize<Animal>(new Dog()); | ||
| Console.WriteLine(json); // {"Type":"dog","Type":"Dog"} | ||
| JsonSerializer.Deserialize<Animal>(json); // JsonException: Deserialized object contains a duplicate 'Type' metadata property. | ||
|
|
||
| [JsonPolymorphic(TypeDiscriminatorPropertyName = "Type")] | ||
| [JsonDerivedType(typeof(Dog), "dog")] | ||
| public abstract class Animal | ||
| { | ||
| public abstract string Type { get; } | ||
| } | ||
|
|
||
| public class Dog : Animal | ||
| { | ||
| public override string Type => "Dog"; | ||
| } | ||
| ``` | ||
|
|
||
| ## New behavior | ||
|
|
||
| Starting in .NET 10, any attempt to serialize that same type results in an early validation error: | ||
|
|
||
| > InvalidOperationException: The type 'Dog' contains property 'Type' that conflicts with an existing metadata property name. Consider either renaming it or ignoring it with JsonIgnoreAttribute. | ||
|
|
||
| This validation error occurs when the serializer is first created or when serialization is first attempted, providing early detection of invalid serialization contracts. | ||
|
|
||
| ## Type of breaking change | ||
|
|
||
| This change is a [behavioral change](../../categories.md#behavioral-change). | ||
|
|
||
| ## Reason for change | ||
|
|
||
| This change provides early prevention of invalid serialization contracts. By validating property names upfront, the serializer prevents scenarios where duplicate properties would be emitted, resulting in invalid JSON that cannot round-trip correctly. This helps developers identify and fix configuration issues during development rather than discovering them at run time during deserialization. | ||
|
|
||
| For more information, see: | ||
|
|
||
| - [[STJ] Disallow property names that conflict with metadata property names (dotnet/runtime#106390)](https://github.com/dotnet/runtime/issues/106390) | ||
| - [Disallow types with property names conflicting with metadata (dotnet/runtime#106460)](https://github.com/dotnet/runtime/pull/106460) | ||
|
|
||
| ## Recommended action | ||
|
|
||
| Avoid using property names that conflict with System.Text.Json-specific metadata properties (such as `$type`, `$id`, and `$ref`). If it's absolutely necessary to keep such a property in the class, apply a <xref:System.Text.Json.Serialization.JsonIgnoreAttribute> annotation on the conflicting property. | ||
|
|
||
| ## Affected APIs | ||
|
|
||
| - <xref:> | ||
gewarren marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
gewarren marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| - <xref:System.Text.Json.JsonSerializer.Deserialize*?displayProperty=fullName> | ||
| - <xref:System.Text.Json.JsonSerializer.DeserializeAsync*?displayProperty=fullName> | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.