Skip to content

Apply [JsonPropertyName] to complex properties mapped to JSON - #39501

Merged
AndriySvyryd merged 1 commit into
dotnet:mainfrom
officialasishkumar:fix/39484-complex-json-property-name-attribute
Nov 4, 2026
Merged

AndriySvyryd merged 1 commit into
dotnet:mainfrom
officialasishkumar:fix/39484-complex-json-property-name-attribute

Conversation

@officialasishkumar

Copy link
Copy Markdown
Contributor

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, where RelationalNavigationJsonPropertyNameAttributeConvention applies the attribute. Moving JSON data from owned types to complex types therefore silently changed the JSON keys.

Fix

  • RelationalPropertyJsonPropertyNameAttributeConvention now also implements IComplexPropertyAddedConvention and IComplexPropertyFieldChangedConvention, the same way RequiredPropertyAttributeConvention covers complex properties. It sets the JSON property name of the complex type from the attribute with DataAnnotation precedence, so explicit HasJsonPropertyName(...) still wins, and HasJsonPropertyName(null) reverts to the CLR name.
  • A JSON property name is only meaningful for a complex property declared on a JSON-mapped type. RelationalModelValidator rejects one on the complex property mapped with ToJson() 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 implements IModelFinalizingConvention and 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):

  • the issue's model: the attribute names are used for a nested complex property, a nested complex collection and their properties, while the attribute on the ToJson() complex property itself is ignored;
  • the attribute is overridden by explicit HasJsonPropertyName("...") and by HasJsonPropertyName(null);
  • the attribute on complex properties that aren't mapped to JSON is ignored and the model is valid.

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 uses json_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 with HasJsonPropertyName(null) or an explicit name.

The public API baseline is updated for the added interfaces and ProcessModelFinalizing.

  • I've read the guidelines for contributing and seen the walkthrough
  • I've posted a comment on an issue with a detailed description of how I am planning to contribute and got approval from a member of the team
  • The code builds and tests pass locally (also verified by our automated build checks)
  • Commit messages follow this format:
        Summary of the changes
        - Detail 1
        - Detail 2

        Fixes #bugnumber
  • Tests for the changes have been added (for bug fixes / features)
  • Code follows the same patterns and style as existing code in this repo

- 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
Copilot AI balanced review requested due to automatic review settings November 2, 2026 16:48
@officialasishkumar
officialasishkumar requested a review from a team as a code owner November 2, 2026 16:48

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 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

@officialasishkumar

Copy link
Copy Markdown
Contributor Author

@dotnet-policy-service agree

@AndriySvyryd AndriySvyryd left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your contribution!

@AndriySvyryd
AndriySvyryd merged commit bf44b93 into dotnet:main Nov 4, 2026
19 of 20 checks passed
@github-actions github-actions Bot added the api-review This PR or issue is introducing public API changes that need to be reviewed label Nov 4, 2026
@github-actions

github-actions Bot commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

API review baseline changes for src/EFCore.Relational/EFCore.Relational.baseline.json

Show diff

The diff below was generated by ApiChief between the base and the PR.

- class Microsoft.EntityFrameworkCore.Metadata.Conventions.RelationalPropertyJsonPropertyNameAttributeConvention :
-     Microsoft.EntityFrameworkCore.Metadata.Conventions.PropertyAttributeConventionBase<System.Text.Json.Serialization.JsonPropertyNameAttribute>
+ class Microsoft.EntityFrameworkCore.Metadata.Conventions.RelationalPropertyJsonPropertyNameAttributeConvention :
+     Microsoft.EntityFrameworkCore.Metadata.Conventions.PropertyAttributeConventionBase<System.Text.Json.Serialization.JsonPropertyNameAttribute>,
+     Microsoft.EntityFrameworkCore.Metadata.Conventions.IComplexPropertyAddedConvention, Microsoft.EntityFrameworkCore.Metadata.Conventions.IConvention,
+     Microsoft.EntityFrameworkCore.Metadata.Conventions.IComplexPropertyFieldChangedConvention,
+     Microsoft.EntityFrameworkCore.Metadata.Conventions.IModelFinalizingConvention
+ virtual void ProcessModelFinalizing(IConventionModelBuilder modelBuilder, IConventionContext<IConventionModelBuilder> context);

@AndriySvyryd AndriySvyryd removed the api-review This PR or issue is introducing public API changes that need to be reviewed label Nov 4, 2026
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Nov 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[JsonPropertyName] is ignored for complex properties and complex collections mapped to JSON

3 participants