scottie1984 / swagger-ui-express

Adds middleware to your express app to serve the Swagger UI bound to your Swagger document. This acts as living documentation for your API hosted from within your app.
MIT License
1.43k stars 226 forks source link

Allow setting relative path for oauth2RedirectUrl #211

Closed mgagliardo91 closed 2 years ago

mgagliardo91 commented 4 years ago

Currently, SwaggerUI accepts an argument (which is passed via swaggerOptions) to set oauth2RedirectUrl. If this value is not set, it will default to ${window.location.protocol}//${window.location.host}/oauth2-redirect.html.

I would like to expose Swagger UI Express at the route /api-docs. Unfortunately this means, without setting the oauth2RedirectUrl manually, that the redirect path will still be {host}:{port}/oauth2-redirect.html and the swagger-ui-express middleware will not catch the redirect route (since it binds its routes to /api-docs).

One way around this is to explicitly set the oauth2RedirectUrl in the swaggerOptions payload to {host}:{port}/api-docs/oauth2-redirect.html; however, this requires the express server to know its hostname ahead of time where as I would prefer to keep it ignorant to its hostname and keep everything relative.

I am proposing having swagger-ui-express override the oauth2RedirectUrl to the route its running behind (i.e. /api-docs). You can take this a step further and make it configurable, but just having it on a non-root route without having to explicitly call out the hostname would be ideal.

An alternative would be to allow passing a relative path for oauth2RedirectUrl and formatting it with the window.location as swagger-ui does before its passed to the swagger ui bundle.

Thanks!

scottie1984 commented 2 years ago

What is I did this on the client?

customOptions.oauth2RedirectUrl = customOptions.oauth2RedirectUrl || window.location.href + 'oauth2-redirect.html'

Essentially setting the oauth2RedirectUrl property automatically to the window href if now defined.

Edalbrelord commented 2 years ago

Referring to https://github.com/swagger-api/swagger-ui/pull/7476 Swagger UI is updated, the default oauth2RedirectUrl will also include sub-pages. I see swagger-ui dependency is still 4.3.0, can this be updated? After that we can fall back to the default redirect-url to solve this issue.

scottie1984 commented 2 years ago

Released: https://github.com/scottie1984/swagger-ui-express/releases/tag/4.4.0