fix(ENG-4462): send cleared optional fields to the OAuth popup

JP
1d ago
+148-12

Summary

When an OAuth account is edited and an optional setup field is emptied, the Hub drops that field from the OAuth popup URL. uc-api then reconnects with the old value. This PR sends the emptied field names as cleared_credential_keys, so uc-api can remove them.

It also makes it a compile error to pass a credentials payload that has already lost its cleared fields to updateAccount, the PATCH path from ENG-902.

Overview: https://claude.ai/artifact/7GtpJfBy66EEzLHg2K4GJT

Linear: ENG-4462

Depends on StackOneHQ/unified-cloud-api#9956, which has to be live on production before the unified-cloud bump that ships this. An older uc-api would store the parameter as a junk credential.

Acceptance criteria

  • Editing an OAuth2 (authorization code) account and emptying an optional, non-secret, non-read-only, non-computed field sends its name in cleared_credential_keys, comma-joined and URL-encoded.
  • A cleared field is never sent as a value.
  • A new link sends no cleared_credential_keys, and its URL is unchanged.
  • Fields that still have a value are sent as today.

What changed

  • buildOAuthPopupUrl (utils/oauthPopupUrl.ts): a new helper that builds the popup URL. It takes the raw CredentialsPayload and derives both the values (withoutClearedFields) and the cleared names (clearedFieldKeys), so the hook has nothing to get wrong. A characterization test pins the URL from before the change.
  • clearedFieldKeys: new, in credentialsPayload.ts next to withoutClearedFields.
  • BuiltCredentialsPayload: a type-only brand that buildCredentialsPayload returns and updateAccount requires. A filled-only Record<string, string> can no longer be passed by accident; a @ts-expect-error test pins this.
  • The hook: passes credentials straight through, and applies withoutClearedFields inline only on the create path (connectAccount).

Sibling surfaces

SurfaceDecision
OAuth popup (handleConnect)changed
Non-OAuth edit (updateAccount, ENG-902)type tightened only; behaviour unchanged
Create (connectAccount)unchanged: no clears
OIDC and non-authorization-code OAuth2 grantsunchanged: they already take the PATCH path
Other /connect/oauth2 URL buildersnone, in hub, unified-cloud or legacy react-hub

Test plan

  • oauthPopupUrl.test.ts:
    • the existing URL is pinned
    • the parameter is appended only when there are clears, comma-joined and encoded
    • a null entry becomes a cleared name, never key=null
  • credentialsPayload.test.ts:
    • clearedFieldKeys returns only the null keys, and none for a create payload
    • the brand rejects a filled-only payload
  • Run locally: npm run lint (biome and tsc) and npm test (72 tests), all passing.

Rollout and rollback

  • Release order:
    1. uc-api #9956, live on production.
    2. This PR, then a release-please release.
    3. The unified-cloud @stackone/hub bump, which is what reaches users.
  • Rollback: revert the unified-cloud bump. uc-api ignores a missing parameter.

Review notes

This PR went through the same 3 bounded review rounds as #9956.

Deferred: credential keys and token are still interpolated into the popup URL unencoded. That predates this ticket, and the keys come from connector configs written by admins. Only the new parameter is encoded.

Known limit: MCP OAuth connectors are presented to the Hub as oauth2, so the Hub sends clears for them too, but uc-api's MCP flow ignores them, as it already ignores every edited setup value.

🤖 Generated with Claude Code