Add discriminatedUnion kind to V2RuntimeSchema coercion - #2801
Open
jar-stripe wants to merge 4 commits into
Open
Add discriminatedUnion kind to V2RuntimeSchema coercion#2801jar-stripe wants to merge 4 commits into
jar-stripe wants to merge 4 commits into
Conversation
The codegen now emits a discriminatedUnion schema for V2 methods whose request/response types contain discriminated unions with coercible fields. This adds the runtime handling: inspect the discriminator field value, look up the matching variant schema, and coerce only that variant's fields. 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:09
jar-stripe
requested review from
xavdid
and
a lite review from Copilot
and removed request for
a team
August 6, 2026 21:09
Tests discriminated union type shapes for both request-side (params with literal discriminator) and response-side (object fields), 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 4 out of 4 changed files in this pull request and generated no new comments.
Suppressed comments (2)
src/V2Coercion.ts:153
discriminatorValue in schema.variantsalso returns true for properties inherited fromObject.prototype(e.g.toString), which can causeschema.variants[discriminatorValue]to be a non-schema value and break coercion. Use an own-property check instead.
const obj = data as Record<string, unknown>;
const discriminatorValue = obj[schema.discriminator];
if (
typeof discriminatorValue === 'string' &&
discriminatorValue in schema.variants
) {
return coerceV2ResponseData(data, schema.variants[discriminatorValue]);
}
src/V2Coercion.ts:70
discriminatorValue in schema.variantsalso returns true for properties inherited fromObject.prototype(e.g.toString), which can causeschema.variants[discriminatorValue]to be a non-schema value and break coercion. Use an own-property check instead.
This issue also appears on line 146 of the same file.
const discriminatorValue = obj[schema.discriminator];
if (
typeof discriminatorValue === 'string' &&
discriminatorValue in schema.variants
) {
return coerceV2RequestData(data, schema.variants[discriminatorValue]);
}
Contributor
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 4 out of 4 changed files in this pull request and generated no new comments.
Suppressed comments (3)
src/V2Coercion.ts:152
- Using the
inoperator here can match properties inherited fromObject.prototype(e.g.toString,__proto__), which could cause an unintended variant match and then pass a non-schema value into coercion (leading toschema.kindbeing undefined and returningundefined). Prefer an own-property check onvariants.
if (
typeof discriminatorValue === 'string' &&
discriminatorValue in schema.variants
) {
return coerceV2ResponseData(data, schema.variants[discriminatorValue]);
src/V2Coercion.ts:70
- Using the
inoperator here can match properties inherited fromObject.prototype(e.g.toString,__proto__), which could cause an unintended variant match and then pass a non-schema value into coercion (leading toschema.kindbeing undefined and returningundefined). Prefer an own-property check onvariants.
This issue also appears on line 148 of the same file.
if (
typeof discriminatorValue === 'string' &&
discriminatorValue in schema.variants
) {
return coerceV2RequestData(data, schema.variants[discriminatorValue]);
test/DiscriminatedUnion.spec.ts:8
- This comment claims these hand-written types "exactly match" what codegen emits, but the file doesn't reference generated types or schemas, so it can drift and become misleading. Consider softening the wording to avoid asserting exact equivalence.
// Type definitions mirroring the shapes the codegen produces for discriminated
// unions. These are hand-written here so the test file is self-contained, but
// they exactly match the object-literal typedef shape the Node generator emits
// for each variant.
xavdid
approved these changes
Aug 7, 2026
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 a
discriminatedUnionschema kind for V2 methods whose request/response types contain discriminated unions with int64_string or decimal_string fields that need runtime coercion. The stripe-node runtime needs to handle this new schema kind.What?
{kind: 'discriminatedUnion', discriminator: string, variants: Record<string, V2RuntimeSchema>}to theV2RuntimeSchematype inTypes.tscase 'discriminatedUnion':to bothcoerceV2RequestDataandcoerceV2ResponseDatainV2Coercion.tsdata[schema.discriminator], looks up the matching variant schema, and recursively coerces only that variant's fields. If the discriminator value isn't found or data doesn't have the field, returns data as-is.Changelog