Skip to content

fix(spec): document an empty sub-path at the prefix, not with a trailing slash - #613

Merged
ehabterra merged 2 commits into
mainfrom
fix/609-empty-subpath-trailing-slash
Nov 19, 2026
Merged

ehabterra merged 2 commits into
mainfrom
fix/609-empty-subpath-trailing-slash

Conversation

@ehabterra

@ehabterra ehabterra commented Nov 19, 2026 •

Copy link
Copy Markdown
Owner

Fixes #609.

Problem

users := api.Group("/users"); users.GET("", h) is served at /users by gin, echo and fiber, but was documented as /users/. A client generated from the spec calls the wrong URL. With gin's default RedirectTrailingSlash it gets a 301.

Two steps produced the slash, and fixing either one alone changes nothing:

  1. extractRouteDetails normalised an empty registration path to "/".
  2. joinPaths always inserted a separator, so joinPaths("/users", "") was /users/.

Fix

  • An empty path is kept as "", and a new RouteInfo.PathEmpty flag marks it as set. The two checks that read Path == "" as "not yet extracted" now go through hasPath(): IsValid, and the guard that keeps a resolved path from being overwritten by a nested placeholder. gitea's Combo("") → getPattern relies on that guard.
  • joinPaths(a, "") returns a untouched.
  • An explicit "/" still yields a trailing slash. chi's Route("/x"){ Get("/") } and gin's GET("/") really do register /x/.

Tests

  • New fixtures testdata/empty_subpath_{gin,echo,fiber} and generator/testdata_empty_subpath_test.go. The test asserts the full path set for each framework. The gin fixture keeps an explicit "/" route next to the empty ones, so a fix that strips every trailing slash would fail. The test fails on main.
  • New joinPaths unit cases: empty path, empty path after a prefix ending in /, explicit "/".
  • TestGenerateFromDirectory_EchoVarWrapperAuth asserted the buggy /user/ for user.GET("", me). It is corrected to /user, with a comment.

Measured on real projects (before → after)

project operations moved to slash-free path content changed other drift
golang-echo-realworld-example-app 6 0 none
Go-Clean-Architecture-REST-API 3 0 none
photoprism, go-clean-echo 0 – none
gitea 321 0 1243 → 1240 operations (below)

The 3 gitea operations are /x + /x/ pairs that gitea serves as one route: its getPattern trims the trailing slash at runtime. In each pair, one copy had no resolved handler (the #612 shape). The merged path keeps the handler-resolved operation. Which duplicate wins follows the existing rule (same method and path, last route wins), and route order is deterministic. Gitea's 10 remaining trailing-slash paths all come from explicit m.Get("/", …) registrations.

Summary by CodeRabbit

  • Bug Fixes
    • API route paths now preserve the difference between an empty subpath and an explicit /. Routes with empty subpaths are generated at the intended path, preventing an unwanted trailing slash and keeping root paths distinct.
  • Tests
    • Added regression coverage to verify generated paths and operations across supported framework fixtures.

…ing slash

`users := api.Group("/users"); users.GET("", h)` is served at /users by
gin, echo and fiber, but was documented as /users/. Two steps produced
the slash: extraction normalised an empty registration path to "/", and
joinPaths always inserted a separator, so "" and an explicit "/" collapsed
into the one spelling the framework does not serve.

An empty path is now kept as "" with a PathEmpty flag marking it as set
(the two checks that read Path == "" as "not yet extracted" go through
hasPath), and joinPaths returns the prefix untouched for an empty
sub-path. An explicit "/" still yields a trailing slash, as chi's
Route + Get("/") and gin's GET("/") really register one.

Fixes #609
@coderabbitai

coderabbitai Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

Explain Change →

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 53 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 78aff08 and 890fd1a.

Files ignored due to path filters: 3

  • testdata/empty_subpath_chi/go.mod is excluded by !**/testdata/**
  • testdata/empty_subpath_chi/go.sum is excluded by !**/*.sum, !**/testdata/**
  • testdata/empty_subpath_chi/main.go is excluded by !**/testdata/**
Files selected for processing: 1

  • generator/testdata_empty_subpath_test.go

No actionable comments were generated in the recent review.

Walkthrough

Route extraction now distinguishes an empty subpath from an explicit root path. Joining an empty subpath preserves the mount prefix. Regression tests cover generated paths for Gin, Echo, and Fiber.

Changes

Empty Subpath Handling

Layer / File(s) Summary
Track and join empty paths
internal/spec/extractor.go, internal/spec/pattern_matchers.go
RouteInfo records an extracted empty path separately from an unset path. Validation accepts an empty path when a handler is present. Joining an empty subpath preserves the prefix, and path extraction retains an existing resolved path when the incoming path contains only placeholders.
Verify generated paths
generator/testdata_empty_subpath_test.go, internal/spec/extractor_simple_test.go, generator/security_test.go
Regression tests cover empty and explicit root paths, validate generated operations for Gin, Echo, and Fiber fixtures, and expect the Gin variable-middleware route at /user.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Medium

Merge Risk: Minimal · previous review 78aff

This change documents an empty sub-path under a route group at the group prefix, for example /users instead of /users/, and still keeps a trailing slash for an explicit "/". It adds fixtures and tests for gin, echo and fiber. No outstanding merge-blocking risk was found.

Pre-merge checks: Passed 3 · Inconclusive 1

Failed Failed checks: 1 inconclusive

Check name Status Explanation Resolution
Linked Issues check Inconclusive Issue #609 requires: (1) an empty sub-path under a group documented at the group prefix (g.GET("", h) on /users gives /users); (2) explicit "/" keeps its trailing slash (chi `Route("/x"){ Get(… Provide the contents of testdata/empty_subpath_gin/main.go and testdata/empty_subpath_echo/main.go (excluded by !**/testdata/**) so the group-prefix routes can be confirmed. Confirm whether a chi fixture exists, or state that the gin …
Passed Passed checks: 3 passed

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the main change: an empty sub-path is now documented at the group prefix instead of with a trailing slash. The fix is in internal/spec path extraction and joinPaths, which the raw summ…
Out of Scope Changes check Passed The reviewable changes connect to #609. internal/spec/extractor.go and internal/spec/pattern_matchers.go implement the empty-path distinction. internal/spec/extractor_simple_test.go covers `join…
Full details: Title check

Explanation

The title names the main change: an empty sub-path is now documented at the group prefix instead of with a trailing slash. The fix is in internal/spec path extraction and joinPaths, which the raw summary and objectives both confirm. The title follows the conventional commit form and fits in one sentence, so it passes.

Full details: Out of Scope Changes check

Explanation

The reviewable changes connect to #609. internal/spec/extractor.go and internal/spec/pattern_matchers.go implement the empty-path distinction. internal/spec/extractor_simple_test.go covers joinPaths. generator/security_test.go corrects the /user/ expectation caused by the same bug. generator/testdata_empty_subpath_test.go is a regression test for the fixtures. The gitea result (1243 → 1240 operations) follows from the change. Three /x and /x/ pairs now merge under the existing same-method, same-path, last-route-wins rule. The PR description discloses this effect. No reviewable change was shown to be unrelated to #609. The fixture files are test data and were not inspected.

Full details: Linked Issues check

Explanation

Issue #609 requires: (1) an empty sub-path under a group documented at the group prefix (g.GET("", h) on /users gives /users); (2) explicit "/" keeps its trailing slash (chi Route("/x"){ Get("/") } gives /x/); (3) a fixture covering gin and echo next to the chi case. Source evidence supports (1) and (2). extractRouteDetails now keeps "" with RouteInfo.PathEmpty. hasPath() guards IsValid and the nested-placeholder overwrite check. joinPaths(a, "") returns a. Unit cases cover empty path, empty path after a /-ending prefix, and explicit "/". The generator test in generator/testdata_empty_subpath_test.go asserts the expected path set for gin, echo, and fiber, and the gin expectations keep an explicit "/" route. The fixture sources testdata/empty_subpath_{gin,echo,fiber}/main.go are excluded by rule !**/testdata/**, so their content was not inspected. Requirement (3) depends on those files. Also, the acceptance criterion names a chi case, and no chi fixture appears in the PR. The explicit-slash behavior is covered only by a gin fixture and a unit test.

Resolution

Provide the contents of testdata/empty_subpath_gin/main.go and testdata/empty_subpath_echo/main.go (excluded by !**/testdata/**) so the group-prefix routes can be confirmed. Confirm whether a chi fixture exists, or state that the gin explicit-"/" fixture and the joinPaths unit case are the intended coverage for the chi requirement.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 7cb1bdd and 78aff08.

Files ignored due to path filters: 9

  • testdata/empty_subpath_echo/go.mod is excluded by !**/testdata/**
  • testdata/empty_subpath_echo/go.sum is excluded by !**/*.sum, !**/testdata/**
  • testdata/empty_subpath_echo/main.go is excluded by !**/testdata/**
  • testdata/empty_subpath_fiber/go.mod is excluded by !**/testdata/**
  • testdata/empty_subpath_fiber/go.sum is excluded by !**/*.sum, !**/testdata/**
  • testdata/empty_subpath_fiber/main.go is excluded by !**/testdata/**
  • testdata/empty_subpath_gin/go.mod is excluded by !**/testdata/**
  • testdata/empty_subpath_gin/go.sum is excluded by !**/*.sum, !**/testdata/**
  • testdata/empty_subpath_gin/main.go is excluded by !**/testdata/**
Files selected for processing: 5

  • generator/security_test.go
  • generator/testdata_empty_subpath_test.go
  • internal/spec/extractor.go
  • internal/spec/extractor_simple_test.go
  • internal/spec/pattern_matchers.go

Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.

Issue #609's acceptance named chi's Route + Get("/") as the control for
an explicit "/". The gin fixture covered that spelling on a group prefix
only; chi reaches it through the mount path (Route and Mount), a separate
join, so it gets its own fixture: /items/, /items/{id} and /users/ keep
their slash.
@ehabterra

ehabterra commented Nov 19, 2026 •

Copy link
Copy Markdown
Owner Author

Re the inconclusive Linked Issues check: the fixtures are under testdata/, which is outside the review path filter, so here is what they cover.

  • testdata/empty_subpath_gin: users.GET("", …) → /api/users, api.Group("") → /api/health, engine-level r.GET("", …) → /, and the explicit-"/" control orgs.GET("/", …) → /api/orgs/.
  • testdata/empty_subpath_echo / empty_subpath_fiber: GET("") / Get("") under a group → /api/articles, /api/items.
  • Added in 890fd1a: testdata/empty_subpath_chi. chi has no empty-path form (a pattern must start with /), so it is the explicit-"/" control the issue named, on the mount path rather than a group: Route("/items"){ Get("/") } → /items/ and Mount("/users", sub) with sub.Get("/") → /users/. That prefix is joined in handleMountNode, a separate join from the group case, so the gin control alone did not pin it.

TestTestdata_EmptySubpath asserts the exact path set for all four.

@ehabterra
ehabterra merged commit f6e1461 into main Nov 19, 2026
6 checks passed
@ehabterra
ehabterra deleted the fix/609-empty-subpath-trailing-slash branch November 19, 2026 22:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

An empty sub-path under a group is documented with a trailing slash: g.GET("", h) on /users becomes /users/

1 participant