fix: incorrect $ref generation for schema names containing / or ~#3903
fix: incorrect $ref generation for schema names containing / or ~#3903aqeelat wants to merge 3 commits into
Conversation
|
|
|
@aqeelat is attempting to deploy a commit to the Hey API Team on Vercel. A member of the Team first needs to authorize it. |
|
There was a problem hiding this comment.
No new issues found in the changed scope.
TL;DR — Routes the four parser-emitted $ref constructions through pathToJsonPointer, restoring RFC 6901 encoding for schema/parameter/requestBody names that contain ~ or /. The fix is correct and matches the encoding already performed by buildGraph.
Key changes
- Encode parser
$refsegments — replaces raw`#/components/.../${name}`interpolations inparseV3_1_X,parseV3_0_X, andparseV2_0_XwithpathToJsonPointer([...segments, name]). - Adds RFC 6901 tests — covers
~,/, and combined cases inpathToJsonPointer.
Summary | 4 files | 1 commit | base: main ← fix/3888-json-pointer-encoding
Consistency with the graph layer
Before: parser emitted
#/components/schemas/node/type~special(4 decoded segments), whilebuildGraphkeyed the same node as#/components/schemas/node~1type~0special(3 segments) viaencodeJsonPointerSegment.
After: both sides agree, so downstream lookups (graph.nodes.get(pointer),resolveRef,jsonPointerToPath) round-trip correctly for schema names containing~or/.
The four call sites cover every top-level $ref produced by the parsers themselves: components.parameters, components.requestBodies, components.schemas (3.x) and definitions (2.0). Securitysschemes uses name as a map key, not a $ref, so it stays as is.
parser/index.ts (3.1.x) · parser/index.ts (3.0.x) · parser/index.ts (2.0.x) · ref.test.ts
Out-of-scope follow-up
Note:
packages/shared/src/openApi/shared/transforms/readWrite.tsstill builds pointer strings via raw interpolation in several places —originals[\#/components/schemas/${name}`],originals[`#/definitions/${name}`], and the${schemasPointerNamespace}${name}joins insplitSchemas/splitDiscriminatorSchemas`.
For schemas whose names contain ~ or /, those keys won't match graph.nodes (which is already encoded) and the readWrite split can silently leave originals behind or fail to rewrite $refs. The internal lookups in that file are mutually consistent (un-encoded throughout), so existing tests don't catch it. Worth a separate PR — out of scope here.
Claude Opus | 𝕏
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #3903 +/- ##
==========================================
+ Coverage 37.73% 38.73% +1.00%
==========================================
Files 582 593 +11
Lines 20837 21327 +490
Branches 6066 6253 +187
==========================================
+ Hits 7862 8262 +400
- Misses 10567 10667 +100
+ Partials 2408 2398 -10
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
@hey-api/codegen-core
@hey-api/json-schema-ref-parser
@hey-api/nuxt
@hey-api/openapi-ts
@hey-api/shared
@hey-api/spec-types
@hey-api/types
@hey-api/vite-plugin
commit: |
Replace string interpolation with pathToJsonPointer() when constructing JSON Pointers in OpenAPI parsers. This ensures ~ and / in schema names are properly encoded (~0, ~1) per RFC 6901. Fixes hey-api#3888
Add integration tests exercising parseV3_1_X, parseV3_0_X, and parseV2_0_X with schema/parameter/requestBody names containing RFC 6901 special characters.
47ceb12 to
7591338
Compare

Fix
Replace string interpolation with
pathToJsonPointer()when constructing$refJSON Pointers in OpenAPI parsers. This ensures~and/in schema/component names are properly encoded (~0,~1) per RFC 6901.Problem
Schema names containing
/or~produce malformed JSON Pointers. For example, a schema namednode/type~specialgenerated#/components/schemas/node/type~special, which decodes to 4 segments instead of the correct 3 segments["components", "schemas", "node/type~special"].Changes
packages/shared/src/openApi/3.1.x/parser/index.ts— 3 interpolations replacedpackages/shared/src/openApi/3.0.x/parser/index.ts— 3 interpolations replacedpackages/shared/src/openApi/2.0.x/parser/index.ts— 1 interpolation replacedpackages/shared/src/utils/__tests__/ref.test.ts— 3 new test cases for RFC 6901 encodingFixes #3888