Skip to content

Swagger Middleware drops trailing slash and can't find path #238

Closed

Description

@ArFe

While joining the paths together opts.BasePath and opts.Path, path.Join drops the trailing slash.

pth := path.Join(opts.BasePath, opts.Path)

So, a /example/ui/ becomes /example/ui, and I get the error
"/example/ui" not found if I try to access /example/ui/

This is a problem because other languages use the trailing / as a pattern (for example python as here http://thomaxxl.pythonanywhere.com/api/ ), and we have webserver that use swagger in different languages.

There are 2 solutions, in my opinion:

  1. [Preferred/Easiest] compare apples to apples and submit r.URL.Path to path.Join as well here:
    if r.URL.Path == pth {

    if path.Join(r.URL.Path) == pth {
  2. do not use path.Join to join the opts.BasePath and opts.Path, just concatenate them

I can submit a PR for option 1 if you agree.

PS. It seems go is planning to have it's own JoinPath() inside net/url, and that could fix the problem in the future:
golang/go#47005
golang/go#52074

Activity

  1. ArFe commented on Apr 21, 2022

    @ArFe
    ContributorAuthor

    @casualjim, would it be ok for me to open a PR with the above change?
    It should not change anything that is working today.

  2. casualjim commented on Apr 22, 2022

    @casualjim
    Member

    Yes a PR would be nice. I too prefer option 1

    There is no 1 right answer to whether the trailing slash should be or not be on the path, but once you pick one it should be consistent. Some servers want the trailing slash others reject the trailing slash

  3. fredbi commented on Dec 7, 2023

    @fredbi
    Member

    Closing: #239 has been merged

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions