Handle JSON null token when reading collections mapped to JSON#38459
Closed
Copilot wants to merge 5 commits into
Closed
Handle JSON null token when reading collections mapped to JSON#38459Copilot wants to merge 5 commits into
Copilot wants to merge 5 commits into
Conversation
Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix mapping issue for nullable reference collections in JSON
Handle JSON null token when reading collections mapped to JSON
Jun 18, 2026
There was a problem hiding this comment.
Pull request overview
This PR fixes JSON primitive-collection reader/writers so that a top-level JSON null token materializes as a null collection rather than throwing, aligning behavior across direct-reader usage (e.g., value converters) and nested JSON reads.
Changes:
- Updated
JsonCollectionOfReferencesReaderWriterandJsonCollectionOfStructsReaderWriterto returnnullwhen the current token isJsonTokenType.Null. - Updated
JsonCollectionOfNullableStructsReaderWriterto returnnullwhen the current token isJsonTokenType.Null(after the existingNoneadvance). - Added
JsonCollectionReaderWriterTestto assert"null"round-trips tonullacross reference, struct, and nullable-struct collection reader/writers.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| src/EFCore/Storage/Json/JsonCollectionOfReferencesReaderWriter.cs | Accept top-level Null token and return null rather than throwing. |
| src/EFCore/Storage/Json/JsonCollectionOfStructsReaderWriter.cs | Accept top-level Null token and return null rather than throwing. |
| src/EFCore/Storage/Json/JsonCollectionOfNullableStructsReaderWriter.cs | Accept top-level Null token and return null (with existing None handling). |
| test/EFCore.Tests/Storage/Json/JsonCollectionReaderWriterTest.cs | New unit tests validating "null" reads as null for various collection shapes. |
Comment on lines
+58
to
+61
| if (manager.CurrentReader.TokenType == JsonTokenType.Null) | ||
| { | ||
| return null!; | ||
| } |
…aderWriter Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
Member
|
Superseded by #38498 |
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.
A JSON column/property mapped to a primitive collection (e.g.
List<string>?) threwInvalidOperationException: Invalid token type: 'Null'when the stored value was a JSONnullliteral instead of an array.The three collection reader/writers only accepted a
StartArraytoken at the top level. Nulls are usually short-circuited before reaching the reader (e.g.FromJsonPropertyString), but the value-converter path (CollectionToJsonStringConverter.FromJsonString) and nested JSON documents can drive the reader directly to aNulltoken.Changes
JsonValueReaderWriter.FromJsonString: short-circuits on a top-levelJsonTokenType.Nulland returnsnull— one central place covers all reader/writers without needing per-class handling. Return type updated fromobjecttoobject?to accurately reflect thatnullis a valid return value.EFCore.baseline.json: updatedFromJsonStringsignature toobject?.Can_read_write_null_value_of_collection_of_string_JSON_values,Can_read_write_null_value_of_collection_of_int_JSON_values, andCan_read_write_null_value_of_collection_of_nullable_int_JSON_valuestoJsonTypesTestBase, asserting that a JSON"null"value round-trips as anullcollection while normal arrays still read correctly. TheCan_read_and_write_JSON_valuehelper now also exercises theFromJsonString("null")path for null values.