Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
ArFe opened this issue Apr 13, 2022 · 3 comments
Closed

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

ArFe opened this issue Apr 13, 2022 · 3 comments

Comments

@ArFe
Copy link
Contributor

ArFe commented Apr 13, 2022

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

@ArFe
Copy link
Contributor Author

ArFe commented Apr 21, 2022

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

@casualjim
Copy link
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

@fredbi
Copy link
Member

fredbi commented Dec 7, 2023

Closing: #239 has been merged

@fredbi fredbi closed this as completed Dec 7, 2023
fredbi added a commit to fredbi/runtime that referenced this issue Dec 11, 2023
- refactored UI middleware
  * factorized chore middleware to remove duplicated code
  * factorized UI middleware options: to avoid breaking changes in the
    options types, there is a decode/encode to a common structure
  * added more options:
    * allows to fully customize the UI template
  * added more unit tests

- Spec middleware: added support for optional SpecOption argument
  * allows to serve the spec from a custom path / document name

- serving with or without trailing "/" (cf. issue go-openapi#238)
  * replaced path.Join() by path.Clean(), which is the intended behavior
    (i.e. serve the path, irrespective of the presence of a trailing
    slash)
  * generalized this behavior to all UI and Spec middleware, not just
    swaggerUI

- API Context:
  * exposed middleware to serve RapiDoc UI
  * allowed new UIOption (...UIOption) to the APIHandler, etc middleware
  * coordinated UI / Spec middleware to be consistent when non-default
    path/document URL is served

* fixes go-openapi#192
* fixes go-openapi#226

Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
fredbi added a commit to fredbi/runtime that referenced this issue Dec 11, 2023
- refactored UI middleware
  * factorized chore middleware to remove duplicated code
  * factorized UI middleware options: to avoid breaking changes in the
    options types, there is a decode/encode to a common structure
  * added more options:
    * allows to fully customize the UI template
  * added more unit tests

- Spec middleware: added support for optional SpecOption argument
  * allows to serve the spec from a custom path / document name

- serving with or without trailing "/" (cf. issue go-openapi#238)
  * replaced path.Join() by path.Clean(), which is the intended behavior
    (i.e. serve the path, irrespective of the presence of a trailing
    slash)
  * generalized this behavior to all UI and Spec middleware, not just
    swaggerUI

- API Context:
  * exposed middleware to serve RapiDoc UI
  * allowed new UIOption (...UIOption) to the APIHandler, etc middleware
  * coordinated UI / Spec middleware to be consistent when non-default
    path/document URL is served

* fixes go-openapi#192
* fixes go-openapi#226

Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
fredbi added a commit that referenced this issue Dec 12, 2023
- refactored UI middleware
  * factorized chore middleware to remove duplicated code
  * factorized UI middleware options: to avoid breaking changes in the
    options types, there is a decode/encode to a common structure
  * added more options:
    * allows to fully customize the UI template
  * added more unit tests

- Spec middleware: added support for optional SpecOption argument
  * allows to serve the spec from a custom path / document name

- serving with or without trailing "/" (cf. issue #238)
  * replaced path.Join() by path.Clean(), which is the intended behavior
    (i.e. serve the path, irrespective of the presence of a trailing
    slash)
  * generalized this behavior to all UI and Spec middleware, not just
    swaggerUI

- API Context:
  * exposed middleware to serve RapiDoc UI
  * allowed new UIOption (...UIOption) to the APIHandler, etc middleware
  * coordinated UI / Spec middleware to be consistent when non-default
    path/document URL is served

* fixes #192
* fixes #226

Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
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

No branches or pull requests

3 participants