Skip to content

fix: update discriminator.mapping values during multi-file $ref bundling#3400

Open
littlemyx wants to merge 2 commits intohey-api:mainfrom
littlemyx:main
Open

fix: update discriminator.mapping values during multi-file $ref bundling#3400
littlemyx wants to merge 2 commits intohey-api:mainfrom
littlemyx:main

Conversation

@littlemyx
Copy link

@littlemyx littlemyx commented Feb 16, 2026

Summary

Fixes #3399

  • When @hey-api/json-schema-ref-parser bundles multi-file OpenAPI schemas, $ref objects get rewritten to internal paths (e.g. #/components/schemas/providers_Foo), but discriminator.mapping values (plain strings, not $ref objects) were left unchanged
  • This caused discriminatorValues() to fail matching and fall back to using the schema name instead of the mapping key, producing incorrect TypeScript discriminator types (e.g. type: 'providers_JetBrainsProviderConfigResponse' instead of type: 'jetbrains')
  • Two fixes in @hey-api/json-schema-ref-parser:
    • bundle.ts: post-processing step updateDiscriminatorMappings() that walks the bundled schema and rewrites stale mapping values by matching against the already-corrected $ref values in sibling oneOf/anyOf arrays
    • index.ts: in mergeMany()'s cloneAndRewrite(), also rewrite discriminator.mapping values using the same rewriteRef() logic as $ref keys

Test plan

  • All 1818 existing tests pass (0 failures)
  • TypeScript typecheck passes
  • Build passes with export checks
  • Verify with a minimal two-file OpenAPI 3.0.3 schema with oneOf + discriminator.mapping that generated types.gen.ts contains correct discriminator values from mapping keys

🤖 Generated with Claude Code

When @hey-api/json-schema-ref-parser bundles multi-file OpenAPI schemas,
$ref objects get rewritten to internal paths (e.g. #/components/schemas/providers_Foo),
but discriminator.mapping values (plain strings, not $ref objects) were left unchanged.
This caused discriminatorValues() to fail matching and fall back to using the schema
name instead of the mapping key, producing incorrect TypeScript discriminator types.

Two fixes:
- bundle.ts: add updateDiscriminatorMappings() post-processing step that walks the
  bundled schema and rewrites stale mapping values by matching against the already-
  corrected $ref values in sibling oneOf/anyOf arrays. Handles URL-encoded non-ASCII
  characters to avoid false positives on single-file schemas.
- index.ts: in mergeMany()'s cloneAndRewrite(), also rewrite discriminator.mapping
  values using the same rewriteRef() logic as $ref keys.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@bolt-new-by-stackblitz
Copy link

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

@changeset-bot
Copy link

changeset-bot bot commented Feb 16, 2026

⚠️ No Changeset found

Latest commit: 6f1903f

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 the size:L This PR changes 100-499 lines, ignoring generated files. label Feb 16, 2026
@vercel
Copy link

vercel bot commented Feb 16, 2026

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

A member of the Team first needs to authorize it.

@codecov
Copy link

codecov bot commented Feb 16, 2026

Codecov Report

❌ Patch coverage is 72.94118% with 23 lines in your changes missing coverage. Please review.
✅ Project coverage is 39.74%. Comparing base (af099f3) to head (6f1903f).

Files with missing lines Patch % Lines
packages/json-schema-ref-parser/src/index.ts 0.00% 10 Missing and 5 partials ⚠️
packages/json-schema-ref-parser/src/bundle.ts 88.57% 8 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3400      +/-   ##
==========================================
+ Coverage   39.50%   39.74%   +0.24%     
==========================================
  Files         473      473              
  Lines       17119    17204      +85     
  Branches     5217     5251      +34     
==========================================
+ Hits         6763     6838      +75     
- Misses       8301     8314      +13     
+ Partials     2055     2052       -3     
Flag Coverage Δ
unittests 39.74% <72.94%> (+0.24%) ⬆️

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 16, 2026

Open in StackBlitz

@hey-api/codegen-core

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

@hey-api/json-schema-ref-parser

npm i https://pkg.pr.new/hey-api/openapi-ts/@hey-api/json-schema-ref-parser@3400

@hey-api/nuxt

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

@hey-api/openapi-ts

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

@hey-api/shared

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

@hey-api/types

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

@hey-api/vite-plugin

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

commit: 8e9876f

- Multi-file test: verifies that discriminator.mapping values are updated
  to match rewritten $ref paths when bundling external schemas
- Single-file test: verifies that mapping values are not modified when
  all refs are already internal and correct
- Test spec files: two-file OpenAPI schema with oneOf + discriminator.mapping

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug 🔥 Broken or incorrect behavior. size:L This PR changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Discriminator mapping values ignored in multi-file schemas — uses $ref name instead of mapping key

1 participant