Skip to content

fix: preserve literal data when converting schemas for Ajv - #902

Draft
liuyinchu wants to merge 1 commit into
fastify:mainfrom
liuyinchu:fix/preserve-validator-literal-data
Draft

liuyinchu wants to merge 1 commit into
fastify:mainfrom
liuyinchu:fix/preserve-validator-literal-data

Conversation

@liuyinchu

Copy link
Copy Markdown

Problem

convertSchemaToAjvFormat previously performed a blind recursive traversal over all object properties. When literal data keywords (such as const, enum, default, examples, or custom annotations) contained objects with a type property (e.g., { type: 'string' }), the converter incorrectly treated them as subschemas, modifying their structure and corrupting the literal data.

Solution

Replaced naive recursion with explicit traversal of subschema containers:

  • Defined Explicit Schema Keyword Sets: Categorized standard subschema keywords into single (schemaKeywords), array (schemaArrayKeywords), and map structures (schemaMapKeywords).
  • Preserved Literal Data: Restricted recursive traversal strictly to subschema containers, leaving data inside const, enum, default, examples, and unknown annotation keywords untouched.

Verification

Added regression tests in test/validator-schema-traversal.test.js:

  • Verified that schema-like objects inside const and enum within anyOf/oneOf are preserved.
  • Ensured data in default, examples, and custom x- annotations remain unmodified.
  • Confirmed schema conversion continues to function correctly for single, array, and map subschemas, as well as property-dependency keywords.

AI assistance

The implementation and regression tests were prepared with AI assistance and reviewed by me.

Checklist

Traverse schema-valued keywords and subschema containers explicitly so
const, enum, annotations and property-dependency data remain unchanged.
Add regression coverage for union selection and supported schema shapes.

Prepared with AI assistance for human review.

This branch has not been deployed

No deployments
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