Swagger-ui: Detecting default server url

Created on 20 Jul 2018  路  6Comments  路  Source: swagger-api/swagger-ui

Is your feature request related to a problem?

We have different server urls: localhost, review apps, staging and production. Everytime one uses
swagger ui, they have to pick correct server first. Currently default server url is the first configured, this can be confusing since swagger is sending request somewhere else, getting one to think something is wrong with the current url.

Describe the solution you'd like

It would be intuitive if server url was picked to match the browser url from where
swagger ui is served (if it does match).

try-it-out 3.x enhancement 3.x

Most helpful comment

Couldn't wrap my head around this wrapping business, so I have a rather dirty hack in place to preselect a certain server from the list by programmatically triggering the select box.

Would appreciate a option to pre-select a server via maybe by the URL or a user-defined function that receives the servers array and returns a index to select.

All 6 comments

What about maintaining a list of links to different environments, e.g.

It's hard to implement that, since review apps post different url for each PR, changing heroku github bot to append the correct url is probably not possible - I doubt it allows configuration.

What's prevent swagger ui to do the right thing? The algorithm would be:

1) go over list of servers, top to bottom
2) for each item, check if it matches prefix of browser url
3) if they match, pick as default and stop
4) if none of the urls in list matches, pick the first server as default

Are you using a shared instance like http://swagger-ui.your-project.com? If you do so, you can customize it.

If I'm getting this right, you can write your own logic to place these lines: https://github.com/swagger-api/swagger-ui/blob/v3.17.4/dist/index.html#L42-L54.

I'm not following what configuration exactly should change this. I have:

const ui = SwaggerUIBundle({
聽 url: "https://custom-app-pr-61.herokuapp.com/api/v1/openapi.yaml",
聽 dom_id: '#swagger-ui',
聽 deepLinking: true,
聽 presets: [
聽  SwaggerUIBundle.presets.apis,
聽  SwaggerUIStandalonePreset
聽 ],
聽 plugins: [
聽  SwaggerUIBundle.plugins.DownloadUrl
聽 ],
聽 layout: "StandaloneLayout"
})

But regardless of url, it still picks http://localhost as default.

So you have multiple servers for several environment in the same api document?

In such case, yes, the first server will be selected by default: https://github.com/swagger-api/swagger-ui/blob/v3.17.4/src/core/plugins/oas3/components/servers.jsx#L25

You could write a wrapComponent plugin to override this behavior. Refs:

Or alternatively, wrap the selector: https://github.com/swagger-api/swagger-ui/blob/v3.17.4/src/core/plugins/oas3/selectors.js#L20

Couldn't wrap my head around this wrapping business, so I have a rather dirty hack in place to preselect a certain server from the list by programmatically triggering the select box.

Would appreciate a option to pre-select a server via maybe by the URL or a user-defined function that receives the servers array and returns a index to select.

Was this page helpful?
0 / 5 - 0 ratings