Skip to content

Commit 996f336

Browse files
fredbiclaude
andcommitted
test(integration): witness fixture for FindModel→GetModel migration
Adds an alias-to-unannotated-target fixture exercised under RefAliases=true. The body of WitnessParams and WitnessResponse is a Go alias whose RHS is an unannotated struct — exactly the shape that, under the old FindModel, would have triggered the implicit ExtraModels registration on the FindDecl-fallback path. Verified A/B against 21148c1^: the golden is byte-identical under the deprecated FindModel and the explicit GetModel + AppendPostDecl pair. PlainTarget reaches spec.definitions through the orchestrator's discovery loop in both cases. The fixture locks the equivalence and serves as institutional memory. Co-Authored-By: Claude <noreply@anthropic.com> Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
1 parent 21148c1 commit 996f336

3 files changed

Lines changed: 129 additions & 0 deletions

File tree

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// SPDX-FileCopyrightText: Copyright 2015-2025 go-swagger maintainers
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
// Package alias_findmodel_witness exercises the parameters and
5+
// responses builders' GetModel calls on alias targets that are
6+
// not annotated with swagger:model — the FindDecl-fallback path.
7+
//
8+
// Pre-migration FindModel registered such targets in ExtraModels
9+
// as an implicit side effect of the lookup. The explicit GetModel
10+
// + AppendPostDecl pair leaves the registration to the spec
11+
// orchestrator's discovery loop, which visits the queued decl and
12+
// produces the same top-level definition.
13+
//
14+
// The golden captured here locks the two paths to identical
15+
// output, witnessing the safety of the FindModel → GetModel
16+
// migration on the parameters and responses builders.
17+
package alias_findmodel_witness
18+
19+
// PlainTarget is a user struct with no swagger:model annotation.
20+
// It must end up in spec.definitions only via the orchestrator's
21+
// discovery of the alias's RHS — not via any implicit lookup side
22+
// effect at scan time.
23+
type PlainTarget struct {
24+
// required: true
25+
ID int64 `json:"id"`
26+
27+
Note string `json:"note"`
28+
}
29+
30+
// AliasOfPlain is an alias pointing at the unannotated target.
31+
type AliasOfPlain = PlainTarget
32+
33+
// WitnessParams has a body parameter whose Go type is an alias
34+
// of an unannotated struct. Triggers buildFieldAlias on the Body
35+
// field; under RefAliases the GetModel(RHS) lookup at the Named
36+
// switch arm is the relevant call.
37+
//
38+
// swagger:parameters witnessRequest
39+
type WitnessParams struct {
40+
// in: body
41+
// required: true
42+
Body AliasOfPlain `json:"body"`
43+
}
44+
45+
// WitnessResponse has a response body whose Go type is the same
46+
// alias — mirror witness on the response builder's
47+
// buildFieldAlias path.
48+
//
49+
// swagger:response witnessResponse
50+
type WitnessResponse struct {
51+
// in: body
52+
Body AliasOfPlain `json:"body"`
53+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"swagger": "2.0",
3+
"paths": {},
4+
"definitions": {
5+
"AliasOfPlain": {
6+
"title": "AliasOfPlain is an alias pointing at the unannotated target.",
7+
"$ref": "#/definitions/PlainTarget"
8+
},
9+
"PlainTarget": {
10+
"description": "It must end up in spec.definitions only via the orchestrator's\ndiscovery of the alias's RHS — not via any implicit lookup side\neffect at scan time.",
11+
"type": "object",
12+
"title": "PlainTarget is a user struct with no swagger:model annotation.",
13+
"required": [
14+
"id"
15+
],
16+
"properties": {
17+
"id": {
18+
"type": "integer",
19+
"format": "int64",
20+
"x-go-name": "ID"
21+
},
22+
"note": {
23+
"type": "string",
24+
"x-go-name": "Note"
25+
}
26+
},
27+
"x-go-package": "github.com/go-openapi/codescan/fixtures/enhancements/alias-findmodel-witness"
28+
}
29+
},
30+
"responses": {
31+
"witnessResponse": {
32+
"description": "WitnessResponse has a response body whose Go type is the same\nalias — mirror witness on the response builder's\nbuildFieldAlias path.",
33+
"schema": {
34+
"$ref": "#/definitions/AliasOfPlain"
35+
}
36+
}
37+
}
38+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// SPDX-FileCopyrightText: Copyright 2015-2025 go-swagger maintainers
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package integration_test
5+
6+
import (
7+
"testing"
8+
9+
"github.com/go-openapi/codescan"
10+
"github.com/go-openapi/codescan/internal/scantest"
11+
"github.com/go-openapi/testify/v2/assert"
12+
"github.com/go-openapi/testify/v2/require"
13+
)
14+
15+
// TestCoverage_AliasFindModelWitness — locks the post-migration
16+
// behaviour of GetModel + AppendPostDecl on alias targets that are
17+
// not annotated swagger:model.
18+
//
19+
// PlainTarget (unannotated) must reach spec.definitions via the
20+
// orchestrator's discovery loop after the alias's RHS lookup
21+
// queues it; under the pre-migration FindModel, the same decl
22+
// reached definitions via the implicit ExtraModels side effect.
23+
// Both paths produce identical output; the golden is the witness.
24+
func TestCoverage_AliasFindModelWitness(t *testing.T) {
25+
doc, err := codescan.Run(&codescan.Options{
26+
Packages: []string{"./enhancements/alias-findmodel-witness/..."},
27+
WorkDir: scantest.FixturesDir(),
28+
ScanModels: true,
29+
RefAliases: true,
30+
})
31+
require.NoError(t, err)
32+
require.NotNil(t, doc)
33+
34+
assert.Contains(t, doc.Definitions, "PlainTarget",
35+
"unannotated alias target must reach definitions via the orchestrator's discovery path")
36+
37+
scantest.CompareOrDumpJSON(t, doc, "enhancements_alias_findmodel_witness.json")
38+
}

0 commit comments

Comments
 (0)
⌂ 换网址