Fix swagger path generation: remove trailing slash for root routes with prefix (#5212)

This commit is contained in:
Copilot
2025-10-05 12:12:03 +08:00
committed by GitHub
parent 875fec3e1a
commit 7a6c3c8129
2 changed files with 95 additions and 1 deletions

View File

@@ -19,7 +19,11 @@ func spec2Paths(ctx Context, srv apiSpec.Service) *spec.Paths {
for _, route := range group.Routes {
routPath := pathVariable2SwaggerVariable(ctx, route.Path)
if len(prefix) > 0 && prefix != "." {
routPath = "/" + path.Clean(prefix) + routPath
if routPath == "/" {
routPath = "/" + path.Clean(prefix)
} else {
routPath = "/" + path.Clean(prefix) + routPath
}
}
pathItem := spec2Path(ctx, group, route)
existPathItem, ok := paths.Paths[routPath]

View File

@@ -0,0 +1,90 @@
package swagger
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/zeromicro/go-zero/tools/goctl/api/spec"
)
func TestSpec2PathsWithRootRoute(t *testing.T) {
tests := []struct {
name string
prefix string
routePath string
expectedPath string
}{
{
name: "prefix with root route",
prefix: "/api/v1/shoppings",
routePath: "/",
expectedPath: "/api/v1/shoppings",
},
{
name: "prefix with sub route",
prefix: "/api/v1/shoppings",
routePath: "/list",
expectedPath: "/api/v1/shoppings/list",
},
{
name: "empty prefix with root route",
prefix: "",
routePath: "/",
expectedPath: "/",
},
{
name: "empty prefix with sub route",
prefix: "",
routePath: "/list",
expectedPath: "/list",
},
{
name: "prefix with trailing slash and root route",
prefix: "/api/v1/shoppings/",
routePath: "/",
expectedPath: "/api/v1/shoppings",
},
{
name: "prefix without leading slash and root route",
prefix: "api/v1/shoppings",
routePath: "/",
expectedPath: "/api/v1/shoppings",
},
{
name: "single level prefix with root route",
prefix: "/api",
routePath: "/",
expectedPath: "/api",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
srv := spec.Service{
Groups: []spec.Group{
{
Annotation: spec.Annotation{
Properties: map[string]string{
propertyKeyPrefix: tt.prefix,
},
},
Routes: []spec.Route{
{
Method: "get",
Path: tt.routePath,
Handler: "TestHandler",
},
},
},
},
}
ctx := testingContext(t)
paths := spec2Paths(ctx, srv)
assert.Contains(t, paths.Paths, tt.expectedPath,
"Expected path %s not found in generated paths. Got: %v",
tt.expectedPath, paths.Paths)
})
}
}