diff --git a/pkg/codegen/codegen.go b/pkg/codegen/codegen.go index 6d9f345b3..4b29d1a06 100644 --- a/pkg/codegen/codegen.go +++ b/pkg/codegen/codegen.go @@ -621,9 +621,10 @@ func GenerateEnums(t *template.Template, types []TypeDefinition) (string, error) wrapper = `"` } enums = append(enums, EnumDefinition{ - Schema: tp.Schema, - TypeName: tp.TypeName, - ValueWrapper: wrapper, + Schema: tp.Schema, + TypeName: tp.TypeName, + ValueWrapper: wrapper, + PrefixTypeName: globalState.options.Compatibility.AlwaysPrefixEnumValues, }) } } @@ -640,8 +641,8 @@ func GenerateEnums(t *template.Template, types []TypeDefinition) (string, error) for e1key := range e1.GetValues() { _, found := e2.GetValues()[e1key] if found { - e1.Conflicts = true - e2.Conflicts = true + e1.PrefixTypeName = true + e2.PrefixTypeName = true enums[i] = e1 enums[j] = e2 break @@ -657,7 +658,7 @@ func GenerateEnums(t *template.Template, types []TypeDefinition) (string, error) } _, found := e1.Schema.EnumValues[tp.TypeName] if found { - e1.Conflicts = true + e1.PrefixTypeName = true enums[i] = e1 } } @@ -666,7 +667,7 @@ func GenerateEnums(t *template.Template, types []TypeDefinition) (string, error) // type name. _, found := e1.GetValues()[e1.TypeName] if found { - e1.Conflicts = true + e1.PrefixTypeName = true enums[i] = e1 } } diff --git a/pkg/codegen/configuration.go b/pkg/codegen/configuration.go index c8530c8ea..727d813ff 100644 --- a/pkg/codegen/configuration.go +++ b/pkg/codegen/configuration.go @@ -26,7 +26,7 @@ type GenerateOptions struct { EchoServer bool `yaml:"echo-server,omitempty"` // EchoServer specifies whether to generate echo server boilerplate GinServer bool `yaml:"gin-server,omitempty"` // GinServer specifies whether to generate gin server boilerplate GorillaServer bool `yaml:"gorilla-server,omitempty"` // GorillaServer specifies whether to generate Gorilla server boilerplate - Strict bool `yaml:"strict-server,omitempty"` // Strict specifies whether to generate strict server wrapper + Strict bool `yaml:"strict-server,omitempty"` // Strict specifies whether to generate strict server wrapper Client bool `yaml:"client,omitempty"` // Client specifies whether to generate client boilerplate Models bool `yaml:"models,omitempty"` // Models specifies whether to generate type definitions EmbeddedSpec bool `yaml:"embedded-spec,omitempty"` // Whether to embed the swagger spec in the generated code @@ -57,6 +57,9 @@ type CompatibilityOptions struct { // When an object contains no members, and only an additionalProperties specification, // it is flattened to a map. Set DisableFlattenAdditionalProperties bool `yaml:"disable-flatten-additional-properties,omitempty"` + // When set to true, always prefix enum values with their type name instead of only + // when typenames would be conflicting. + AlwaysPrefixEnumValues bool `yaml:"always-prefix-enum-values,omitempty"` } // OutputOptions are used to modify the output code in some way. diff --git a/pkg/codegen/schema.go b/pkg/codegen/schema.go index f8da94f40..ce5f586a8 100644 --- a/pkg/codegen/schema.go +++ b/pkg/codegen/schema.go @@ -111,15 +111,17 @@ type EnumDefinition struct { // ValueWrapper wraps the value. It's used to conditionally apply quotes // around strings. ValueWrapper string - // Conflicts is set to true when this enum conflicts with another in - // terms of TypeNames - Conflicts bool + // PrefixTypeName determines if the enum value is prefixed with its TypeName. + // 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 } // GetValues generates enum names in a way to minimize global conflicts func (e *EnumDefinition) GetValues() map[string]string { // in case there are no conflicts, it's safe to use the values as-is - if !e.Conflicts { + if !e.PrefixTypeName { return e.Schema.EnumValues } // If we do have conflicts, we will prefix the enum's typename to the values.