Skip to content

fix: skip null members of non-string enums - #2654

Merged
mromaszewicz merged 2 commits into
oapi-codegen:mainfrom
nk-batman21:fix/null-enum-member
Nov 9, 2026
Merged

mromaszewicz merged 2 commits into
oapi-codegen:mainfrom
nk-batman21:fix/null-enum-member

Conversation

@nk-batman21

Copy link
Copy Markdown
Contributor

Fixes #2636

A nullable enum lists null among its values (nullable: true in 3.0, type: [T, "null"] in 3.1). The enum branch of generateGoSchema formats every member with %v, so null becomes the value <nil>. When the enum's Go type is not string, values are written unquoted, so generation fails. This hits the oneOf from the rootly-go spec in #2636 as well as a plain {type: integer, nullable: true, enum: [1, 2, null]}:

error generating code for type enums: enum value "<nil>" of AutomationMaxCreditsPerRun2 is not a number or boolean literal, which its Go type float32 requires

For non-string enums this now skips null when building the constants, along with the matching x-enum-varnames / x-enumNames entry, since those line up with the enum by index. The pointer or nullable field already carries null. The #2636 schema generates type AutomationMaxCreditsPerRun2 float32 with no constants, [1, 2, null] gives N1 and N2, and a nullable boolean [true, null] gives True. Collapsing same-type oneOf branches, as discussed in the issue, would still need this for a plain nullable enum.

String enums are left alone. They still get a "<nil>" constant, but that compiles, and dropping it would rename constants people already use: with two nullable string enums in one spec, the shared "<nil>" value is what prefixes them (StatusActive, ColorRed). So the only specs whose output changes are ones that fail to generate today.

Ran: go test -cover ./..., go generate ./... (no changes to generated files) and go mod tidy -diff in the root, internal/test and examples modules; golangci-lint run ./... (v2.15.0).
Checked: generated and compiled the #2636 schema (default merge, v1 and v3) and 3.0/3.1 nullable integer, number and boolean enums; x-enum-varnames / x-enumNames stay aligned with null first or in the middle; specs with one and with two nullable string enums (also with old-enum-conflicts) generate byte-identical output before and after.

A nullable enum lists null among its values, as `nullable: true` in
OpenAPI 3.0 and `type: [T, "null"]` in 3.1 expect. The enum branch of
generateGoSchema formatted every member with %v, so null became the
value "<nil>". An enum whose Go type is not string has its values
emitted unquoted, so generation failed with 'enum value "<nil>" ... is
not a number or boolean literal'.

For non-string enums, skip null members when building the constants,
together with their x-enum-varnames / x-enumNames entry, which lines up
with the enum by index. The pointer or nullable field already carries
null. String enums keep their current output, so existing constant
names do not change.

Fixes oapi-codegen#2636
@nk-batman21
nk-batman21 requested a review from a team as a code owner November 1, 2026 19:20
@greptile-apps

greptile-apps Bot commented Nov 1, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge.

Summary

The PR skips null members when generating constants for non-string enums.

  • Nullable non-string enums generate constants only for their non-null values.

No actionable issues were found.

Reviews (2) · Last reviewed commit: "Merge remote-tracking branch 'upstream/m..." · Reviewed by Greptile

@mromaszewicz mromaszewicz added the bug Something isn't working label Nov 9, 2026
@mromaszewicz
mromaszewicz enabled auto-merge (squash) November 9, 2026 19:07
@mromaszewicz
mromaszewicz merged commit 72ca3cf into oapi-codegen:main Nov 9, 2026
21 of 23 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

A float32 in a oneOf with a nullable type can lead to syntax errors

2 participants