fix(zod): handle allOf schemas with additional properties #2492
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.
Fix: Handle
allOfschemas with additional properties in zod generatorProblem
When a schema used
allOfto extend a base type while also defining additional properties at the same level, the zod generator would only process the schemas in theallOfarray and completely ignore the additional properties. This resulted in incomplete zod schemas that were missing critical fields.Example
In an OpenAPI specification, a schema like
SearchLeadPurposesListQueryResponseEntryPagingResultmight have this structure:{ "title": "PagingResult<SearchLeadPurposesListQueryResponseEntry>", "required": ["items", "meta"], "type": "object", "allOf": [ { "$ref": "#/components/schemas/PagingResult" } ], "properties": { "items": { "title": "IReadOnlyCollection<SearchLeadPurposesListQueryResponseEntry>", "type": "array", "items": { "$ref": "#/components/schemas/SearchLeadPurposesListQueryResponseEntry" } } }, "additionalProperties": false }Where
PagingResultprovides themetaproperty:{ "required": ["meta"], "type": "object", "properties": { "meta": { "$ref": "#/components/schemas/PagingResultMeta" } }, "additionalProperties": false }Before the fix
The generated zod schema only included the base schema properties (
meta) and completely ignored theitemsarray:After the fix
The generated zod schema now correctly includes both the base schema properties (
meta) AND the additional properties (itemsarray):Technical Details
The fix was implemented in
packages/zod/src/index.ts. When processingallOfschemas, the code now:allOfarraypropertiesdefined at the same levelallOfarray.and()method, which combines both schemasThis follows the OpenAPI specification where
allOfcombined withpropertiesmeans "start with all properties from allOf schemas, then also add these properties".Testing
'handles allOf with additional properties'inpackages/zod/src/zod.test.tsallOfand additional propertiesImpact
This fix affects any OpenAPI schema that uses
allOfwith additional properties, ensuring complete zod schema generation. This is particularly important for paginated responses and other common API patterns where a base type is extended.Files Changed
packages/zod/src/index.ts- Added logic to handle additional properties in allOf schemaspackages/zod/src/zod.test.ts- Added test case for allOf with additional propertiesFix #2340
Fix #2306