Skip to content

Conversation

@corani
Copy link
Contributor

@corani corani commented Jul 7, 2022

Add a compatibility option to always prefix enum values with their typename.

Previously the following spec:

status:
  type: string
  enum: [ success, failed ]

would be generated as:

type Status string

const (
    Success Status = "success"
    Failed  Status = "failed"
)

or in case of conflicts:

type Status string

const (
    StatusSuccess Status = "success"
    StatusFailed  Status = "failed"
)

This means if you start out without conflicts but introduce conflicts in a later version the generated code unexpectedly changes from under you and you potentially have to fix your code in many places.

When the new option compatibility.always-prefix-enum-values is set to true, the latter code is always generated.

Closes: #625

@corani corani marked this pull request as ready for review July 7, 2022 04:01
@corani corani force-pushed the daniel/prefix-enum branch from 34b47da to be564df Compare July 7, 2022 04:07
@corani corani changed the title Add option to prefix enum values with their typename (#657) Add option to prefix enum values with their typename (#625) Jul 7, 2022
@corani corani changed the title Add option to prefix enum values with their typename (#625) Add option to prefix enum values with their typename Jul 7, 2022
// This is set to true when this enum conflicts with another in terms of
// TypeNames or when explicitly requested via the
// `compatibility.always-prefix-enum-values` option.
PrefixTypename bool
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably makes sense to be:

Suggested change
PrefixTypename bool
PrefixTypeName bool

If it's using TypeName

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, done!

@corani corani force-pushed the daniel/prefix-enum branch from be564df to 60e0217 Compare July 12, 2022 01:33
@deepmap-marcinr deepmap-marcinr merged commit d9622d1 into oapi-codegen:master Jul 25, 2022
@corani corani deleted the daniel/prefix-enum branch July 26, 2022 01:14
adrianpk pushed a commit to foorester/oapi-codegen that referenced this pull request Jan 16, 2024
kodiakhq bot pushed a commit to cloudquery/cloudquery-api-go that referenced this pull request May 8, 2024
This ensures enums are always generated with the same name, regardless of conflicts (see oapi-codegen/oapi-codegen#662)
adrianpk added a commit to foorester/oapi-codegen that referenced this pull request May 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Enum value collision

3 participants