Skip to content

Add discriminated union serialization tests - #2798

Closed
jar-stripe wants to merge 1 commit into
masterfrom
jar/discriminated-unions
Closed

Add discriminated union serialization tests#2798
jar-stripe wants to merge 1 commit into
masterfrom
jar/discriminated-unions

Conversation

@jar-stripe

@jar-stripe jar-stripe commented Jul 30, 2026

Copy link
Copy Markdown
Contributor

Why?

Validates that the discriminated union serialization design works correctly in stripe-node before codegen produces these patterns. Exercises request-side encoding via flattenAndStringify (the SDK's actual form-param encoder) and response-side TypeScript type narrowing.

What?

  • Adds test/DiscriminatedUnion.spec.ts with 29 tests covering:
    • Request standalone DU encoding via flattenAndStringify: Verifies bracket-notation output for nested variant params (color[model]=rgb, color[r]=255), optional field omission, and discriminator + payload encoding
    • Request inline DU encoding via flattenAndStringify: Verifies top-level discriminator, nested variant payload encoding (card[number]=4242), base fields at top level, and non-selected variant absence
    • Response standalone DU type narrowing: TypeScript compile-time verification that discriminator-based narrowing resolves to correct variant types
    • Response inline DU deserialization: JSON round-trip into intersection types (BaseFields & (VariantA | VariantB)), discriminator-based field access, non-selected variant null check, full base+variant field preservation
    • Round-trip: flattenAndStringify for request direction, JSON parse for response direction

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
@jar-stripe

Copy link
Copy Markdown
Contributor Author

Collapsed into #2801 (coercion PR now includes serialization tests)

@jar-stripe jar-stripe closed this Aug 6, 2026
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.

1 participant