Skip to content

fix(openapi-typescript): enumValues generates valid TypeScript for oneOf/anyOf unions - #2536

Merged
drwpow merged 3 commits into
openapi-ts:mainfrom
darkbasic:fix/union-enums
Feb 8, 2026
Merged

drwpow merged 3 commits into
openapi-ts:mainfrom
darkbasic:fix/union-enums

Conversation

@darkbasic

Copy link
Copy Markdown
Contributor

Changes

When using enumValues: true with OpenAPI schemas containing oneOf/anyOf unions, the generated TypeScript type paths would fail because properties might only exist on some union variants.

Before (invalid TypeScript):

export const resourceItemsNestedCodeValues: ReadonlyArray<
  FlattenedDeepRequired<components>["schemas"]["Resource"]["items"]["nested"]["code"]
> = ["a", "b"];
// Error: Property 'nested' does not exist on type '{ type: "simple"; } | { type: "complex"; nested: { code: "a" | "b"; }; }'

After (valid TypeScript):

export const resourceItemsOneOf1NestedCodeValues: ReadonlyArray<
  Extract<Extract<FlattenedDeepRequired<components>["schemas"]["Resource"]["items"], {
    nested: unknown;
  }>["nested"], {
    code: unknown;
  }>["code"]
> = ["a", "b"];

The fix wraps property accesses after oneOf/anyOf indices with Extract<> to narrow the union type before accessing variant-specific properties.

This PR has been tested in a production environment with a custom build of openapi-typescript.

How to Review

I've added three tests:

  • Basic union variant narrowing
  • Same property name with different inner schemas
  • Deeply nested unions (multiple Extract levels)

Checklist

  • Unit tests updated
  • [] docs/ updated (if necessary)
  • [] pnpm run update:examples run (only applicable for openapi-typescript)

@darkbasic
darkbasic requested a review from a team as a code owner November 28, 2025 16:35
@netlify

netlify Bot commented Nov 28, 2025 •

Copy link
Copy Markdown

👷 Deploy request for openapi-ts pending review.

Visit the deploys page to approve it

Name Link
🔨 Latest commit 19638fa

@changeset-bot

changeset-bot Bot commented Nov 28, 2025 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 19638fa

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
openapi-typescript Patch
swr-openapi Patch

Not sure what this means? Click here to learn what changesets are.

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

@darkbasic

Copy link
Copy Markdown
Contributor Author

Note: two tests will fail due to network errors (probably the original gist has been deleted). Nothing I can do on that front (they were already failing on main).

@sacha-c

sacha-c commented Dec 3, 2025

Copy link
Copy Markdown
Contributor

Note: two tests will fail due to network errors (probably the original gist has been deleted). Nothing I can do on that front (they were already failing on main).

Indeed, I've address this in a separate PR a few weeks back which is awaiting review: #2522

@darkbasic

Copy link
Copy Markdown
Contributor Author

@sacha-c thanks, I've rebased so that tests can pass in CI now

@drwpow

drwpow commented Feb 8, 2026

Copy link
Copy Markdown
Contributor

Thank you! Tests look good and this seems like an improvement. Could you please add a changeset (see comment) so we can version & release? Thank you!

@darkbasic

Copy link
Copy Markdown
Contributor Author

@drwpow done

@drwpow
drwpow merged commit e3c05a4 into openapi-ts:main Feb 8, 2026
7 checks passed
This was referenced Feb 8, 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.

3 participants