Skip to content

Add discriminated union serialization tests - #1922

Closed
jar-stripe wants to merge 2 commits into
masterfrom
jar/discriminated-unions
Closed

Add discriminated union serialization tests#1922
jar-stripe wants to merge 2 commits into
masterfrom
jar/discriminated-unions

Conversation

@jar-stripe

@jar-stripe jar-stripe commented Jul 30, 2026

Copy link
Copy Markdown
Contributor

Why?

Validates that the discriminated union serialization design works correctly in stripe-ruby before codegen produces these patterns. Exercises both request-side (RequestParams#to_h with explicit-key tracking) and response-side (StripeObject.construct_from recursive deserialization).

What?

  • Adds test/stripe/discriminated_union_test.rb with 17 tests covering:
    • Request standalone union: RgbColor/HsvColor RequestParams subclasses with fixed discriminator defaults, serialized via to_h. Verifies discriminator field is present, nil variants omitted, Union-typed field on parent encodes correctly.
    • Request standalone union as nested field: Standalone DU placed inside parent params (DrawParams.new(color: RgbColor.new(...))), verifying nested hash structure.
    • Request inline union: PaymentMethodParams with discriminator + per-variant nullable payload params (CardPayload, BankPayload). Verifies discriminator at top level, nested variant payload, non-selected variant absent, and partial payload fields.
    • Response standalone union: StripeObject.construct_from with flat JSON, verifying discriminator and field access via bracket notation and method-style accessors.
    • Response inline union: StripeObject.construct_from with nested variant data ({"type" => "card", "card" => {"number" => "4242"}}), verifying recursive StripeObject construction, respond_to? for absent keys, and to_hash round-trip.

Tests discriminated union serialization for both request-side
(RequestParams with defaulted discriminator) and response-side
(StripeObject construction), covering standalone and inline variants.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Committed-By-Agent: claude
@jar-stripe
jar-stripe marked this pull request as ready for review August 6, 2026 21:13
@jar-stripe
jar-stripe requested a review from a team as a code owner August 6, 2026 21:13
@jar-stripe
jar-stripe requested review from zacchua-stripe and a lite review from Copilot and removed request for a team August 6, 2026 21:13
@jar-stripe

Copy link
Copy Markdown
Contributor Author

Collapsed into #1926 (coercion PR now includes serialization tests)

@jar-stripe jar-stripe closed this Aug 6, 2026

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.

Pull request overview

Warning

Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.

Adds a new test suite to validate discriminated union serialization/deserialization behavior in stripe-ruby, covering both request parameter serialization (including nested inline unions) and response-side StripeObject.construct_from.

Changes:

  • Introduces standalone union RequestParams fixtures (RgbColor, HsvColor) and asserts to_h output.
  • Adds nested/inline union serialization coverage via DrawParams.
  • Adds response-side construction tests ensuring discriminator/variant fields are accessible on StripeObject.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +67 to +77
should "default discriminator to the variant name when not explicitly supplied" do
# When the caller omits `model:`, the initialize default sets @model but
# the explicit-key tracker does not include it. Callers who rely on the
# default must pass the discriminator explicitly for it to appear in the
# serialized hash; this test documents that expectation.
params = RgbColor.new(model: "rgb", r: 10)
result = params.to_h

assert_equal "rgb", result[:model], "discriminator must be present in serialized output"
assert_equal 10, result[:r]
end
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