From da8515c8dde76047169ce16ca148961bc179c04b Mon Sep 17 00:00:00 2001 From: kidkender Date: Fri, 13 Nov 2026 09:44:46 +0700 Subject: [PATCH] fix(go-parser): join group prefixes and route paths the way Gin does Route paths were the plain concatenation of every Group() prefix and the route path, so groups written without slashes (Group("api").Group("v1") .POST("device")) came out as "apiv1device" instead of /api/v1/device, and "/api/" + "/users" as "/api//users". Gin joins them with path.Join, keeping a trailing slash on the relative path; joinPaths does the same. thingspanel-backend-community: 233 of 267 routes had a malformed path, now 0. ginfast: 2 of 122, now 0. Route counts unchanged. --- .../src/__tests__/route-path-join.test.ts | 59 +++++++++++++++++++ packages/go-parser/src/route-parser.ts | 17 +++++- 2 files changed, 73 insertions(+), 3 deletions(-) create mode 100644 packages/go-parser/src/__tests__/route-path-join.test.ts diff --git a/packages/go-parser/src/__tests__/route-path-join.test.ts b/packages/go-parser/src/__tests__/route-path-join.test.ts new file mode 100644 index 0000000..0895af2 --- /dev/null +++ b/packages/go-parser/src/__tests__/route-path-join.test.ts @@ -0,0 +1,59 @@ +import { describe, test, expect } from "@jest/globals" +import { extractRoutes, joinPaths, type GoSourceFile } from "../route-parser.js" + +// Gin joins a group's prefix and a route's path with path.Join, so slashes are +// optional on either side: Group("api").Group("v1").POST("device") serves +// /api/v1/device. Plain concatenation produced "apiv1device". + +const file = (content: string): GoSourceFile => ({ path: "main.go", content }) +const paths = (content: string) => extractRoutes([file(content)]).map((r) => `${r.method} ${r.path}`) + +describe("joinPaths", () => { + test.each([ + ["", "", ""], + ["", "/", "/"], + ["", "api", "/api"], + ["/api", "v1", "/api/v1"], + ["/api/", "/v1", "/api/v1"], + ["/api", "", "/api"], + ["/api", "/", "/api/"], + ["/api", "users/", "/api/users/"], + ["/api", "//users", "/api/users"], + ["/api", ":id", "/api/:id"], + ["/files", "*filepath", "/files/*filepath"], + ])("joinPaths(%j, %j) = %j", (base, relative, expected) => { + expect(joinPaths(base, relative)).toBe(expected) + }) +}) + +describe("route paths built from groups without slashes", () => { + test("groups and routes written without leading slashes", () => { + expect(paths(`package main +func main() { + r := gin.Default() + api := r.Group("api") + v1 := api.Group("v1") + device := v1.Group("device") + device.POST("", create) + device.GET(":id", get) + device.GET("detail/:id", detail) +}`)).toEqual(["POST /api/v1/device", "GET /api/v1/device/:id", "GET /api/v1/device/detail/:id"]) + }) + + test("a trailing slash on the group does not double up", () => { + expect(paths(`package main +func main() { + r := gin.Default() + api := r.Group("/api/") + api.GET("/users", list) +}`)).toEqual(["GET /api/users"]) + }) + + test("a route on the engine with an empty path is /", () => { + expect(paths(`package main +func main() { + r := gin.Default() + r.GET("", home) +}`)).toEqual(["GET /"]) + }) +}) diff --git a/packages/go-parser/src/route-parser.ts b/packages/go-parser/src/route-parser.ts index 7a7c525..d7abdf9 100644 --- a/packages/go-parser/src/route-parser.ts +++ b/packages/go-parser/src/route-parser.ts @@ -21,7 +21,7 @@ export interface MiddlewareRef { export interface RouteInfo { method: string - /** Fully resolved path — concatenation of every enclosing Group() prefix. */ + /** Fully resolved path — every enclosing Group() prefix joined the way Gin joins them. */ path: string /** Raw text of the final (handler) argument, e.g. "handler.CreateOrder". */ handlerText: string @@ -170,7 +170,18 @@ function groupOf(expr: SyntaxNode, file: string, groups: Map const refs = (nodes: SyntaxNode[]): MiddlewareRef[] => nodes.map((a) => toMiddlewareRef(a, file)).filter((m): m is MiddlewareRef => !!m) if (method === "Use") return { ...base, middleware: [...base.middleware, ...refs(args)] } const [pathArg, ...mwArgs] = args - return { prefix: base.prefix + (pathArg ? stringLiteralValue(pathArg) ?? "" : ""), middleware: [...base.middleware, ...refs(mwArgs)] } + return { prefix: joinPaths(base.prefix, pathArg ? stringLiteralValue(pathArg) ?? "" : ""), middleware: [...base.middleware, ...refs(mwArgs)] } +} + +/** + * Gin's joinPaths: `path.Join(base, relative)`, keeping a trailing slash the + * relative path has. `Group("api").Group("v1").POST("device")` is /api/v1/device, + * and `Group("/api/").GET("/x")` is /api/x — not a plain concatenation. + */ +export function joinPaths(base: string, relative: string): string { + if (relative === "") return base + const joined = "/" + [...base.split("/"), ...relative.split("/")].filter((s) => s !== "" && s !== ".").join("/") + return relative.endsWith("/") && !joined.endsWith("/") ? `${joined}/` : joined } /** @@ -313,7 +324,7 @@ function handleStatement( routes.push({ method: parts.method, - path: state.prefix + suffix, + path: joinPaths(state.prefix, suffix) || "/", handlerText: handlerArg.text, handlerReceiverType: handlerReceiverType(handlerArg, paramTypes), middleware: [...state.middleware, ...inlineMw],