Support OpenAPI 3.1 nullable union - #77
Merged
Merged
Conversation
Changepacks@devup-api/generator@0.1.25 → 0.1.26 - packages/generator/package.jsonPatch
@devup-api/next-plugin@0.1.14 → 0.1.15 - packages/next-plugin/package.jsonPatch
@devup-api/rsbuild-plugin@0.1.14 → 0.1.15 - packages/rsbuild-plugin/package.jsonPatch
@devup-api/vite-plugin@0.1.14 → 0.1.15 - packages/vite-plugin/package.jsonPatch
@devup-api/webpack-plugin@0.1.14 → 0.1.15 - packages/webpack-plugin/package.jsonPatch
|
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.
Problem
OpenAPI 3.1 expresses a nullable
$refas a union with the JSON Schemanulltype:{ "anyOf": [{ "$ref": "#/components/schemas/Item" }, { "type": "null" }] }The generator treated
anyOf/oneOfas a plain union, so{ "type": "null" }wasconverted to
unknownand the$reflost its component identity:({ id?: string } | unknown)DevupObject<'request', 'openapi.json'>['Item'](DevupObject<...>['Item'] | unknown)DevupObject<'response', 'openapi.json'>['Item']z.union([...])z.lazy(() => _Item)createItem: openapi_json_request_Item(T | unknown)collapses tounknownin TypeScript, which is the reportedTS2322: Type 'unknown' is not assignable to .... On the zod side the result became aZodUnion, which has no.unwrap().Fix
anyOf: [S, { type: "null" }]is 3.1's nullable notation, not a union. Two helpers inopenapi-utils.tsnormalize it to the OpenAPI 3.0 equivalent{ ...S, nullable: true }before any union handling, so the existing nullable machinery applies unchanged:
isNullTypeSchema()— detects a{ "type": "null" }membersplitNullableUnion()— splits union members from the nullability they contributenormalizeNullableUnion()— collapses a single-member null union to the 3.0 shape,preserving sibling keywords (
description,default) and returning the inputunchanged when there is nothing to collapse
Applied on all three paths:
generate-schema.ts—getTypeFromSchema()union branchgenerate-zod.ts—schemaToZod(),schemaToZodType(), and the request-body schemaname lookup that fed
postPathSchemasgenerate-interface.ts— response content and request-body branches, so the componentreference shortcut and the raw-multipart check see the normalized schema
Unions that keep two or more members stay unions and now additionally reflect a removed
nullmember, soanyOf: [string, number, null]generates(string | number | null)instead of
(string | number | unknown).Not regressed
anyOf: [{type:"string"},{type:"number"}]still generates(string | number)/z.union([...])/z.ZodUnion<...>.nullable: trueinput keeps working.type: ["string","null"]arrays were already handled bygetPrimaryType()/isNullableSchema()and are unchanged.Tests
New
nullable-union.test.tsasserts that every 3.1 nullable form generates abyte-identical result to its 3.0 counterpart across
generateInterface,generateZodSchemas, andgenerateZodTypeDeclarations, in both a request-body and acomponent-property position. Verified TDD: 14 of the new tests fail without the fix and
all 31 pass with it, while the genuine-union guards pass in both states.
bun test1148 pass / 0 fail, 100% line and function coverage on all four changed files,bun run lintandbun run buildclean.