Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Go] Duplicate constant names for enums with same values #535

Open
moser4035 opened this issue Jul 11, 2018 · 7 comments
Open

[Go] Duplicate constant names for enums with same values #535

moser4035 opened this issue Jul 11, 2018 · 7 comments

Comments

@moser4035
Copy link

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

@grokify
Copy link
Member

grokify commented Jul 12, 2018

Good post @moser4035.

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

I've filed PR #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

@grokify
Copy link
Member

grokify commented Jul 12, 2018

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)"
)

@unipark00
Copy link

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?

@kanwar-saad
Copy link

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

@mikecowgill
Copy link

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

@snaka
Copy link

snaka commented Sep 15, 2022

This problem is supposed to be solved by the enumClassPrefix option.

see: https://github.com/OpenAPITools/openapi-generator/blob/master/docs/generators/go-gin-server.md

nilskuhn pushed a commit to nilskuhn/openapi-generator that referenced this issue Apr 6, 2023
…e-versions-4.x

fix(deps): update dependency compare-versions to v4
@mlebihan
Copy link
Contributor

mlebihan commented May 31, 2024

No, I disagree. The handling of duplicated enums name isn't finish.

enumClassPrefix adds a prefix on every constant {{name}}:

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

so that if we have:

animals in openapi.yaml with: enums dog, sheep, bird
and
petsAccepted with: enums dog, cat

It will generate:

(
       ANIMALS_DOG = "dog"
       ANIMALS_SHEEP = "sheep"
       ANIMALS_BIRD = "bird"
)

and

(
       PETSACCEPTED_DOG = "dog"
       PETSACCEPTED_CAT = "cat"
)

That many developers, I think, would like to avoid, because it's heavy to use and not required for 3 enums name on 4 that aren't duplicated.

→ currently I encounter the problem at work: 15 enums names are duplicated among 1,000 and I don't want to prefix 985 enums names that don't require it.

And if enumClassPrefix is false, you cannot a wrong code being generated whatever.


I my mind, when enumClassPrefix is false, you have to prefix names that are really found duplicated:

(
       ANIMALS_DOG = "dog"
       SHEEP = "sheep"
       BIRD = "bird"
)

and

(
       PETSACCEPTED_DOG = "dog"
       CAT = "cat"
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants