fix: resolve percent-encoded $ref definition keys#846
Open
DucMinhNe wants to merge 1 commit into
Open
Conversation
Ajv resolves a $ref's JSON pointer by url-decoding each segment, so a definition whose key is itself percent-encoded (e.g. Some%3Cloremipsum%3E) could no longer be found once the referenced schema contained oneOf/anyOf/allOf and Ajv compiled a sub-validator for it: the pointer #/definitions/Some%3Cloremipsum%3E decodes to Some<loremipsum>, which does not match the literal key, throwing MissingRefError. Re-encode the percent signs in the ref fragment before handing it to Ajv so its single decode round-trips back to the original key. The internal json-schema-ref-resolver (which performs a literal lookup) is untouched. Fixes fastify#740
There was a problem hiding this comment.
Pull request overview
This PR fixes a $ref resolution failure when a schema definition key is itself percent-encoded (e.g. Some%3Cloremipsum%3E) and Ajv is invoked to compile/validate oneOf/anyOf/allOf branches (issue #740). It does so by escaping % in the $ref fragment before Ajv processes it, so Ajv’s single decode round-trips back to the original literal key.
Changes:
- Add an
escapeRefForAjvhelper and apply it to schema refs passed intoValidator.validate(). - Escape
$refstrings during Ajv schema conversion (convertSchemaToAjvFormat). - Add a regression test covering a percent-encoded definition key referenced from a schema that uses
oneOf.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| lib/validator.js | Escapes % in $ref fragments for Ajv validation and during Ajv schema conversion to avoid percent-decoding mismatches. |
| test/ref.test.js | Adds a regression test reproducing issue #740 with a percent-encoded definition key and oneOf. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+74
to
+76
| if (typeof schema.$ref === 'string') { | ||
| schema.$ref = escapeRefForAjv(schema.$ref) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #740
Problem
When a definition key is itself percent-encoded (e.g.
Some%3Cloremipsum%3E) and the referenced subschema containsoneOf/anyOf/allOf, building a serializer throws:Such schemas need Ajv to compile a sub-validator for the
oneOf/anyOf/allOfbranch. Ajv resolves a$refJSON pointer by url-decoding each segment, so#/definitions/Some%3Cloremipsum%3Edecodes toSome<loremipsum>, which does not match the literal definition keySome%3Cloremipsum%3E— henceMissingRefError. Plain refs (nooneOf/anyOf/allOf) avoided this only because they were resolved by the internal literal-lookup resolver instead of Ajv.Fix
In
lib/validator.js, re-encode the%in the ref fragment before handing it to Ajv (invalidate()and when converting a schema's$refinconvertSchemaToAjvFormat). Ajv's single decode then round-trips back to the original literal key. The internaljson-schema-ref-resolverused by the serializer (which performs a literal lookup) is untouched, so both resolvers stay correct.The fix is ~12 lines and adds a small
escapeRefForAjvhelper.Test
Added a unit test in
test/ref.test.js(node:test, matching the existing file style) that ports the repro from the issue: a schema with a percent-encoded$refdefinition key whose subschema usesoneOfnow serializes correctly.Full suite is green:
npm test→ 473 unit tests + tstyche type tests pass, lint clean.Note
Standalone mode (
mode: 'standalone') carries the same escaped refs throughrestoreFromState, so it benefits from this fix as well; any remaining standalone-specific edge cases are pre-existing and out of scope for this change.