Repository navigation
fix: skip null members of non-string enums - #2654
Merged
Merged
Conversation
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
Contributor
[Medium impact] The PR appears safe to merge. SummaryThe PR skips null members when generating constants for non-string enums.
No actionable issues were found. Reviews (2) · Last reviewed commit: "Merge remote-tracking branch 'upstream/m..." · Reviewed by Greptile |
mromaszewicz
approved these changes
Nov 9, 2026
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 #2636
A nullable enum lists
nullamong its values (nullable: truein 3.0,type: [T, "null"]in 3.1). The enum branch ofgenerateGoSchemaformats every member with%v, sonullbecomes the value<nil>. When the enum's Go type is notstring, values are written unquoted, so generation fails. This hits theoneOffrom the rootly-go spec in #2636 as well as a plain{type: integer, nullable: true, enum: [1, 2, null]}:For non-string enums this now skips null when building the constants, along with the matching
x-enum-varnames/x-enumNamesentry, since those line up with the enum by index. The pointer or nullable field already carries null. The #2636 schema generatestype AutomationMaxCreditsPerRun2 float32with no constants,[1, 2, null]givesN1andN2, and a nullable boolean[true, null]givesTrue. Collapsing same-typeoneOfbranches, 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) andgo mod tidy -diffin the root,internal/testandexamplesmodules;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-enumNamesstay aligned with null first or in the middle; specs with one and with two nullable string enums (also withold-enum-conflicts) generate byte-identical output before and after.