Skip to content

Preserve event context and clear inherited account scoping - #2329

Open
jar-stripe wants to merge 5 commits into
masterfrom
jar/fix-event-context-scoping
Open

jar-stripe wants to merge 5 commits into
masterfrom
jar/fix-event-context-scoping

Conversation

@jar-stripe

@jar-stripe jar-stripe commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

Why?

Event and notification requests must use the context Stripe returned in the payload. Previously, a notification's initial fetch could inherit the original client's Stripe-Account while also sending the notification's Stripe-Context, a combination the API rejects with HTTP 400. A related object fetched through a full Event also retained the original getter, so later mutations could revert to the client's account/context defaults.

Fetching and subsequent object operations now share the event's scope. When a notification has no context, its initial fetch clears the inherited context and account, matching the scope already attached to returned objects on master.

What?

  • Replace options cloning with a response-getter wrapper that retains the configured requestor; keep the StripeResponseGetter interface unchanged.
  • Apply the event context and explicitly suppress inherited account defaults for initial event/notification fetches and subsequent mutations, nested objects, and pagination.
  • Preserve master's explicit per-request context/account overrides and ordinary null/clearing behavior outside scoped clients. Conflicting explicit overrides remain a next-major follow-up.
  • Extend the existing notification regression to inspect both the initial GET and subsequent DELETE headers, stubbing the HTTP client so account leakage is exercised.

Validation

  • just test: all 2,107 tests passed.
  • just format-check and git diff --check passed.
  • Ran the strengthened StripeClientTest.eventFetchedRelatedObjectRetainsContext against master (afefabe430b): it fails because the initial GET includes Stripe-Account. It passes with this change.
  • Additional coverage checks absent/empty contexts, explicit overrides, legacy custom getters, nested objects, v1/v2 pagination, raw requests, streams, and validation delegation.

See Also

Configuration

  • skip-changefile: This PR is not a user-facing change, so there's no changefile.

…bject

Event.fetchRelatedObject() used the response getter it was deserialized
with directly, so a later no-options mutation on the returned object
could pick up whatever default account/context the original client had,
instead of the event's own context. EventNotification already avoided
this by deserializing with a context-scoped client; give Event the same
treatment by adding withStripeContext() to StripeResponseGetter /
LiveStripeResponseGetter, and have StripeClient.withStripeContext()
delegate to it instead of duplicating the options-cloning logic.

Fixes RUN_DEVSDK-3694 (java).

Committed-By-Agent: claude
Removed redundant comments about the context scoping.
@jar-stripe
jar-stripe marked this pull request as ready for review November 7, 2026 04:11
@jar-stripe
jar-stripe requested a review from a team as a code owner November 7, 2026 04:11
@jar-stripe
jar-stripe requested review from mnorth-stripe and removed request for a team November 7, 2026 04:11
@jar-stripe
jar-stripe enabled auto-merge (squash) November 7, 2026 04:11
default StripeResponseGetter withStripeContext(String context) {
throw new IllegalStateException(
"Cannot create a response getter with a custom context for this StripeResponseGetter implementation");
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this throw errors for our users if they created a client with a custom getter, eg. maybe in tests or something?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yea thats a good catch. let me play around with this to see how we can do this in a non breaking way.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reworked in a455d22. Now, we support a StripeResponseGetter wrapper that injects context and clears account in the default request options and then delegate to the underlying wrapper for all other operations. This should allow custom getters to work with this without an interface change.

@zacchua-stripe zacchua-stripe left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Just had a question about the error thrown by default in StripeResponseGetter

Committed-By-Agent: codex
Co-authored-by: codex <noreply@openai.com>
@jar-stripe jar-stripe changed the title Preserve Event's own context when fetching and mutating its related object Preserve event context and clear inherited account scoping Nov 10, 2026

This branch has not been deployed

No deployments
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.

2 participants