Skip to content

Add nullable and discriminatedUnion field encoding coercion - #2113

Open
jar-stripe wants to merge 4 commits into
masterfrom
jar/discriminated-union-coercion
Open

Add nullable and discriminatedUnion field encoding coercion#2113
jar-stripe wants to merge 4 commits into
masterfrom
jar/discriminated-union-coercion

Conversation

@jar-stripe

Copy link
Copy Markdown
Contributor

Why?

The sdk-codegen now emits V2RuntimeSchema field encoding schemas that can include two new kinds: nullable and discriminatedUnion. 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?

  • Adds nullable kind handling to coerceRequestParams and coerceResponseValues in lib/Util/Int64.php: unwraps to the inner schema, passing null through unchanged
  • Adds discriminatedUnion kind handling to both methods: reads the discriminator field from the value, looks up the matching variant schema, and coerces using that schema
  • Adds decimal_string kind handling to coerceRequestParams: converts float/int values to string, matching the existing int64_string pattern
  • Adds 19 new tests covering all new code paths (happy path, wrong/missing discriminator, null values, non-array passthrough)

See Also

No external links.

Changelog

  • Adds support for nullable and discriminatedUnion field encoding coercion in the V2 API runtime, enabling correct int64/decimal type conversion for fields wrapped in these schema kinds

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
jar-stripe marked this pull request as ready for review August 6, 2026 21:08
@jar-stripe
jar-stripe requested a review from a team as a code owner August 6, 2026 21:08
@jar-stripe
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

Copilot AI left a comment

Copy link
Copy Markdown

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 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::coerceResponseValues to support nullable and discriminatedUnion schemas
  • 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 thread lib/Util/Int64.php
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 thread lib/Util/Int64.php
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
…ests

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Committed-By-Agent: claude
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