Preserve unrecognized token response fields through marshal roundtrip#150
Merged
Preserve unrecognized token response fields through marshal roundtrip#150
Conversation
Stashes the raw JSON payload on TokenResponse and merges it back during MarshalJSON so server-specific fields (e.g. email, environment_id, legal_entity_name) survive without enumerating known field names or introducing a --raw flag. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
dcluna
approved these changes
Apr 28, 2026
ikawalec
approved these changes
Apr 28, 2026
jdabrowski
approved these changes
Apr 29, 2026
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
email,environment_id,legal_entity_name) without a--rawflag.TokenResponsestashes the original payload in an unexportedraw map[string]json.RawMessage.MarshalJSONoverlays typed fields onto it, so unknowns ride along automatically.golang.org/x/oauth2's internal handling of unknown token fields.Notes
rawis non-empty the marshaled output goes through amap[string]json.RawMessage, so keys come out alphabetical (Go'sjson.Marshalsorts map keys). JSON-correct, just visually different from before.NewTokenResponseFromForm) leaverawnil, so their marshal output is byte-identical to today.encoding/json/v2has a,unknowntag that does this natively, but it's gated behindGOEXPERIMENT=jsonv2in Go 1.25 and not stable for public modules. Worth revisiting once it lands in stable Go.Test plan
go test ./...passesTestTokenResponseExtraFields— unknown fields survive roundtripTestTokenResponseNoExtraFields— output identical when no extras presentTestTokenResponseTypedFieldsWinOnConflict— typed assignments override raw values🤖 Generated with Claude Code