Add discriminated union serialization tests - #1922
Closed
jar-stripe wants to merge 2 commits into
Closed
Conversation
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
marked this pull request as ready for review
August 6, 2026 21:13
jar-stripe
requested review from
zacchua-stripe
and
a lite review from Copilot
and removed request for
a team
August 6, 2026 21:13
Contributor
Author
|
Collapsed into #1926 (coercion PR now includes serialization tests) |
Contributor
There was a problem hiding this comment.
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
RequestParamsfixtures (RgbColor,HsvColor) and assertsto_houtput. - 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why?
Validates that the discriminated union serialization design works correctly in stripe-ruby before codegen produces these patterns. Exercises both request-side (
RequestParams#to_hwith explicit-key tracking) and response-side (StripeObject.construct_fromrecursive deserialization).What?
test/stripe/discriminated_union_test.rbwith 17 tests covering:RgbColor/HsvColorRequestParams subclasses with fixed discriminator defaults, serialized viato_h. Verifies discriminator field is present, nil variants omitted, Union-typed field on parent encodes correctly.DrawParams.new(color: RgbColor.new(...))), verifying nested hash structure.PaymentMethodParamswith discriminator + per-variant nullable payload params (CardPayload,BankPayload). Verifies discriminator at top level, nested variant payload, non-selected variant absent, and partial payload fields.StripeObject.construct_fromwith flat JSON, verifying discriminator and field access via bracket notation and method-style accessors.StripeObject.construct_fromwith nested variant data ({"type" => "card", "card" => {"number" => "4242"}}), verifying recursive StripeObject construction,respond_to?for absent keys, andto_hashround-trip.