Skip to content

feat(transformers): generate proper transformers for discriminated unions#3298

Open
lukeramsden wants to merge 1 commit intohey-api:mainfrom
architect-eng:claude/fix-discriminated-union-transformers-1woDw
Open

feat(transformers): generate proper transformers for discriminated unions#3298
lukeramsden wants to merge 1 commit intohey-api:mainfrom
architect-eng:claude/fix-discriminated-union-transformers-1woDw

Conversation

@lukeramsden
Copy link

Add support for generating runtime transformers for OpenAPI discriminated
unions (oneOf/anyOf with discriminator). Previously, the transformers plugin
would emit "schema is too complex" warnings and skip transformer generation
for these schemas.

Changes:

  • Add detectDiscriminatedUnion() to identify discriminated union patterns
    in the IR schema (logicalOperator: 'or' with 'and' items containing
    const discriminator values)
  • Add processDiscriminatedUnion() to generate if-else chains that switch
    on the discriminator property value and call variant-specific transformers
  • Add comprehensive test spec covering oneOf, anyOf, nested objects with
    dates, arrays of discriminated unions, and nested discriminated unions

The generated transformers now properly:

  • Transform date-time fields to Date objects in each variant
  • Transform int64 fields to BigInt in each variant
  • Handle optional date fields with null checks
  • Handle nested objects containing dates
  • Handle arrays of discriminated unions
  • Handle containers with nested discriminated union fields

Fixes #1636, #1613


Had Claude do a first pass at this because this is something we run in to in our codebase. Started from a fairly detailed prompt describing the specific issue I was having. Not super familiar with your codebase so let me know if this is the right approach :)

…ions

Add support for generating runtime transformers for OpenAPI discriminated
unions (oneOf/anyOf with discriminator). Previously, the transformers plugin
would emit "schema is too complex" warnings and skip transformer generation
for these schemas.

Changes:
- Add detectDiscriminatedUnion() to identify discriminated union patterns
  in the IR schema (logicalOperator: 'or' with 'and' items containing
  const discriminator values)
- Add processDiscriminatedUnion() to generate if-else chains that switch
  on the discriminator property value and call variant-specific transformers
- Add comprehensive test spec covering oneOf, anyOf, nested objects with
  dates, arrays of discriminated unions, and nested discriminated unions

The generated transformers now properly:
- Transform date-time fields to Date objects in each variant
- Transform int64 fields to BigInt in each variant
- Handle optional date fields with null checks
- Handle nested objects containing dates
- Handle arrays of discriminated unions
- Handle containers with nested discriminated union fields

Fixes hey-api#1636, hey-api#1613

https://claude.ai/code/session_015NvA9dsXMVRLR18NMorwgd
@bolt-new-by-stackblitz
Copy link

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

@vercel
Copy link

vercel bot commented Feb 3, 2026

@claude is attempting to deploy a commit to the Hey API Team on Vercel.

A member of the Team first needs to authorize it.

@changeset-bot
Copy link

changeset-bot bot commented Feb 3, 2026

⚠️ No Changeset found

Latest commit: 5b10498

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@dosubot dosubot bot added size:XXL This PR changes 1000+ lines, ignoring generated files. feature 🚀 Feature request. labels Feb 3, 2026
@lukeramsden
Copy link
Author

Interesting - I see Copilot in a similar area in #3296 ?

@codecov
Copy link

codecov bot commented Feb 3, 2026

Codecov Report

❌ Patch coverage is 1.83486% with 107 lines in your changes missing coverage. Please review.
✅ Project coverage is 34.74%. Comparing base (ce5977e) to head (5b10498).

Files with missing lines Patch % Lines
...api-ts/src/plugins/@hey-api/transformers/plugin.ts 1.83% 107 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3298      +/-   ##
==========================================
- Coverage   34.91%   34.74%   -0.17%     
==========================================
  Files         401      401              
  Lines       22237    22346     +109     
  Branches     1429     1429              
==========================================
+ Hits         7763     7765       +2     
- Misses      14470    14577     +107     
  Partials        4        4              
Flag Coverage Δ
unittests 34.74% <1.83%> (-0.17%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@pkg-pr-new
Copy link

pkg-pr-new bot commented Feb 3, 2026

Open in StackBlitz

@hey-api/codegen-core

npm i https://pkg.pr.new/hey-api/openapi-ts/@hey-api/codegen-core@3298

@hey-api/nuxt

npm i https://pkg.pr.new/hey-api/openapi-ts/@hey-api/nuxt@3298

@hey-api/openapi-ts

npm i https://pkg.pr.new/hey-api/openapi-ts/@hey-api/openapi-ts@3298

@hey-api/shared

npm i https://pkg.pr.new/hey-api/openapi-ts/@hey-api/shared@3298

@hey-api/types

npm i https://pkg.pr.new/hey-api/openapi-ts/@hey-api/types@3298

@hey-api/vite-plugin

npm i https://pkg.pr.new/hey-api/openapi-ts/@hey-api/vite-plugin@3298

commit: 5b10498

@mrlubos
Copy link
Member

mrlubos commented Feb 3, 2026

Yeah, GitHub was down again. I'll rerun it later and see what it comes up with and we can compare. Are you using Zod btw? There are other issues around this, we could use Zod as a transformer. Would that be desirable for you too?

@lukeramsden
Copy link
Author

Yeah, GitHub was down again. I'll rerun it later and see what it comes up with and we can compare. Are you using Zod btw? There are other issues around this, we could use Zod as a transformer. Would that be desirable for you too?

Ye - all Zod

@mrlubos
Copy link
Member

mrlubos commented Feb 3, 2026

@lukeramsden which transforms do you need? dates? bigint? Other?

@mrlubos
Copy link
Member

mrlubos commented Feb 3, 2026

@lukeramsden does this preview build work for you? #3301

@lukeramsden
Copy link
Author

@lukeramsden which transforms do you need? dates? bigint? Other?

dates is the main one

@lukeramsden
Copy link
Author

lukeramsden commented Feb 4, 2026

@lukeramsden does this preview build work for you? #3301

will try to check if i get time but the test cases in this PR (#3298) are representative of my usecase so if the preview build works for those then all good

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature 🚀 Feature request. size:XXL This PR changes 1000+ lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Transformers warning: too complex

3 participants