Skip to content

Commit 5bbd33a

Browse files
fredbiclaude
andcommitted
feat(spec): name-identity deconfliction for colliding definition names
Replace the short-name definition-key proxy with a real model-identity mechanism so distinct Go types that share a name no longer merge silently (and non-deterministically) onto one definition. The build keys every definition by a fully-qualified, compiler-unique identity ("<pkgpath>/<name>", EntityDecl.DefKey); a final reduce stage projects each back to the shortest acceptable user-facing name: - a globally-unique leaf is lifted to its bare name (byte-identical to the previous output — zero churn in the common case); - a colliding leaf is qualified with the minimal-depth PascalCase concat of its nearest package segments (b.Test / c.Test -> BTest / CTest), deepening until the group is globally unique, with a diagnostic; - same-package duplicates (necessarily via swagger:model overrides) revert the later declaration to its Go name, with a diagnostic; - a colliding group whose best flat concat exceeds a readability budget can, opt-in, be emitted as nested container definitions instead. The whole pass is a pure function of the reachable identity set, so the output is deterministic regardless of discovery / map-iteration order. Author short-name $refs resolve to a colliding identity by leaf (unique -> promote, none -> dangling, ambiguous -> diagnose + drop). New options (scanner.Options): - NameConcatBudget (default 0.65): readability-score threshold above which a concat is a candidate for the hierarchical fail-safe. - EmitHierarchicalNames (default false): opt into the nested fallback. Includes the witness corpus (recursion / no-collision controls; the 3-way / mixed / same-package-dup / deep / recursion-collision / examples-no-mix / hierarchical scenarios) with golden locks, plus the readability-score exploration harness (disabled). Signed-off-by: Frederic BIDON <fredbi@yahoo.com> contributes to go-swagger#2783 contributes to go-swagger#2637 contributes to go-swagger#2662 contributes to go-swagger#1734 contributes to go-swagger#2126 contributes to go-swagger#2398 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent 64cbe48 commit 5bbd33a

89 files changed

Lines changed: 2842 additions & 246 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎fixtures/bugs/2637/api.go‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// SPDX-FileCopyrightText: Copyright 2015-2025 go-swagger maintainers
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
// Package bug2637 reproduces go-swagger issue #2637 ("Cyclic type definition
5+
// for defined types using the same name"): a local type defined from a
6+
// same-named type in another package collides on the short key and the
7+
// definition gets a SELF-$ref alongside its properties — an invalid, self-
8+
// cyclic definition that hangs downstream codegen. Same family as #2783.
9+
package bug2637
10+
11+
import "github.com/go-openapi/codescan/fixtures/bugs/2637/mongo"
12+
13+
// swagger:model CreateDomainRequest
14+
type CreateDomainRequest mongo.CreateDomainRequest
15+
16+
// swagger:parameters createDomain
17+
type createDomainParams struct {
18+
// in: body
19+
Body CreateDomainRequest
20+
}
21+
22+
// swagger:route POST /domains domains createDomain
23+
//
24+
// responses:
25+
// 200: description: ok
26+
func createDomain() {}

‎fixtures/bugs/2637/mongo/types.go‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// SPDX-FileCopyrightText: Copyright 2015-2025 go-swagger maintainers
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package mongo
5+
6+
// swagger:model CreateDomainRequest
7+
type CreateDomainRequest struct {
8+
Name string `json:"name"`
9+
}

‎fixtures/bugs/2783/api.go‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// SPDX-FileCopyrightText: Copyright 2015-2025 go-swagger maintainers
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
// Package bug2783 reproduces go-swagger issue #2783 ("Models get mixed when
5+
// using structs from several packages"): two packages each declare a
6+
// swagger:model named Test. They collide on the short definition key "Test"
7+
// and silently merge into one definition (union of fields, last package wins),
8+
// instead of being disambiguated.
9+
package bug2783
10+
11+
import (
12+
"github.com/go-openapi/codescan/fixtures/bugs/2783/b"
13+
"github.com/go-openapi/codescan/fixtures/bugs/2783/c"
14+
)
15+
16+
// swagger:model TestResponseBody
17+
type TestResponseBody struct {
18+
Test1 b.Test `json:"test1"`
19+
Test2 c.Test `json:"test2"`
20+
}

‎fixtures/bugs/2783/b/test.go‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// SPDX-FileCopyrightText: Copyright 2015-2025 go-swagger maintainers
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package b
5+
6+
// swagger:model Test
7+
type Test struct {
8+
B string `json:"b"`
9+
}

‎fixtures/bugs/2783/c/test.go‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// SPDX-FileCopyrightText: Copyright 2015-2025 go-swagger maintainers
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package c
5+
6+
// swagger:model Test
7+
type Test struct {
8+
// required: true
9+
C string `json:"c"`
10+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// SPDX-FileCopyrightText: Copyright 2015-2025 go-swagger maintainers
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package a
5+
6+
// swagger:model Widget
7+
type Widget struct {
8+
A string `json:"a"`
9+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// SPDX-FileCopyrightText: Copyright 2015-2025 go-swagger maintainers
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
// Package threeway is a name-identity corpus member: THREE packages each
5+
// declare a `swagger:model Widget` with distinct fields, all referenced from
6+
// one container. Today they collide on the short key "Widget" and merge into a
7+
// single definition (union of fields, last package wins, non-deterministic).
8+
// Target: three distinct definitions.
9+
package threeway
10+
11+
import (
12+
"github.com/go-openapi/codescan/fixtures/enhancements/name-identity-3way/a"
13+
"github.com/go-openapi/codescan/fixtures/enhancements/name-identity-3way/b"
14+
"github.com/go-openapi/codescan/fixtures/enhancements/name-identity-3way/c"
15+
)
16+
17+
// swagger:model Container
18+
type Container struct {
19+
A a.Widget `json:"a"`
20+
B b.Widget `json:"b"`
21+
C c.Widget `json:"c"`
22+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// SPDX-FileCopyrightText: Copyright 2015-2025 go-swagger maintainers
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package b
5+
6+
// swagger:model Widget
7+
type Widget struct {
8+
B int64 `json:"b"`
9+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// SPDX-FileCopyrightText: Copyright 2015-2025 go-swagger maintainers
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package c
5+
6+
// swagger:model Widget
7+
type Widget struct {
8+
// required: true
9+
C bool `json:"c"`
10+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// SPDX-FileCopyrightText: Copyright 2015-2025 go-swagger maintainers
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package mongo
5+
6+
// swagger:model Book
7+
type Book struct {
8+
Title string `json:"title"`
9+
}

0 commit comments

Comments
 (0)
⌂ 换网址