Repository navigation
Apply [JsonPropertyName] to complex properties mapped to JSON - #39501
Merged
AndriySvyryd merged 1 commit intoNov 4, 2026
Merged
AndriySvyryd merged 1 commit into
AndriySvyryd merged 1 commit into
Conversation
- RelationalPropertyJsonPropertyNameAttributeConvention now also handles complex properties and complex collections, configuring the JSON property name of nested complex types from the attribute with data annotation precedence, as it already does for scalar properties and as RelationalNavigationJsonPropertyNameAttributeConvention does for owned navigations. Previously only scalar properties inside a JSON-mapped complex type honored the attribute, so nested complex properties were stored and queried under their CLR names. - On model finalizing, names that came from the attribute are removed from complex properties that aren't declared on a JSON-mapped type (the complex property mapped with ToJson() itself, or complex properties mapped to table columns), where a JSON property name is not used and would otherwise fail model validation. - Explicit configuration still wins, and HasJsonPropertyName(null) reverts to the CLR name. Fixes dotnet#39484
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues are reported for the changes extending JSON property-name attribute support to nested complex properties and collections.
No open issues 🎉
What changed in this PR
Fixes #39484 by extending EF Core’s relational conventions to honor [JsonPropertyName] on nested complex properties and complex collections mapped to JSON, aligning their naming behavior with owned navigations.
Changes:
- Applies attribute-based JSON names while preserving explicit configuration precedence.
- Removes attribute-derived names where they do not apply: JSON roots and complex properties mapped to table columns.
- Adds regression tests and updates the public API baseline.
| File | Description |
|---|---|
| test/EFCore.Relational.Specification.Tests/ModelBuilding/RelationalModelBuilderTest.cs | Tests nested JSON names, explicit overrides, and non-JSON mappings. |
| src/EFCore.Relational/Metadata/Conventions/RelationalPropertyJsonPropertyNameAttributeConvention.cs | Handles complex-property attributes and removes inapplicable names during model finalization. |
| src/EFCore.Relational/EFCore.Relational.baseline.json | Records the added convention interfaces and finalization method. |
Review effort: Balanced
Contributor
Author
|
@dotnet-policy-service agree |
AndriySvyryd
approved these changes
Nov 4, 2026
AndriySvyryd
left a comment
Member
There was a problem hiding this comment.
Thanks for your contribution!
Contributor
API review baseline changes for
|
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes #39484
[JsonPropertyName]was applied to scalar properties inside a JSON-mapped complex type, but ignored for nested complex properties and complex collections. They were stored and queried under their CLR names, unlike the equivalent owned navigations, whereRelationalNavigationJsonPropertyNameAttributeConventionapplies the attribute. Moving JSON data from owned types to complex types therefore silently changed the JSON keys.Fix
RelationalPropertyJsonPropertyNameAttributeConventionnow also implementsIComplexPropertyAddedConventionandIComplexPropertyFieldChangedConvention, the same wayRequiredPropertyAttributeConventioncovers complex properties. It sets the JSON property name of the complex type from the attribute withDataAnnotationprecedence, so explicitHasJsonPropertyName(...)still wins, andHasJsonPropertyName(null)reverts to the CLR name.RelationalModelValidatorrejects one on the complex property mapped withToJson()itself, and on complex properties mapped to table columns. Both models are valid today even when the CLR property carries[JsonPropertyName], for example for API serialization. To keep them valid, the convention also implementsIModelFinalizingConventionand removes a name that came from the attribute in those two cases. Explicitly configured names are left alone, so the validation still applies to them.Tests (
RelationalModelBuilderTest.RelationalComplexTypeTestBase, run by the SQL Server and SQLite generic/non-generic model builder tests):ToJson()complex property itself is ignored;HasJsonPropertyName("...")and byHasJsonPropertyName(null);The first test fails without the change (
Expected: "a",Actual: "Author"). Without the finalizing step, all three tests fail model validation. I also checked end to end on SQLite with the model from the issue. Saving produces{"t":"x","a":{"n":"bob"},"tg":[{"n":"ef"}]}, the query usesjson_each("b"."details", '$.tg')and"b"."details" ->> '$.a.n', and the entity materializes correctly.Note that this changes the JSON keys used for existing models that already have
[JsonPropertyName]on nested complex properties mapped to JSON. Such models can keep the CLR names withHasJsonPropertyName(null)or an explicit name.The public API baseline is updated for the added interfaces and
ProcessModelFinalizing.