Skip to content

Fix percent-encoded refs in ajv validation paths#845

Open
morgan-coded wants to merge 2 commits into
fastify:mainfrom
morgan-coded:fix/740-percent-encoded-ref-ajv
Open

Fix percent-encoded refs in ajv validation paths#845
morgan-coded wants to merge 2 commits into
fastify:mainfrom
morgan-coded:fix/740-percent-encoded-ref-ajv

Conversation

@morgan-coded

Copy link
Copy Markdown

fast-json-stringify could compile a schema with percent-encoded definition keys, then crash on the first stringify() when a validation branch went through ajv.
The serializer resolves those JSON-pointer keys literally, while ajv decodes URI fragments before lookup. This keeps the existing literal-key behavior and re-encodes % only for refs handed to ajv, including the schema clone used by standalone mode.
I tested the issue repro plus the new branch and standalone regressions. The unit suite passes with 478 tests, tstyche passes 12/12, and lint plus git diff --check are clean.
Closes #740

Copilot AI review requested due to automatic review settings June 11, 2026 00:40

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

This PR addresses schema $ref resolution issues involving percent-encoded sequences (Ajv fragment decoding vs fast-json-stringify literal key handling), and adds regression tests—covering both normal and standalone serializer generation.

Changes:

  • Added Validator.encodeRefFragment() and applied it to $ref conversion and generated validation calls.
  • Added regression tests for issue #740 cases (oneOf/anyOf/if-then-else, literal % in property names, and standalone mode).
  • Added a standalone-mode test ensuring toJSON (Date) behavior matches inline compilation for oneOf schemas.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.

File Description
test/standalone-mode.test.js Adds a regression test comparing standalone vs inline serialization for toJSON objects under oneOf.
test/issue-740.test.js New test suite covering percent-encoded schema keys and standalone mode regression for issue #740.
lib/validator.js Adds fragment-encoding helper and ensures Ajv sees refs encoded to preserve literal schema keys.
index.js Applies fragment encoding to schema refs used in generated Ajv validate() calls for oneOf and if/then/else.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread test/issue-740.test.js Outdated
const path = require('path')
const build = require('..')

process.env.TZ = 'UTC'
Comment thread test/issue-740.test.js
Comment on lines +112 to +119
const destination = path.resolve('test/fixtures', 'standalone-issue-740.js')

after(async () => {
await fs.promises.rm(destination, { force: true })
})

await fs.promises.writeFile(destination, code)
const standalone = require(destination)
Comment thread test/standalone-mode.test.js Outdated
Comment on lines +240 to +242
after(async () => {
await fs.promises.rm(destination, { force: true })
})
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.

Error using stringify on schema with uri-encoded definition property containing anyOf, allOf or oneOf

2 participants