Don't emit enum constants for array-typed schemas#2424
Merged
Conversation
A schema that combines `type: array` with an `enum` is malformed: the enum members are scalars, not arrays, so no array value could ever satisfy the constraint. In the wild this shows up as the item enum being copied onto the array (e.g. Jellyfin's `TranscodingInfo.TranscodeReasons` in issue oapi-codegen#2176). oapi-codegen took the enum branch purely on `len(schema.Enum) > 0`, emitting typed constants while the Go type resolved from `type: array` to a slice. That produced `const X SliceType = ...`, which is not a valid Go constant type: type TranscodingInfoTranscodeReasons []TranscodeReason const TranscodingInfoTranscodeReasonsAnamorphicVideoNotSupported \ TranscodingInfoTranscodeReasons = ... // invalid constant type Guard the enum branch with `!t.Is("array")` so array-typed schemas fall through to normal array handling and generate as a plain `[]ItemType`. The meaningful enum lives on the item schema, which still gets its own constants. No existing fixtures change. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Contributor
Greptile SummaryThis PR changes enum handling for array-typed schemas in code generation. The main changes are:
Confidence Score: 5/5This looks safe to merge.
|
| Filename | Overview |
|---|---|
| pkg/codegen/schema.go | Updates GenerateGoSchema so array-typed enum or const schemas generate as arrays instead of invalid typed constants. |
Reviews (1): Last reviewed commit: "Don't emit enum constants for array-type..." | Re-trigger Greptile
lwc
pushed a commit
to lwc/oapi-codegen
that referenced
this pull request
Jun 23, 2026
A schema that combines `type: array` with an `enum` is malformed: the enum members are scalars, not arrays, so no array value could ever satisfy the constraint. In the wild this shows up as the item enum being copied onto the array (e.g. Jellyfin's `TranscodingInfo.TranscodeReasons` in issue oapi-codegen#2176). oapi-codegen took the enum branch purely on `len(schema.Enum) > 0`, emitting typed constants while the Go type resolved from `type: array` to a slice. That produced `const X SliceType = ...`, which is not a valid Go constant type: type TranscodingInfoTranscodeReasons []TranscodeReason const TranscodingInfoTranscodeReasonsAnamorphicVideoNotSupported \ TranscodingInfoTranscodeReasons = ... // invalid constant type Guard the enum branch with `!t.Is("array")` so array-typed schemas fall through to normal array handling and generate as a plain `[]ItemType`. The meaningful enum lives on the item schema, which still gets its own constants. No existing fixtures change. Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
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.
Closes: #2176
A schema that combines
type: arraywith anenumis malformed: the enum members are scalars, not arrays, so no array value could ever satisfy the constraint. In the wild this shows up as the item enum being copied onto the array.oapi-codegen took the enum branch purely on
len(schema.Enum) > 0, emitting typed constants while the Go type resolved fromtype: arrayto a slice. That producedconst X SliceType = ..., which is not a valid Go constant type:Guard the enum branch with
!t.Is("array")so array-typed schemas fall through to normal array handling and generate as a plain[]ItemType. The meaningful enum lives on the item schema, which still gets its own constants. No existing fixtures change.