Openapi-generator: [Go] Duplicate constant names for enums with same values

Created on 11 Jul 2018  路  5Comments  路  Source: OpenAPITools/openapi-generator

Description

I've defined enums that contain same values (see below)
The openapi-generator creates constants with the same names.
But this is not allowed and the code does not compile anymore.

I get an error like this:

previous declaration at ../go-client/model_color_primaries.go:18:28
../go-client/model_color_space.go:20:26: SMPTE170_M redeclared in this block

used openapi-generator version: 3.0.3

OpenAPI declaration file content or url
ColorSpace:
  title: ColorSpace
  enum:
  - UNSPECIFIED
  - SMPTE170M
  - SMPTE240M

ColorPrimaries:
  title: ColorPrimaries
  enum:
  - UNSPECIFIED
  - SMPTE170M
  - SMPTE240M
Generated Go code

file model_color_space.go:

        type ColorSpace string

        const (
            UNSPECIFIED ColorSpace = "UNSPECIFIED"
            SMPTE170_M ColorSpace = "SMPTE170M"
            SMPTE240_M ColorSpace = "SMPTE240M"
        )

file model_color_primaries.go:

        type ColorPrimaries string

        const (
            UNSPECIFIED ColorPrimaries = "UNSPECIFIED"
            SMPTE170_M ColorPrimaries = "SMPTE170M"
            SMPTE240_M ColorPrimaries = "SMPTE240M"
        )
Command line used for generation

java -jar /opt/openapi-generator-cli.jar generate -g go -i openapi.json -o /go/src/go-client

Suggest a fix/enhancement

Maybe this could be solved by adding a prefix to the constants of the enum

Go

Most helpful comment

I am using version 4.1.1 and I still see the same issue.

All 5 comments

Good post @moser4035.

I've run into this in the past as well.

I've filed PR https://github.com/OpenAPITools/openapi-generator/pull/542 against the 4.0.x branch because this is a braking change without fallback.

This only changes model.mustache as shown below. Everything else in the PR is for testing. Feel free to use this locally until this is shipped in a release.

https://github.com/OpenAPITools/openapi-generator/pull/542/files#diff-6403a68f6b1039398715805f9043d0a1

This also happens when the values are similar but have different non-punctuation characters:

Here is some test output:

type EnumClassAntiCollision string

// List of EnumClassAntiCollision
const (
    ABC EnumClassAntiCollision = "abc"
    ABC EnumClassAntiCollision = "_abc"
    ABC EnumClassAntiCollision = "-abc"
    ABC EnumClassAntiCollision = "(abc)"
)

go-swagger handles this by substituting characters for the punctuation including Minus for - and Nr for (). Of note it also only capitalizes the first letter.

type EnumClass string

const (
    // EnumClassAbc captures enum value "_abc"
    EnumClassAbc EnumClass = "_abc"
    // EnumClassMinusEfg captures enum value "-efg"
    EnumClassMinusEfg EnumClass = "-efg"
    // EnumClassNrXyz captures enum value "(xyz)"
    EnumClassNrXyz EnumClass = "(xyz)"
)

Hi, I have encountered the same problem in generating go-server stub code.
I used sample yaml - openapi.yaml
https://raw.githubusercontent.com/OpenAPITools/openapi-generator/master/samples/openapi3/server/petstore/go-api-server/api/openapi.yaml

go/model_outer_enum_default_value.go:16:33: PLACED redeclared in this block
previous declaration at go/model_outer_enum.go:16:21
go/model_outer_enum_default_value.go:17:35: APPROVED redeclared in this block
previous declaration at go/model_outer_enum.go:17:23
go/model_outer_enum_default_value.go:18:36: DELIVERED redeclared in this block
previous declaration at go/model_outer_enum.go:18:24

1537 OuterEnum:
1538 enum:
1539 - placed
1540 - approved
1541 - delivered
1542 type: string

1550 OuterEnumDefaultValue:
1551 default: placed
1552 enum:
1553 - placed
1554 - approved
1555 - delivered
1556 type: string

Problem still exist?

I am using version 4.1.1 and I still see the same issue.

I ran into this with 4.3.2 but see the option for enumClassPrefix which is disabled by default.

-p enumClassPrefix=true

fixed it for me

Was this page helpful?
0 / 5 - 0 ratings