fix(zod): preserve null type when .nullish() is applied to named schema reference#145
Open
daniel-rose wants to merge 3 commits intotazo90:mainfrom
Open
fix(zod): preserve null type when .nullish() is applied to named schema reference#145daniel-rose wants to merge 3 commits intotazo90:mainfrom
daniel-rose wants to merge 3 commits intotazo90:mainfrom
Conversation
…ma reference
When a field references a named Zod schema and .nullable() or .nullish() is
called, the generator was wrapping the $ref in allOf but silently dropping the
null type. The fix transforms the output to anyOf: [{ $ref }, { type: 'null' }],
the correct OpenAPI 3.1 representation. The version processor is also fixed to
carry outer schema metadata (e.g. description) when downgrading anyOf to
nullable: true for OpenAPI 3.0.
Closes tazo90#142
34b443e to
f614b77
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Description
When applying
.nullish()or.nullable()to a field that references a named schema (producing a$ref), the generator wrapped the reference inallOf: [{ $ref }]but silently dropped the null type. The field was not nullable in the generated spec.The fix transforms the output to
anyOf: [{ $ref }, { type: 'null' }], which is the correct OpenAPI 3.1 representation. The existing version processor handles 3.0 downgrade automatically (and is now also fixed to preserve outer schema metadata likedescriptionduring that downgrade).Closes #142
Type of Change
Changes
packages/openapi-core/src/schema/zod/zod-converter.ts: Two locations fixed:SchemaIdentifier.nullable()/SchemaIdentifier.nullish()path (line ~1072): now producesanyOf: [{ $ref }, { type: 'null' }]instead of bareallOf: [{ $ref }]processZodChainnullable/nullish case (line ~1770): whenschema.allOfis set, transforms toanyOfwith null branch instead of silently skippingpackages/openapi-core/src/openapi/version-processor.ts:downgradeSchemaForOpenApi30now preserves outer metadata (e.g.description) when collapsinganyOf: [{ $ref }, { type: 'null' }]to{ $ref, nullable: true }tests/integration/regressions/zod-nullability.test.ts: Two regression tests added for.nullish()and.nullable()on named schema referencesChecklist
pnpm checkpassespnpm test:unit,pnpm test:integrationandpnpm build)