Features/2332 zod circular references #2502
Draft
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.
Add Support for Circular References in Zod Schemas
Problem
When OpenAPI schemas contain circular or self-referencing structures (e.g., a
Nodeschema that references itself via achildproperty), Orval was generating incorrect Zod validation schemas. Instead of usingzod.lazy()to handle the circular reference properly, it would generatezod.unknown()orzod.any(), making the validation schemas non-functional.Example Issue:
Before (Incorrect):
After (Correct):
Solution
Implemented comprehensive support for circular references by:
Detection: Added
__circular__and__refName__markers to identify circular references during schema dereferencing in thedeferencefunction.Processing: Modified
generateZodValidationSchemaDefinitionto handle circular reference markers and generate['circularRef', refName]function definitions.Code Generation: Updated
parseZodValidationSchemaDefinitionto:#/components/schemas/Node→Node)zod.lazy(() => SchemaName).optional()and.nullable()after the lazy wrapperArray Support: Extended the array parsing logic to handle circular references within arrays, generating
zod.array(zod.lazy(() => SchemaName)).Schema Export: Implemented automatic detection and export of schemas referenced in circular dependencies, ensuring the referenced schemas are available in the generated code.
Self-Reference Handling: Enhanced the
deferencefunction to detect self-references within a schema being generated, ensuring proper circular reference handling even when a schema references itself.Changes
packages/zod/src/index.ts:deference()to return circular reference markersgenerateZodValidationSchemaDefinition()to detect and handle circular referencesparseZodValidationSchemaDefinition()to generatezod.lazy()wrapperspackages/zod/src/zod.test.ts:Testing
Verified with real-world scenarios:
Examples
Simple Circular Reference:
Generates:
manager: zod.lazy(() => User).optional()Circular Reference in Array:
Generates:
children: zod.array(zod.lazy(() => File)).optional()Complex Scenario (User Hierarchy):
Generates both the endpoint schemas and exports the
Userschema definition.Closes
Closes #2332