fix: disambiguate struct unions when multiple variants share a discriminator value#692
Open
rsd-darshan wants to merge 1 commit into
Open
fix: disambiguate struct unions when multiple variants share a discriminator value#692rsd-darshan wants to merge 1 commit into
rsd-darshan wants to merge 1 commit into
Conversation
…minator value newStructUnionDecoder picked the first registered variant whose discriminator matched, even when several variants share the same value (for example, several "message" shaped variants distinguished only by other fields like role or the presence of an id). When more than one variant matches, now pick whichever variant's own fields account for the most of the JSON object's keys instead of always taking the first.
Author
|
Both issues describe the same underlying bug: |
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.
Summary
When a struct union registers more than one variant under the same discriminator value (for example, several "message" shaped variants distinguished only by other fields like
roleor the presence of anid),newStructUnionDecoderalways picked whichever matching variant was registered first, regardless of which one the data actually matched.For
ResponseInputItemUnionParam, three variants (EasyInputMessageParam,ResponseInputItemMessageParam,ResponseOutputMessageParam) are all registered under"type":"message". As a result, decoding an output message echoed back into the input (e.g.{"id":"msg_1","type":"message","role":"assistant","status":"completed","content":[...]}) always landed onEasyInputMessageParam, dropping theid/statusfields.Fix
When more than one registered variant matches the discriminator value, compare each candidate's own recognized JSON field names against the object's top-level keys and decode whichever one accounts for the most of them, instead of decoding the first match unconditionally. The existing single-match fast path is unchanged.
Test plan
internal/apijson/decodeparam_test.gothat registers two variants under the same discriminator value and asserts both directions resolve to the correct variant.go test ./...passes with no regressions.