Skip to content

[BUG][Go] enumUnknownDefaultCase generates colliding UNKNOWN_DEFAULT_OPEN_API constants when enumClassPrefix=false #23844

@TyphonHill

Description

@TyphonHill

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
Description

When using enumUnknownDefaultCase=true with the Go generator and enumClassPrefix=false (the default), every generated enum type emits a bare UNKNOWN_DEFAULT_OPEN_API constant. Since all enum models live in the same Go package, this causes a compilation error:

UNKNOWN_DEFAULT_OPEN_API redeclared in this block
        model_status_a.go:XX: other declaration of UNKNOWN_DEFAULT_OPEN_API

The sentinel constant is added as an enumVar and rendered through the same template path as regular constants:

{{#enumClassPrefix}}{{{classname.toUpperCase}}}_{{/enumClassPrefix}}{{name}}

When enumClassPrefix=false, no prefix is applied, so every enum gets an identical UNKNOWN_DEFAULT_OPEN_API constant.

Setting enumClassPrefix=true fixes the collision but renames all existing enum constants (e.g. ProvisionHalted becomes MODELPROVISIONSTAGEVALUE_ProvisionHalted), which is a breaking change for consumers.

openapi-generator version

7.22.0 (not a regression — this is the first release with Go support for enumUnknownDefaultCase, introduced by PR #23417)

OpenAPI declaration file content or url
openapi: 3.0.1
info:
  version: 1.0.0
  title: Test API
paths: {}
components:
  schemas:
    StatusA:
      type: string
      enum:
        - active
        - inactive
    StatusB:
      type: string
      enum:
        - pending
        - completed
Generation Details
openapi-generator-cli generate \
  -i spec.yaml \
  -g go \
  --additional-properties enumUnknownDefaultCase=true \
  --package-name myapi
Steps to reproduce
  1. Generate a Go client with the above spec and enumUnknownDefaultCase=true (without enumClassPrefix=true)
  2. Attempt to compile the generated code: go build ./...
  3. Observe: UNKNOWN_DEFAULT_OPEN_API redeclared in this block
Related issues/PRs
Suggest a fix

The sentinel constant should be unique per enum type when enumClassPrefix=false. The template could unconditionally prefix the sentinel with the classname, e.g.:

{{{classname}}}UnknownDefaultOpenApi {{{classname}}} = "unknown_default_open_api"

This would generate StatusAUnknownDefaultOpenApi, StatusBUnknownDefaultOpenApi, etc. — unique names without requiring enumClassPrefix=true and without renaming existing constants.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions