Skip to content

Improve exceptions for invalid elements in JSON entity collections - #39485

Merged
AndriySvyryd merged 1 commit into
dotnet:mainfrom
Sean-mn:fix/json-collection-element-exceptions
Nov 3, 2026
Merged

AndriySvyryd merged 1 commit into
dotnet:mainfrom
Sean-mn:fix/json-collection-element-exceptions

Conversation

@Sean-mn

@Sean-mn Sean-mn commented Oct 31, 2026

Copy link
Copy Markdown
Contributor

Fixes #35863

Description

  • Throw a descriptive exception naming the entity type and navigation (RelationalStrings.JsonEntityCollectionNullElement) when a null element is encountered in MaterializeJsonEntityCollection.
  • Throw a descriptive exception naming the entity type and navigation (RelationalStrings.JsonEntityCollectionExpectedObject) when a non-object element is encountered in MaterializeJsonEntityCollection.
  • Add functional tests in JsonQueryRelationalTestBase covering both cases.

  • 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

- Throw a descriptive exception naming the entity type and navigation when a null element is encountered in MaterializeJsonEntityCollection
- Throw a descriptive exception naming the entity type and navigation when a non-object element is encountered in MaterializeJsonEntityCollection
- Add tests for both null and non-object elements in JSON entity collections

Fixes dotnet#35863
@Sean-mn
Sean-mn requested a review from a team as a code owner October 31, 2026 10:24
@Sean-mn
Sean-mn force-pushed the fix/json-collection-element-exceptions branch from f7c0e3a to 292b4a4 Compare October 31, 2026 10:24
@Sean-mn

Sean-mn commented Oct 31, 2026

Copy link
Copy Markdown
Contributor Author

@dotnet-policy-service agree

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 remain, so the diagnostic, localization, API baseline, and functional test changes can proceed.

No open issues 🎉

1 file not reviewed
  • src/EFCore.Relational/Properties/RelationalStrings.Designer.cs: Generated file
What changed in this PR

This pull request improves diagnostics for invalid elements in relational JSON collections by distinguishing null and non-object values and identifying the affected type and property. It also adds localized resources, generated accessors, API baseline updates, and functional test coverage.

Changes:

  • Added descriptive exceptions for null and non-object JSON collection elements.
  • Added localization resources, generated accessors, and API baseline entries.
  • Added functional tests covering both invalid element cases.
  • The diagnostics currently describe malformed complex-property collections as entity navigations and require correction.

This pull request requires changes.

File Description
test/​EFCore.Relational.Specification.Tests/​Query/​JsonQueryRelationalTestBase.cs Adds functional coverage for null and non-object JSON collection elements.
src/​EFCore.Relational/​Query/​RelationalShapedQueryCompilingExpressionVisitor.ShaperProcessingExpressionVisitor.ClientMethods.cs Throws descriptive exceptions for invalid JSON collection elements.
src/​EFCore.Relational/​Properties/​RelationalStrings.resx Defines the new localized diagnostic messages.
src/​EFCore.Relational/​Properties/​RelationalStrings.Designer.cs Provides generated accessors for the new messages.
src/​EFCore.Relational/​EFCore.Relational.baseline.json Records the added public resource methods in the API baseline.

Review effort: Lite

@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 3315bd1 into dotnet:main Nov 3, 2026
18 of 19 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 3, 2026
@github-actions

github-actions Bot commented Nov 3, 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.

  static class Microsoft.EntityFrameworkCore.Diagnostics.RelationalStrings
+ static string JsonEntityCollectionExpectedObject(object? entityType, object? navigation);
+ static string JsonEntityCollectionNullElement(object? entityType, object? navigation);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api-review This PR or issue is introducing public API changes that need to be reviewed community-contribution

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Improve exceptions for invalid JSON values in JSON column

3 participants