Skip to content

fix: dispatch UnmarshalJSON by implemented interface, not entrypoint - #589

Open
momomuchu wants to merge 1 commit into
goccy:masterfrom
momomuchu:fix/unmarshaler-context-dispatch
Open

fix: dispatch UnmarshalJSON by implemented interface, not entrypoint#589
momomuchu wants to merge 1 commit into
goccy:masterfrom
momomuchu:fix/unmarshaler-context-dispatch

Conversation

@momomuchu

Copy link
Copy Markdown

Description

Fixes #583.

Decode picked which UnmarshalJSON to call based on whether ContextOption
was set on the call (json.Unmarshal vs json.UnmarshalContext), instead of
checking which interface the concrete type actually implements. If a type only
implements UnmarshalJSON(context.Context, []byte) and gets decoded via plain
json.Unmarshal, the type assertion v.(json.Unmarshaler) panics (and vice
versa for the reverse case).

DecodeStream already handles this correctly: it type-switches on
unmarshalerContext vs json.Unmarshaler and only falls back to
context.Background() when ContextOption isn't set (since c05e1e2, #327).
This brings Decode in line with that, so both paths behave the same way.

Added a test covering both directions plus a check that the plain Unmarshaler
path still returns identical bytes to encoding/json.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Decoder Mishandles json.UnmarshalerContext When json.Unmarshal Called, json.Unmarshaler When json.UnmarshalContext Called

1 participant