fix(wire): handle null/empty in Content and DisplayBlockData UnmarshalJSON#204
Open
Ricardo-M-L wants to merge 1 commit into
Open
fix(wire): handle null/empty in Content and DisplayBlockData UnmarshalJSON#204Ricardo-M-L wants to merge 1 commit into
Ricardo-M-L wants to merge 1 commit into
Conversation
…lJSON
Content.UnmarshalJSON and DisplayBlockData.UnmarshalJSON switched on
data[0] right after bytes.TrimSpace without handling the JSON null literal
or guarding against empty input. Two consequences:
- A null content field in an otherwise valid message failed to parse:
json.Unmarshal([]byte(`{"output": null}`), &v) returned
"invalid content type" instead of leaving the field zero-valued. By
convention a json.Unmarshaler must treat null as a no-op.
- Empty/whitespace input panicked with index out of range [0] with
length 0.
Return early on empty input or the null literal in both unmarshalers, and
add tests covering null (standalone and as a struct field) and the
empty-input case. Existing behavior for valid strings, arrays/objects, and
invalid tokens is unchanged.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Author
|
Hi! Just a friendly ping on this PR. It's been open for a while — would appreciate a review when you get a chance. Thanks for your time! |
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.
Problem
Content.UnmarshalJSONandDisplayBlockData.UnmarshalJSONswitch ondata[0]immediately afterbytes.TrimSpace, with no handling for the JSONnullliteral and no guard for empty input:Two real consequences:
nullcontent field fails to parse.Contentis used as a bare (non-Optional) field in several wire types (ToolResultReturnValue.Output,PromptParams.UserInput,TurnBegin.UserInput). When the peer sendsnullfor one of them,encoding/jsoncallsContent.UnmarshalJSON([]byte("null")), which falls intodefaultand returnsinvalid content type— failing the entire message decode. By convention, ajson.Unmarshalershould treatnullas a no-op.runtime error: index out of range [0] with length 0.Reproduced against
mainbefore the fix:Fix
Return early on empty input or the
nullliteral in both unmarshalers:DisplayBlockDatais currently only reached throughOptional[DisplayBlockData](which already short-circuitsnull), but the same guard keeps it correct and consistent if it is ever used bare.Tests
Added to
wire/message_test.go:TestContent_UnmarshalJSON_Null—nullstandalone and as a struct field is a no-op.TestContent_UnmarshalJSON_EmptyDoesNotPanic— whitespace input no longer panics.TestDisplayBlockData_UnmarshalJSON_Null.Existing behavior for strings, arrays/objects, and invalid tokens (
TestContent_UnmarshalJSON_InvalidToken) is unchanged.Verification