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

allow override of host and basePath #532

Closed
fehguy opened this issue Jul 22, 2015 · 13 comments
Closed

allow override of host and basePath #532

fehguy opened this issue Jul 22, 2015 · 13 comments
Assignees
Milestone

Comments

@fehguy
Copy link
Contributor

fehguy commented Jul 22, 2015

Once swagger-client is loaded, you cannot easily override the host or basePath values as they are copied to each operation. While you can currently iterate over each api + host, it's a pain.

@jifka
Copy link

jifka commented Aug 31, 2016

Bump - could someone explain how to use this feature to change the URL/basePath on the fly?

I've tried creating a SwaggerClient and called setHost and setBasePath and the UI still uses the url/path defined in the swagger.json.

@fehguy
Copy link
Contributor Author

fehguy commented Sep 7, 2016

OK @jifka it sounds like you're trying to override those in the context of the UI. If that's true, you'll need to get the swagger-js instance that the UI is using, and call it from there. For example, I open http://petstore.swagger.io and enter this in the console:

window.swaggerUi.api.setBasePath('/v3');

The calls will now reflect that basePath.

@jifka
Copy link

jifka commented Sep 7, 2016

I'm actually more interested in setting the URL to execute calls against a different server, instead of the URL defined in the yaml/json definition of the API. This is supposed to be thru the 'host' but doesn't seem to work.

@fehguy
Copy link
Contributor Author

fehguy commented Sep 8, 2016

Hi, I'm confused. by setting the basePath like I suggested, you can load the swagger file from one location and execute methods against a different one, just as if you had set the host parameter. It does work, so if you can share more info we can help troubleshoot it.

@jifka
Copy link

jifka commented Sep 8, 2016

Like the OP stated, I'm also looking for a way to dynamically set the host URL without reloading the API definition yaml/jason.

I now see that there's a function on swaggerUi.api.setHost() that will change the endpoint on the fly so I'll need to update the UI to allow users to select their own host.

@fehguy
Copy link
Contributor Author

fehguy commented Sep 8, 2016

So my suggestion from #532 (comment) should do just that. You can let the user choose which host and trigger logic like what I mentioned. Then you can dynamically target different hosts as needed

@pcontezini
Copy link

Hi.
I'm trying to create a select box so my user can choose between production and test environment.
My code looks like this:

function addBaseUrl() {
var url = $('#input_baseUrl').val();
if (url && url.trim() != "") {
window.swaggerUi.api.setHost(url);
log("host: " + url);
}

  }

$('#input_baseUrl').change(addBaseUrl);

But it is simply not working, when I inspect the swaggerUi object, they are all with my new host, but when I try to do the call, it always calls the original host. I've trying calling swaggerUi.api.build() and the returned object also seems fine.

I'm thinking this is a bug, so i'm reporting it here.

I'm attaching the full index.html file, maybe it helps.

index.txt

@dnascimento
Copy link

@pcontezini can you share the link to your branch of Swagger UI?

@pcontezini
Copy link

@k11k2
Copy link

k11k2 commented Apr 21, 2017

const ui = SwaggerUIBundle({
                    url: `url1` ,
                    dom_id: '#swagger-ui-1',
                    presets: [
                  SwaggerUIBundle.presets.apis,
                  // yay ES6 modules ↘
                  Array.isArray(SwaggerUIStandalonePreset) ? SwaggerUIStandalonePreset : SwaggerUIStandalonePreset.default
                    ],
                    plugins: [
                      SwaggerUIBundle.plugins.DownloadUrl
                    ],

                    layout: "StandaloneLayout"

                })

how could i achieve this setBasePath/setHost functionality here..

@webron
Copy link
Contributor

webron commented Apr 21, 2017

It's not supported in the new client. Please file a new ticket.

@char0n
Copy link
Member

char0n commented Apr 30, 2020

Addressing this issue should now be possible purely from swagger-ui by UrlMutatorPlugin.

@Padmahas
Copy link

OK @jifka it sounds like you're trying to override those in the context of the UI. If that's true, you'll need to get the swagger-js instance that the UI is using, and call it from there. For example, I open http://petstore.swagger.io and enter this in the console:

window.swaggerUi.api.setBasePath('/v3');

The calls will now reflect that basePath.

Hi @fehguy , which console do you mean? Browser console? Which will show up when F12 key is pressed?
I opened the URL you gave and executed the command you showed, to change the basePath, but I'm getting the below error "Uncaught TypeError: Cannot read property 'api' of undefined
at :1:18
(anonymous) @ VM51:1"

I think I'm executing this in wrong console. which console do you mean?

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

No branches or pull requests

8 participants