Skip to content

Preserve unrecognized token response fields through marshal roundtrip#150

Merged
mbilski merged 1 commit intomasterfrom
token-response-extra-fields
Apr 29, 2026
Merged

Preserve unrecognized token response fields through marshal roundtrip#150
mbilski merged 1 commit intomasterfrom
token-response-extra-fields

Conversation

@mbilski
Copy link
Copy Markdown
Contributor

@mbilski mbilski commented Apr 28, 2026

Summary

  • Alternative to Add --raw flag to output unmodified JSON token response #149 — keeps unknown server-specific token-response fields (e.g. email, environment_id, legal_entity_name) without a --raw flag.
  • TokenResponse stashes the original payload in an unexported raw map[string]json.RawMessage. MarshalJSON overlays typed fields onto it, so unknowns ride along automatically.
  • No enumeration of known field names, no reflection, no third-party deps. Pattern mirrors golang.org/x/oauth2's internal handling of unknown token fields.

Notes

  • When raw is non-empty the marshaled output goes through a map[string]json.RawMessage, so keys come out alphabetical (Go's json.Marshal sorts map keys). JSON-correct, just visually different from before.
  • Form-constructed responses (NewTokenResponseFromForm) leave raw nil, so their marshal output is byte-identical to today.
  • encoding/json/v2 has a ,unknown tag that does this natively, but it's gated behind GOEXPERIMENT=jsonv2 in Go 1.25 and not stable for public modules. Worth revisiting once it lands in stable Go.

Test plan

  • go test ./... passes
  • TestTokenResponseExtraFields — unknown fields survive roundtrip
  • TestTokenResponseNoExtraFields — output identical when no extras present
  • TestTokenResponseTypedFieldsWinOnConflict — typed assignments override raw values

🤖 Generated with Claude Code

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>
@mbilski mbilski merged commit d7c5b7d into master Apr 29, 2026
2 checks passed
@mbilski mbilski deleted the token-response-extra-fields branch April 29, 2026 07:53
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.

4 participants