Repository navigation
Conversation
OpenAPI 3.1 writes a nullable enum as type: [T, "null"] and/or with null in enum. EnumSchemaParser stripped the null value and only kept 3.0 nullable: true, so these enums lost their null. Treat null in type or enum as nullable, include null in union-style enum types, and add null to a property that references a nullable enum through $ref when the enum style cannot hold null (enum, const). Fixes acacode#1847
🦋 Changeset detectedLatest commit: 8e6a88f The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
There was a problem hiding this comment.
All reported issues were addressed across 8 files
Reply with feedback, questions, or to request a fix.
View guided diff | Re-trigger cubic
Move the nullable enum $ref check into the primitive parser so array items, union branches and record values get `| null` too, not only object properties. Read the raw component when it is not parsed yet. Keep null for nullable array-valued enums. Split the OpenAPI 3.0 cases into their own fixture.
There was a problem hiding this comment.
All reported issues were addressed across 9 files (changes from recent commits).
Reply with feedback, questions, or to request a fix.
View guided diff | Re-trigger cubic
This branch has not been deployed
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.
Fixes #1847
Problem
OpenAPI 3.1 removed
nullable: true. A nullable enum is written astype: ["string", "null"]and/or withnullinenum. The generator dropped thatnull:came out as
status?: "active" | "pending",level?: 1 | 2andcolor?: Colorin every enum style, so code checking fornulldid not type-check.EnumSchemaParser.parse()filtersnullout ofschema.enum, and the formatters only addnullback when the 3.0nullableflag is set.A 3.0
nullable: trueenum had a related gap: inline it gets| null, but a property that references it through$refstayscolor?: Color.Solution
EnumSchemaParsersetsnullableon the parsed enum when the schema hasnullable: true,"null"intype, ornullinenum(newSchemaUtils.hasNullTypeOrValue). The check runs before the existing filter removesnull. Inline enums then get| nullthrough the existing inline formatter, the same way 3.0 ones do.enumStyle: "union", the generated type includesnull:export type Color = "red" | "green" | null.enumandconststyles, a TypeScript enum or const object cannot holdnull, so a$refto a nullable enum is typedColor | nullwherever it is used: object properties, array items ((Color | null)[]), oneOf branches and record values. This lives inPrimitiveSchemaParser, where a$refbecomes a type name, viaSchemaUtils.isNullableEnumRef. It reads the component's parsed data, or its raw schema when the component is not parsed yet. Withunionthe type already containsnull, so nothing is added there.enum: [["a", "b"], ["c", "d"], null]) keeps itsnulltoo.Enums without
nullare unchanged. No existing snapshot changes.Verification
tests/spec/nullable-enumwith a 3.1 fixture (type+enumnull,type-only null,enum-only null, array-valued enum,$refin properties / array items / oneOf / records, a component declared after its user, non-nullable enums) and a separate 3.0 fixture (nullable: true), run withenumStyleenum,unionandconst: snapshots plus explicit checks. 8 of the 9 tests fail onmainand all pass here.bun run test: 318 tests pass.bun run format:checkandbun run buildpass.bun run lintreports the same 4 errors and 60 warnings as onmain(none in changed lines).Summary by cubic
Keeps
nullin OpenAPI 3.1 nullable enums so generated types match the schema. Previously, nullable enums declared withtype: ["string", "null"]ornullinenumlost the null, and a$refto a 3.0nullable: trueenum also dropped null.Fixes #1847.
nullintypeorenumas nullable, matching 3.0nullable: truebehavior.null("red" | "green" | null).enumandconststyles, every reference to a nullable enum—object properties, array items, union branches, and record values—is typedColor | nullbecause a TypeScript enum or const object can't holdnull.$refuses across all enum styles, including enum-only nullable enums and components referenced before they're parsed.Written for commit 8e6a88f. Summary will update on new commits.