Skip to content

Fix stripe_account and stripe_context in Event#fetch_related_object - #2022

Merged
jar-stripe merged 7 commits into
masterfrom
jar/fix-event-context-scoping
Nov 10, 2026
Merged

jar-stripe merged 7 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?

A directly-retrieved Event (as opposed to an EventNotification built from a webhook) kept falling back to the retrieving client's own stripe_account instead of the event's own context when fetching a related object, because _request's combine_opts merge re-applied the Event's retained @opts.

What?

  • Overrides Event#_request to route through a stripe_context-scoped, stripe_account-free requestor built once per event, mirroring how EventNotification already scopes its @client at construction time.
  • Adds a test that directly retrieves an Event, calls fetch_related_object, and asserts only the event's own Stripe-Context is sent on both that call and a subsequent call on the fetched object, with no stale Stripe-Account.
  • Adds a changefile for the fix.

See Also

Configuration

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

jar-stripe and others added 3 commits November 6, 2026 14:47
A directly-retrieved Event#fetch_related_object fell back to the
retrieving client's own stripe_account instead of the event's own
context, because _request's combine_opts merge re-applied the Event's
retained @opts. EventNotification already avoids this by scoping its
@client to the notification's context at construction time; Event now
does the equivalent by routing _request through a context-scoped,
account-free requestor.

Fixes RUN_DEVSDK-3694 (ruby portion).

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Committed-By-Agent: claude
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Committed-By-Agent: claude
@jar-stripe jar-stripe changed the title Fix account scoping on Event#fetch_related_object Fix stripe_account and stripe_context in Event#fetch_related_object Nov 6, 2026
jar-stripe and others added 2 commits November 6, 2026 19:37
Event is generated from the OpenAPI spec, so the scoped_requestor/
_request override from the previous commit lived in a generated file
with no durable home across regeneration. sdk-codegen (stripe/sdk-codegen#4349)
now generates `Event < EventBase` instead of `Event < APIResource`;
EventBase is a new hand-maintained class, autoloaded the same way as
EventNotification, that carries the scoping logic verbatim. Regenerated
via `just gen ruby --repo <this worktree>`.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Committed-By-Agent: claude
@jar-stripe
jar-stripe marked this pull request as ready for review November 7, 2026 04:21
@jar-stripe
jar-stripe requested a review from a team as a code owner November 7, 2026 04:21
@jar-stripe
jar-stripe requested review from xavdid and a balanced review from Copilot and removed request for a team November 7, 2026 04:21

Copilot AI 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.

🟡 Changes recommended

Related-object fetching discards retained API key and version overrides, risking authentication or schema mismatches, while missing EventBase RBI declarations leave Sorbet consumers with unresolved inheritance.

2 open issues
2 files not reviewed
  • rbi/stripe.rbi: File type not supported
  • rbi/stripe/resources/v2/core/event.rbi: File type not supported
What changed in this PR

This PR updates related-object requests from directly retrieved v2 events to use the event’s own context rather than the retrieving client’s account settings.

Changes:

  • Introduces a shared EventBase with an event-scoped requestor and updates event inheritance.
  • Adds regression coverage for context propagation and removal of stale account headers.
  • Updates type annotations and adds a patch changefile.
File Description
test/​stripe/​v2_event_test.rb Tests routing headers on related-object fetches and subsequent requests.
rbi/​stripe/​resources/​v2/​core/​event.rbi Changes the event’s declared superclass to EventBase.
rbi/​stripe.rbi Updates event inheritance in the combined annotations.
lib/​stripe/​resources/​v2/​core/​event.rb Makes events inherit the shared request behavior.
lib/​stripe/​resources/​v2/​core/​event_base.rb Adds a cached, context-scoped requestor.
lib/​stripe/​resources.rb Registers and loads EventBase.
.hark/​changes/​2026-11-06_jar_fix-event-context-scoping.change.md Documents the context-scoping fix.

Review effort: Balanced

Comment thread rbi/stripe/resources/v2/core/event.rbi Resolved
…re EventBase RBI

EventBase#_request skipped RequestOptions.combine_opts(@opts, req_opts)
entirely, so a retained per-object api_key or stripe_version override
(e.g. from events.retrieve(id, api_key: 'rk_restricted')) was silently
dropped on fetch_related_object, falling back to the ambient
requestor's default. Restores the merge, with @opts's own
stripe_account/stripe_context forced to the safe values first so a
stale account can't leak back in through it.

Also ships a hand-maintained rbi/stripe/resources/v2/core/event_base.rbi,
mirroring event_notification.rbi, since EventBase is referenced as
Event's superclass in the generated RBI but was otherwise undeclared
anywhere in the packaged gem.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Committed-By-Agent: claude
@jar-stripe
jar-stripe enabled auto-merge (squash) November 7, 2026 04:40
@jar-stripe
jar-stripe merged commit c3cc21f into master Nov 10, 2026
16 checks passed
@jar-stripe
jar-stripe deleted the jar/fix-event-context-scoping branch November 10, 2026 03:21
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.

3 participants