Add nullable and discriminatedUnion field encoding coercion - #2113
Open
jar-stripe wants to merge 4 commits into
Open
Add nullable and discriminatedUnion field encoding coercion#2113jar-stripe wants to merge 4 commits into
jar-stripe wants to merge 4 commits into
Conversation
Extends the V2RuntimeSchema field encoding coercion to handle two new schema kinds emitted by the code generator: - nullable: unwraps to the inner schema, passing null through unchanged - discriminatedUnion: selects the variant schema by reading the discriminator field's value from the data, then coerces using it Also adds decimal_string request coercion (float/int to string), matching the existing int64_string pattern. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> Committed-By-Agent: claude
jar-stripe
marked this pull request as ready for review
August 6, 2026 21:08
jar-stripe
requested review from
justiny-stripe
and
a lite review from Copilot
and removed request for
a team
August 6, 2026 21:08
Tests discriminated union serialization for both request-side (array params with literal 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
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 runtime support for additional V2 field-encoding schema kinds so int64/decimal coercion still occurs when fields are wrapped (e.g., nullable or discriminated unions).
Changes:
- Extend
Int64::coerceRequestParams/Int64::coerceResponseValuesto supportnullableanddiscriminatedUnionschemas - Add request-side coercion for
decimal_string - Add/extend tests for the new coercion behavior and discriminated-union parameter/serialization behavior
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 5 comments.
| File | Description |
|---|---|
| lib/Util/Int64.php | Adds handling for decimal_string, nullable, and discriminatedUnion in request/response coercion paths |
| tests/Stripe/Util/Int64Test.php | Adds coverage for new coercion branches (nullable, discriminated union, decimal) |
| tests/Stripe/DiscriminatedUnionTest.php | Adds tests around discriminated-union request parameter shapes and serialization/deserialization behavior |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+52
to
+58
| if ('discriminatedUnion' === $schema['kind'] && isset($schema['discriminator'], $schema['variants'])) { | ||
| if (\is_array($params) && \array_key_exists($schema['discriminator'], $params)) { | ||
| $discriminatorValue = $params[$schema['discriminator']]; | ||
| if (\array_key_exists($discriminatorValue, $schema['variants'])) { | ||
| return self::coerceRequestParams($params, $schema['variants'][$discriminatorValue]); | ||
| } | ||
| } |
Comment on lines
+128
to
+134
| } elseif ('discriminatedUnion' === $encoding['kind'] && isset($encoding['discriminator'], $encoding['variants'])) { | ||
| if (\is_array($value) && \array_key_exists($encoding['discriminator'], $value)) { | ||
| $discriminatorValue = $value[$encoding['discriminator']]; | ||
| if (\array_key_exists($discriminatorValue, $encoding['variants'])) { | ||
| $values = self::coerceResponseValues($values, [$field => $encoding['variants'][$discriminatorValue]]); | ||
| } | ||
| } |
Prevents a TypeError when the discriminator field value is null, an array, or any other non-string type — array_key_exists requires a string or int key. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> Committed-By-Agent: claude
xavdid
approved these changes
Aug 8, 2026
…ests 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 V2RuntimeSchema field encoding schemas that can include two new kinds:
nullableanddiscriminatedUnion. Without handling for these kinds in the PHP runtime, those fields would pass through uncoerced — int64_string fields nested inside a nullable or discriminated union wrapper would not be converted between their wire representation and their PHP type.What?
nullablekind handling tocoerceRequestParamsandcoerceResponseValuesinlib/Util/Int64.php: unwraps to the inner schema, passing null through unchangeddiscriminatedUnionkind handling to both methods: reads the discriminator field from the value, looks up the matching variant schema, and coerces using that schemadecimal_stringkind handling tocoerceRequestParams: converts float/int values to string, matching the existingint64_stringpatternSee Also
No external links.
Changelog
nullableanddiscriminatedUnionfield encoding coercion in the V2 API runtime, enabling correct int64/decimal type conversion for fields wrapped in these schema kinds