Add nullable and discriminated_union field encoding coercion - #1923
Open
jar-stripe wants to merge 4 commits into
Open
Add nullable and discriminated_union field encoding coercion#1923jar-stripe wants to merge 4 commits into
jar-stripe wants to merge 4 commits into
Conversation
Consolidates request-side (encode: native → wire) and response-side
(decode: wire → native) type coercion into a shared module that handles
all V2 encoding kinds: int64_string, decimal_string, object, array,
nullable, and discriminated_union.
Previously, request_params.rb had its own coercion implementation and
stripe_object.rb only handled decimal_string with a flat equality check.
This left int64_string fields uncoerced on the response side (returning
String instead of Integer) and didn't support nested schemas like
{kind: :nullable, inner: :decimal_string}.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Committed-By-Agent: claude
Extend RequestParams#coerce_composite to handle two new field encoding kinds emitted by the V2RuntimeSchema codegen: - :nullable — short-circuits on nil (already handled by coerce_value), then recurses with the inner schema for non-nil values - :discriminated_union — reads the discriminator field from the hash, looks up the matching variant schema by symbol key, and coerces the whole value using that variant's schema; returns the value unchanged if the discriminator key is absent or no variant matches Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> Committed-By-Agent: claude
jar-stripe
force-pushed
the
jar/discriminated-union-coercion
branch
from
August 6, 2026 19:03
fe994b7 to
dfdef32
Compare
jar-stripe
marked this pull request as ready for review
August 6, 2026 21:01
jar-stripe
requested review from
kidus-stripe
and
a lite review from Copilot
and removed request for
a team
August 6, 2026 21:01
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
Contributor
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 7 out of 7 changed files in this pull request and generated no new comments.
Suppressed comments (2)
test/stripe/discriminated_union_test.rb:71
- This test claims to cover the case where the discriminator is not explicitly supplied, but it passes
model: "rgb", so it isn't exercising the behavior described in the comment. Either omitmodel:and assert it’s not serialized, or change the test name/comment to reflect explicit passing.
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.
test/stripe/discriminated_union_test.rb:14
- The fixture comment says callers only need to supply variant-specific fields because the discriminator defaults, but
RequestParams#to_honly includes keys that were explicitly set. With the currentinitializeusing@model = model, omittingmodel:will not serialize the discriminator, so this comment is misleading.
This issue also appears on line 67 of the same file.
# A standalone union where each variant is a distinct RequestParams class.
# The discriminator field (model) defaults to the variant name so callers
# only need to supply the variant-specific fields.
xavdid
approved these changes
Aug 8, 2026
Inline module_function declarations, add super() calls in test fixture initializers, and disable Naming/MethodParameterName for color component params (r, g, b, h, s, v). Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> Committed-By-Agent: claude
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?
The sdk-codegen now emits
V2RuntimeSchemafield encoding descriptors with two new composite kinds —nullableanddiscriminated_union— that the Ruby runtime'sRequestParamscoercion layer didn't yet handle. Without these cases, fields wrapped in a nullable or behind a discriminated union would be passed through uncoerced, silently droppingint64_string/decimal_stringconversions on nested fields.What?
:nullablehandling tocoerce_composite: defers nil to the existing early-return incoerce_value, then recurses withencoding[:inner]for non-nil values.:discriminated_unionhandling tocoerce_composite: reads the discriminator field value from the hash (handles both symbol and string hash keys), looks up the matching variant schema by symbol key, and coerces the entire hash using that variant's schema. Returns the value unchanged if the discriminator field is absent or no variant matches.coerce_discriminated_unionclass method to keep the logic self-contained.See Also
Changelog
nullableanddiscriminated_unionfield encoding kinds inRequestParamscoercion, enabling correctint64_string/decimal_stringserialization for fields nested inside nullable wrappers or discriminated unions.