Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions internal/test/issues/issue-1189/issue1189.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

92 changes: 77 additions & 15 deletions pkg/codegen/codegen.go
Original file line number Diff line number Diff line change
Expand Up @@ -1021,25 +1021,87 @@ func GenerateEnums(t *template.Template, types []TypeDefinition) (string, error)

// Now, go through all the enums, and figure out if we have conflicts with
// any others.
for i := range enums {
// Look through all other enums not compared so far. Make sure we don't
// compare against self.
e1 := enums[i]
for j := i + 1; j < len(enums); j++ {
e2 := enums[j]

for e1key := range e1.GetValues() {
_, found := e2.GetValues()[e1key]
if found {
e1.PrefixTypeName = true
e2.PrefixTypeName = true
enums[i] = e1
enums[j] = e2
break
if globalState.options.Compatibility.OldEnumConflicts {
// Legacy behavior (pre-v2.7.1): compare generated constant names via
// GetValues(). This is order-dependent because GetValues() reflects
// already-applied prefixes, so enums processed later may miss conflicts
// with enums that were prefixed in an earlier iteration. Preserved here
// as an escape hatch for users who need to keep existing generated output.
for i := range enums {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If I understand this right, the next loop completely supersedes this one, so this one doesn't even need to run.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Not quite, Stage 2 alone misses CState in the AState/BState/CState case.

Once Stage 2's first pass prefixes AState because it shares "running" with BState, AState.GetValues() returns {AStateRunning, AStateMigrating}.

When it then checks AState vs CState, "migrating" in CState no longer matches any key in AState's now-prefixed values, so CState is left unprefixed.

I verified this by removing Stage 1 and running the tests, both TestEnumConflictDetectionOrderIndependent and TestEnumConflictDetectionBothOrders fail with Migrating CState = "migrating" in the output.

e1 := enums[i]
for j := i + 1; j < len(enums); j++ {
e2 := enums[j]
for e1key := range e1.GetValues() {
if _, found := e2.GetValues()[e1key]; found {
e1.PrefixTypeName = true
e2.PrefixTypeName = true
enums[i] = e1
enums[j] = e2
break
}
}
}
}
} else {
// Stage 1: detect conflicts based on raw (unprefixed) values. This catches
// the case where two enums share the same value string (e.g. both have
// "running"), regardless of which order they appear.
for i := range enums {
e1 := enums[i]
for j := i + 1; j < len(enums); j++ {
e2 := enums[j]
for e1key := range e1.Schema.EnumValues {
if _, found := e2.Schema.EnumValues[e1key]; found {
e1.PrefixTypeName = true
e2.PrefixTypeName = true
enums[i] = e1
enums[j] = e2
break
}
}
}
}

// Stage 2: iteratively detect effective-name conflicts. After Stage 1 some
// enums are now prefixed; their prefixed constant names (e.g. "Enum1One")
// may collide with another enum's raw constant name. We repeat until
// stable.
for {
changed := false
for i := range enums {
e1 := enums[i]
for j := i + 1; j < len(enums); j++ {
e2 := enums[j]
if e1.PrefixTypeName && e2.PrefixTypeName {
continue
}
for e1key := range e1.GetValues() {
if _, found := e2.GetValues()[e1key]; found {
if !e1.PrefixTypeName {
e1.PrefixTypeName = true
enums[i] = e1
changed = true
}
if !e2.PrefixTypeName {
e2.PrefixTypeName = true
enums[j] = e2
changed = true
}
break
}
}
}
}
if !changed {
break
}
}
}

// Check each enum against global type names and self-conflict.
for i := range enums {
e1 := enums[i]

// now see if this enum conflicts with any global type names.
for _, tp := range types {
// Skip over enums, since we've handled those above.
Expand Down
130 changes: 130 additions & 0 deletions pkg/codegen/codegen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,5 +329,135 @@ paths:
assert.Contains(t, code, "roleName string")
}

// TestEnumConflictDetectionOrderIndependent checks that conflict detection
// doesn't miss overlaps because an enum was already marked for prefixing.
func TestEnumConflictDetectionOrderIndependent(t *testing.T) {
// AState+BState share "running" (both prefixed), AState+CState share "migrating".
// The bug: once AState was marked, GetValues() returned prefixed names that
// no longer matched CState's raw values, so CState's conflict was missed.
const spec = `
openapi: "3.0.0"
info:
version: 1.0.0
title: Test Enum Conflict Detection
paths: {}
components:
schemas:
AState:
type: string
enum:
- running
- migrating
BState:
type: string
enum:
- running
CState:
type: string
enum:
- migrating
`
loader := openapi3.NewLoader()
swagger, err := loader.LoadFromData([]byte(spec))
require.NoError(t, err)

opts := Configuration{
PackageName: "api",
Generate: GenerateOptions{
Models: true,
},
OutputOptions: OutputOptions{
SkipPrune: true,
},
}

code, err := Generate(swagger, opts)
require.NoError(t, err)

_, err = format.Source([]byte(code))
require.NoError(t, err)

// All three enums share values with at least one other enum; all must be prefixed.
assert.Contains(t, code, "AStateRunning")
assert.Contains(t, code, "AStateMigrating")
assert.Contains(t, code, "BStateRunning")
assert.Contains(t, code, "CStateMigrating")
}

// TestEnumConflictDetectionBothOrders verifies that enum conflict detection
// produces identical output regardless of the order schemas appear in the spec.
// The old GetValues()-based approach was order-dependent: processing AState
// before CState caused CState to be left unprefixed, while reversing the order
// would prefix it. Go map iteration is non-deterministic (randomized since
// Go 1.12), so this was a latent correctness bug.
func TestEnumConflictDetectionBothOrders(t *testing.T) {
specAFirst := `
openapi: "3.0.0"
info:
version: 1.0.0
title: Test
paths: {}
components:
schemas:
AState:
type: string
enum: [running, migrating]
BState:
type: string
enum: [running]
CState:
type: string
enum: [migrating]
`
specCFirst := `
openapi: "3.0.0"
info:
version: 1.0.0
title: Test
paths: {}
components:
schemas:
CState:
type: string
enum: [migrating]
BState:
type: string
enum: [running]
AState:
type: string
enum: [running, migrating]
`
opts := Configuration{
PackageName: "api",
Generate: GenerateOptions{Models: true},
OutputOptions: OutputOptions{
SkipPrune: true,
},
}

loader := openapi3.NewLoader()

swaggerA, err := loader.LoadFromData([]byte(specAFirst))
require.NoError(t, err)
codeA, err := Generate(swaggerA, opts)
require.NoError(t, err)

swaggerC, err := loader.LoadFromData([]byte(specCFirst))
require.NoError(t, err)
codeC, err := Generate(swaggerC, opts)
require.NoError(t, err)

// Both orderings must produce fully prefixed constants.
for _, code := range []string{codeA, codeC} {
assert.Contains(t, code, "AStateRunning")
assert.Contains(t, code, "AStateMigrating")
assert.Contains(t, code, "BStateRunning")
assert.Contains(t, code, "CStateMigrating")
// Unprefixed names must not appear as standalone constants.
assert.NotContains(t, code, "\tRunning ")
assert.NotContains(t, code, "\tMigrating ")
}
}

//go:embed test_spec.yaml
var testOpenAPIDefinition string
8 changes: 7 additions & 1 deletion pkg/codegen/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,13 @@ type CompatibilityOptions struct {
// Enum values can generate conflicting typenames, so we've updated the
// code for enum generation to avoid these conflicts, but it will result
// in some enum types being renamed in existing code. Set OldEnumConflicts to true
// to revert to old behavior. Please see:
// to revert to old behavior.
// As of v2.7.1, this also reverts the order-independent conflict detection
// fix (issue #2391): the legacy GetValues()-based path is order-dependent
// and may produce different output on different runs, but is provided as
// an escape hatch for users who need to preserve existing generated code.
// Please see https://github.com/oapi-codegen/oapi-codegen/issues/549
// Please see https://github.com/oapi-codegen/oapi-codegen/issues/2391
OldEnumConflicts bool `yaml:"old-enum-conflicts,omitempty"`
// It was a mistake to generate a go type definition for every $ref in
// the OpenAPI schema. New behavior uses type aliases where possible, but
Expand Down Expand Up @@ -299,6 +304,7 @@ type CompatibilityOptions struct {
// are treated as required.
// Please see https://github.com/oapi-codegen/oapi-codegen/issues/2267
HeadersImplicitlyRequired bool `yaml:"headers-implicitly-required,omitempty"`

}

func (co CompatibilityOptions) Validate() map[string]string {
Expand Down