Fix null return condition in NullableConverter<T>.OnTryRead#129220
Open
prozolic wants to merge 2 commits into
Open
Fix null return condition in NullableConverter<T>.OnTryRead#129220prozolic wants to merge 2 commits into
prozolic wants to merge 2 commits into
Conversation
Change the early return guard in NullableConverter<T>.OnTryRead from !state.IsContinuation to state.Current.ObjectState == StackFrameObjectState.None. On continuation re-entry, ReadStack.Push resets _continuationCount to 0 when _continuationCount == _count (i.e., the deepest frame is reached), causing IsContinuation to return false before OnTryRead is entered. ObjectState is saved in the frame and shows if the element converter has started processing the current value. It is None only on the very first entry, so replacing !IsContinuation with ObjectState == None correctly skips the early-return regardless of _continuationCount state.
Contributor
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
Adds a regression test for stream-based deserialization around nullable structs and adjusts nullable reading logic to handle null tokens in continuation scenarios.
Changes:
- Add a new stream-based regression test targeting a buffer-boundary edge case.
- Update
NullableConverter<T>.OnTryReadto treatnulltokens asnullvalues based on stack object state rather thanIsContinuation.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Serialization/NullableTests.cs | Adds a new regression test and helper types for stream/buffer boundary handling. |
| src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/NullableConverter.cs | Adjusts nullable null token handling during incremental/continuation reads. |
Comment on lines
30
to
35
| internal override bool OnTryRead(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options, scoped ref ReadStack state, out T? value) | ||
| { | ||
| if (!state.IsContinuation && reader.TokenType == JsonTokenType.Null) | ||
| if (state.Current.ObjectState == StackFrameObjectState.None && reader.TokenType == JsonTokenType.Null) | ||
| { | ||
| value = null; | ||
| return true; |
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.
This PR change the early return guard in
NullableConverter<T>.OnTryReadfrom!state.IsContinuationtostate.Current.ObjectState == StackFrameObjectState.None.On continuation re-entry,
ReadStack.Pushresets_continuationCountto 0 when_continuationCount == _count(i.e., the deepest frame is reached), causingIsContinuationto return false beforeOnTryReadis entered.ObjectStateis saved in the frame and shows if the element converter has started processing the current value. It is None only on the very first entry, so replacing!IsContinuationwithObjectState == Nonecorrectly skips the early-return regardless of_continuationCountstate.Closes #125237