Sanitize the definition token in nested JsonSchema references - #6936
Sanitize the definition token in nested JsonSchema references#6936fubhy wants to merge 1 commit into
Conversation
|
There was a problem hiding this comment.
Important
The new regression test is correct and currently failing because the implementation fix it exercises is not yet present on this branch. Merge as-is will break CI.
Reviewed changes
- Reviewed the focused regression test added to
packages/effect/test/JsonSchema.test.ts. - Confirmed the test reproduces the bug described in the PR by running the targeted Vitest filter; it fails as expected with the existing source.
⚠️ Implementation fix missing
The diff adds the regression test, but packages/effect/src/JsonSchema.ts still contains the original bug at lines 678–689. The rewrite helper looks up tokens[tokens.length - 1], so for #/$defs/A$B/properties/value it sanitizes the trailing value token instead of the definition token A$B, producing #/components/schemas/A$B/properties/value.
Merging this branch leaves CI red. The source needs to sanitize the token immediately following $defs in the JSON Pointer, preserving the rest of the path.
Technical details
# Fix nested `$defs` reference sanitization
## Affected sites
- `packages/effect/src/JsonSchema.ts:678-689` — `rewrite` sanitizes the last pointer token (`tokens[tokens.length - 1]`) instead of the definition token.
## Required outcome
- For every `#/$defs/<definition>/...` reference, the `<definition>` token must be rewritten to its sanitized OpenAPI component key while the remaining pointer path is preserved.
- The added test `sanitizes the definition token in nested references` must pass.
## Suggested approach
- Split the `$ref`, locate the `$defs` segment, and sanitize the next token using the existing `keyMap`.
- Reconstruct the `$ref` and then apply the existing `#/components/schemas` replacement.
Example for `#/$defs/A$B/properties/value`:
- tokens = `["#", "$defs", "A$B", "properties", "value"]`
- sanitize `"A$B"` → `"A_B"`
- emit `#/components/schemas/A_B/properties/value`ℹ️ Nitpicks
- The new assertion uses
assert.deepStrictEqualfrom@effect/vitest, but the file already importsdeepStrictEqualfrom@effect/vitest/utils. Prefer the existing import for consistency.
@v0 or keep the SHA fresh with Dependabot | Fix all ➔ | Fix 👍s ➔ | View workflow run | Using Kimi K2 (free via Pullfrog for OSS) | 𝕏
| @@ -1,4 +1,4 @@ | |||
| import { describe, it } from "@effect/vitest" | |||
| import { assert, describe, it } from "@effect/vitest" | |||
There was a problem hiding this comment.
This imports assert only for the new assert.deepStrictEqual call. The file already imports deepStrictEqual from @effect/vitest/utils, so prefer that existing utility for consistency.

Summary
A nested $defs reference can retain an invalid unsanitized definition name while the component is stored under a sanitized key, producing a dangling OpenAPI reference.
Important
This PR starts with focused failing reproduction tests. Add the implementation fix to this same branch; CI is expected to fail until that fix is included.
Nested definition references sanitize the wrong token
Module:
JsonSchemaAudit ID:
core-g-r-jsonschema-nested-reference-wrong-tokenSeverity / confidence: medium / high
What happens
A nested $defs reference can retain an invalid unsanitized definition name while the component is stored under a sanitized key, producing a dangling OpenAPI reference.
Why it happens
The rewrite sanitizes tokens[tokens.length - 1], which is the final nested path token rather than the definition token. For #/$defs/A$B/properties/value it checks value, emits A$B in the reference, and stores the component as A_B.
Expected behavior
Every #/$defs//... reference is rewritten under the sanitized OpenAPI component key while preserving the remaining pointer path.
Relevant implementation
These links and excerpts are pinned to audit base
c9b56ab507f224426ee8388dc450da447ec4715f.packages/effect/src/JsonSchema.ts:678-689View problematic code at
packages/effect/src/JsonSchema.ts:678-689View exact lines on GitHub
Reproduction
pnpm vitest run packages/effect/test/JsonSchema.test.ts -t "sanitizes the definition token in nested references"Observed failure: The intended failure was reproduced: the emitted reference retained A$B instead of A_B.
Implementation handoff
The initial reproduction tests on this branch are the regression specification for the implementation fix that should follow in this PR.
pnpm vitest run packages/effect/test/JsonSchema.test.ts -t "sanitizes the definition token in nested references"Audit provenance
c9b56ab507f224426ee8388dc450da447ec4715fc9b56ab507f224426ee8388dc450da447ec4715fcore-g-r-jsonschema-nested-reference-wrong-token