2

Our service has two instances. Live is operational and has the URL .../bazaar and beta is for public testing and has the URL .../betabazaar.

We deployed a Swagger UI instanze and changed the url to fetch .../betabazaar/swagger.json which works. But when I use "try out" all requests goes to .../bazaar/...

I assume Swagger uses the @ServicePath parameter to fill the path. For both instances the @ServicePath is /bazaar. Our Nginx proxy server does the job for sending all requests for .../betabazaar/... to to beta instance.

So can I change the URL Swagger UI is using for "try it out" request in Swagger UI? Or do I need to change the @ServicePath in Java?

I already played with window.swaggerUi.setBasePath('/betabazaar'); but this leads to Swagger UI try to fetch http://example.com/api in the first place.

user1482309
  • 289
  • 3
  • 16

2 Answers2

1

The correct call is

window.swaggerUi.setBasePath('/betabazaar');

and needs to be called in onComplete callback.

user1482309
  • 289
  • 3
  • 16
1

You may override the basePath in the onComplete method by calling setBasePath:

window.swaggerUi = new SwaggerUi({
  ...
  onComplete: function(swaggerApi, swaggerUi) {
    swaggerApi.setBasePath('/betabazaar')

    // Alternatively
    swaggerUi.api.setBasePath('/betabazaar')

    // Also this is possible
    window.swaggerUi.api.setBasePath('/betabazaar');
  }
}

It is also possible to override the host (which is what I was looking for by the way).

josemigallas
  • 3,761
  • 1
  • 29
  • 68
  • 1
    It's not working anymore with Swagger UI 4.11.0. At least, there is an error `Uncaught TypeError: swaggerApi is undefined` in the console. Modern Swagger seems to call the onComplete function with no args, and it hasn't got the setBasePath method. – mymedia May 11 '22 at 10:07
  • @mymedia do you found a solution (for swagger UI 4.x)? – Michele Verriello Jul 26 '22 at 11:48