|
| 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 | +// A tag or an operationId of a single character is ordinary in OAS 2.0 — both are free-form strings |
| 16 | +// — and used to void the whole `swagger:route` it appeared in. |
| 17 | +// |
| 18 | +// The failure was not local to the short name, which is why it was so quiet. The tags group is |
| 19 | +// optional, so the parse did not stop when `e` failed to match: it fell back to matching with NO |
| 20 | +// tags, leaving the operationId pattern to swallow `e listOne`, whose alphabet has no space in it. |
| 21 | +// The line then matched nothing at all — and a `swagger:route` that matches nothing is not a |
| 22 | +// malformed route, it is simply not a route. Nothing downstream could tell it apart from prose, so |
| 23 | +// there was nothing to report and the path just never appeared. |
| 24 | +func TestRouteNameShapes(t *testing.T) { |
| 25 | + doc, err := codescan.Run(&codescan.Options{ |
| 26 | + Packages: []string{"./enhancements/route-name-shapes/..."}, |
| 27 | + WorkDir: scantest.FixturesDir(), |
| 28 | + }) |
| 29 | + require.NoError(t, err) |
| 30 | + require.NotNil(t, doc) |
| 31 | + require.NotNil(t, doc.Paths) |
| 32 | + |
| 33 | + for _, tc := range []struct { |
| 34 | + path string |
| 35 | + wantID string |
| 36 | + wantTag []string |
| 37 | + }{ |
| 38 | + {path: "/short-tag", wantID: "listOne", wantTag: []string{"e"}}, |
| 39 | + {path: "/short-id", wantID: "l", wantTag: []string{"shapes"}}, |
| 40 | + {path: "/short-both", wantID: "l", wantTag: []string{"e"}}, |
| 41 | + {path: "/short-id-no-tags", wantID: "q"}, |
| 42 | + {path: "/short-among", wantID: "listAmong", wantTag: []string{"a", "shapes"}}, |
| 43 | + } { |
| 44 | + t.Run(tc.path, func(t *testing.T) { |
| 45 | + item, ok := doc.Paths.Paths[tc.path] |
| 46 | + require.True(t, ok, "no path %s: the annotation did not parse", tc.path) |
| 47 | + require.NotNil(t, item.Get) |
| 48 | + assert.Equal(t, tc.wantID, item.Get.ID) |
| 49 | + assert.Equal(t, tc.wantTag, item.Get.Tags) |
| 50 | + }) |
| 51 | + } |
| 52 | + |
| 53 | + // The per-path assertions above say the annotations parsed; the golden says what they produced. |
| 54 | + scantest.CompareOrDumpJSON(t, doc, "enhancements_route_name_shapes.json") |
| 55 | +} |
0 commit comments