Skip to content

fix: escape property names in the schema refs the generated code validates against - #882

Open
nigrosimone wants to merge 2 commits into
fastify:mainfrom
nigrosimone:fix-property-name-escaping
Open

nigrosimone wants to merge 2 commits into
fastify:mainfrom
nigrosimone:fix-property-name-escaping

Conversation

@nigrosimone

@nigrosimone nigrosimone commented Sep 13, 2026

Copy link
Copy Markdown
Contributor

A property whose subschema has anyOf, oneOf or if is validated at run time through ajv, by a schema ref the generated code carries as a string literal: validator.validate("#/properties/<name>/oneOf/0", ...). The name goes in unescaped, so a property named with a newline, a quote or a backslash makes the build throw Invalid or unexpected token, and the same ref is used unescaped in the TypeError template of a mismatch. The JSON pointer is not escaped either, so a name with a / or a ~ points at nothing and ajv answers no schema with key or ref. The ref also sits on a comment line above the function of a nested object or array, where a newline or a U+2028 in the name ends the comment and the rest of the name becomes code.

The fix embeds the ref and the message with JSON.stringify, escapes the pointer segments as RFC 6901 says, and escapes the line terminators on the comment lines.

The added test builds each of the three keywords with such names, and a nested object under a name with a line terminator: 24 of its 39 cases fail on main, all pass here.

@nigrosimone
nigrosimone marked this pull request as ready for review September 13, 2026 11:30
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.

1 participant