Skip to content

Don't emit enum constants for array-typed schemas#2424

Merged
mromaszewicz merged 1 commit into
oapi-codegen:mainfrom
mromaszewicz:fix/issue-2176
Jun 21, 2026
Merged

Don't emit enum constants for array-typed schemas#2424
mromaszewicz merged 1 commit into
oapi-codegen:mainfrom
mromaszewicz:fix/issue-2176

Conversation

@mromaszewicz

@mromaszewicz mromaszewicz commented Jun 21, 2026

Copy link
Copy Markdown
Member

Closes: #2176

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.

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.

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>
@mromaszewicz
mromaszewicz requested a review from a team as a code owner June 21, 2026 05:04
@mromaszewicz mromaszewicz added the bug Something isn't working label Jun 21, 2026
@greptile-apps

greptile-apps Bot commented Jun 21, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR changes enum handling for array-typed schemas in code generation. The main changes are:

  • Array schemas with enum or OpenAPI 3.1 const now skip scalar enum constant generation.
  • Those schemas fall through to normal array type generation.
  • Item schemas still carry their own enum constants when the enum is defined on the array items.

Confidence Score: 5/5

This looks safe to merge.

  • No blocking issues found in the changed code.

Important Files Changed

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

@mromaszewicz
mromaszewicz merged commit bb192a2 into oapi-codegen:main Jun 21, 2026
15 checks passed
@mromaszewicz
mromaszewicz deleted the fix/issue-2176 branch June 21, 2026 05:06
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>
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.

Unable to generate from https://api.jellyfin.org/openapi/jellyfin-openapi-stable.json

1 participant