Skip to content

fix: handle legacy string format for customHeaders in ConnectionOptionsOAuth2 - #816

Open
alexmsenger wants to merge 1 commit into
auth0:v1from
alexmsenger:fix/oauth2-legacy-custom-headers-unmarshal
Open

fix: handle legacy string format for customHeaders in ConnectionOptionsOAuth2#816
alexmsenger wants to merge 1 commit into
auth0:v1from
alexmsenger:fix/oauth2-legacy-custom-headers-unmarshal

Conversation

@alexmsenger

Copy link
Copy Markdown

🔧 Changes

Extends ConnectionOptionsOAuth2.UnmarshalJSON to gracefully handle the customHeaders field when the Auth0 Management API returns it as a JSON-encoded string rather than a map[string]string object.

Prior to this change, the decoder attempted to unmarshal the raw value directly into *map[string]string, which produced:

json: cannot unmarshal string into Go struct field of type map[string]string

The fix intercepts customHeaders as interface{} (alongside the existing scope handling) and normalizes the two possible forms:

  • map[string]interface{} — decoded normally into *map[string]string
  • string (legacy format) — discarded gracefully; CustomHeaders is left nil so the operator can restore the values through their config

Types/methods changed:

  • ConnectionOptionsOAuth2.UnmarshalJSON in management/connection.go

📚 References

Closes #815

🔬 Testing

The existing make test suite passes with HTTP recordings unchanged —
the fix touches only unmarshal logic, not any API interaction.

Three new cases are added to TestOAuth2Connection_UnmarshalJSON in
management/connection_test.go, covering:

  1. Normal map form — {"customHeaders":{"X-Foo":"bar"}} → decoded into *map[string]string
  2. Legacy string form — {"customHeaders":"{\"X-Foo\":\"bar\"}"} → no error, CustomHeaders is nil
  3. Null form — {"customHeaders":null} → no error, CustomHeaders is nil

No new HTTP recordings are required as the bug is fully exercisable without
a live tenant.

📝 Checklist

  • All new/changed/fixed functionality is covered by tests (or N/A)
  • I have added documentation for all new/changed functionality (or N/A)

…nsOAuth2

Some older Auth0 tenants stored the customHeaders field as a JSON-encoded
string rather than a map[string]string object. When the SDK attempted to
unmarshal such a response directly into *map[string]string, it produced:

  cannot unmarshal string into Go struct field ... of type map[string]string

This change extends ConnectionOptionsOAuth2.UnmarshalJSON to intercept the
raw customHeaders value as interface{} (alongside the existing scope handling)
before encoding/json attempts direct decoding. The two cases are then handled
explicitly:

- map[string]interface{}: decoded normally into *map[string]string
- string (legacy format): discarded gracefully; the operator can restore
  custom header values in their Terraform config

Tests are added to TestOAuth2Connection_UnmarshalJSON covering the map,
legacy string, and null cases.
@alexmsenger
alexmsenger requested a review from a team as a code owner July 7, 2026 13:11
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.

1 participant