Skip to content

Improve remaining JsonReaderInvalidTokenType exceptions during JSON materialization - #39517

Open
Sean-mn wants to merge 1 commit into
dotnet:mainfrom
Sean-mn:fix/json-invalid-token-type-exceptions
Open

Sean-mn wants to merge 1 commit into
dotnet:mainfrom
Sean-mn:fix/json-invalid-token-type-exceptions

Conversation

@Sean-mn

@Sean-mn Sean-mn commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #39486

Description

Follow-up to #39485: replaces the remaining generic Invalid token type: '{tokenType}'. exceptions thrown during JSON materialization with messages that name what was being materialized and what JSON was expected.

  • Pass the structural property to IncludeJsonEntityCollection (as a liftable constant, like MaterializeJsonEntityCollection), so null / non-object elements in top-level JSON collections throw RelationalStrings.JsonEntityCollectionNullElement / RelationalStrings.JsonEntityCollectionExpectedObject.
  • Root token mismatches now throw RelationalStrings.JsonStructuralTypeExpectedObject (MaterializeJsonStructuralType, MaterializeJsonNullableValueStructuralType, IncludeJsonEntityReference) and RelationalStrings.JsonEntityCollectionExpectedArray (MaterializeJsonEntityCollection, IncludeJsonEntityCollection).
  • The EndObject checks in both collection methods now report manager.CurrentReader.TokenType instead of the stale StartObject token.
  • JsonCollectionOfStructsReaderWriter, JsonCollectionOfNullableStructsReaderWriter and JsonCollectionOfReferencesReaderWriter now throw CoreStrings.JsonCollectionExpectedArray / CoreStrings.JsonCollectionInvalidElement, which include the element type.
  • Tests: updated the Try_project_collection_but_JSON_is_entity / Try_project_reference_but_JSON_is_collection assertions, added include-path tests in JsonQueryRelationalTestBase, and added JsonCollectionReaderWriterTest. The EndObject fix isn't covered by a test since that branch isn't reachable with valid shapers.

  • 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

@Sean-mn
Sean-mn marked this pull request as ready for review November 5, 2026 03:06
@Sean-mn
Sean-mn requested a review from a team as a code owner November 5, 2026 03:06
@AndriySvyryd AndriySvyryd self-assigned this Nov 7, 2026
@AndriySvyryd
AndriySvyryd requested a lite review from Copilot November 7, 2026 00:35

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 identified, and the change includes focused regression coverage and corresponding diagnostic/API updates for the JSON materialization scenarios.

No open issues 🎉

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

This pull request improves diagnostics for malformed JSON during relational materialization and primitive JSON collection deserialization. Generic token-type errors are replaced with messages describing the expected JSON shape and relevant structural or element type.

Changes:

  • Adds contextual errors for JSON object/array mismatches and invalid or null collection elements.
  • Passes structural-property metadata through JSON collection include processing and corrects stale token reporting.
  • Adds reader/writer and relational regression tests, diagnostic resources, generated accessors, and API baseline updates.
  • Review note: add focused coverage for nullable value-type JSON structural projections with an array root.
File Description
test/​EFCore.Tests/​Storage/​Json/​JsonCollectionReaderWriterTest.cs Tests descriptive errors from primitive collection JSON reader/writers.
test/​EFCore.Relational.Specification.Tests/​Query/​JsonQueryRelationalTestBase.cs Updates expected diagnostics and adds include-path regression tests.
src/​EFCore/​Storage/​Json/​JsonCollectionOfStructsReaderWriter.cs Reports collection element type and expected JSON token context.
src/​EFCore/​Storage/​Json/​JsonCollectionOfReferencesReaderWriter.cs Reports collection element type and expected JSON token context.
src/​EFCore/​Storage/​Json/​JsonCollectionOfNullableStructsReaderWriter.cs Reports collection element type and expected JSON token context.
src/​EFCore/​Properties/​CoreStrings.resx Adds primitive collection diagnostic resources.
src/​EFCore/​Properties/​CoreStrings.Designer.cs Generates accessors for the new core diagnostic resources.
src/​EFCore/​EFCore.baseline.json Records the new CoreStrings API surface.
src/​EFCore.Relational/​Query/​RelationalShapedQueryCompilingExpressionVisitor.ShaperProcessingExpressionVisitor.cs Supplies structural metadata to included JSON collection materialization.
src/​EFCore.Relational/​Query/​RelationalShapedQueryCompilingExpressionVisitor.ShaperProcessingExpressionVisitor.ClientMethods.cs Adds descriptive root and element diagnostics and corrects token reporting.
src/​EFCore.Relational/​Properties/​RelationalStrings.resx Adds relational JSON shape and collection diagnostic resources.
src/​EFCore.Relational/​Properties/​RelationalStrings.Designer.cs Generates accessors for the new relational diagnostic resources.
src/​EFCore.Relational/​EFCore.Relational.baseline.json Records the new RelationalStrings API surface.

Review effort: Lite

…aterialization

- Pass the structural property to IncludeJsonEntityCollection so top-level JSON collections throw JsonEntityCollectionNullElement/JsonEntityCollectionExpectedObject for invalid elements
- Name the structural type or property and the expected JSON object/array when the root JSON token doesn't match
- Report the current token instead of the stale StartObject token in the EndObject checks
- Include the element type in the exceptions thrown by the primitive collection JsonValueReaderWriters

Fixes dotnet#39486
@Sean-mn
Sean-mn force-pushed the fix/json-invalid-token-type-exceptions branch from 6660f6f to 58fc5c7 Compare November 7, 2026 17:52
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.

Improve remaining JsonReaderInvalidTokenType exceptions during JSON materialization

3 participants