springdoc / springdoc-openapi

Library for OpenAPI 3 with spring-boot
https://springdoc.org
Apache License 2.0
3.25k stars 492 forks source link

Proxy Routing Problem When Using Swagger-UI #331

Closed kjc14f closed 4 years ago

kjc14f commented 4 years ago

Hi,

Love the library! Thanks for all the work you have put in!

I've been migrating one of my applications from springfox to springdoc and I've run into a problem involving a Zuul proxy and a Spring cloud microservice using springdoc swagger-ui.

My 2 apps:

When I start the test-app and go to localhost:11111/swagger-ui.html, I am routed to localhost:11111/swagger-ui/index.html?configURL=/v3/api-docs/swagger-config and everything works as expected.

Once the test-app worked I tried to get it to work through the proxy by adding server.forward-headers-strategy: framework to my test-app yaml as per your FAQ in order to get my reverse proxy to work. If I start both the application and the proxy and hit localhost:22222/test-app/swagger-ui.html, I am routed to the correct place and I can see the swagger-ui as expected (I can also see the forwarded prefix coming in okay).

The issue seems to arrise when using a combination of the 2 approaches. Sometimes I want to hit the swagger through the proxy, othertimes by hitting the service direct. It seems however whichever method I do first is the only one that works because it seems to cache the URL.

E.g. If the first call after startup is to the proxy (localhost:22222/test-app/swagger-ui.html) that works fine. If I then hit (localhost:11111/swagger-ui.html) afterwards it takes me to localhost:11111/swagger-ui/index.html?configURL=/test-app/v3/api-docs/swagger-config . Notice that /test-app/ is prefixed to the configURL like it is still coming from the proxy even though it isn't, this obviously gives 'Failed to load remote comfiguration'.

I stepped into the code a bit and I can see this seems to be from the method buildConfigUrl() in SwaggerWelcome.java. It has the following statement if (StringUtils.isEmpty(swaggerUiConfig.getConfigUrl())) This always returns false after hitting the swagger the first time but the buildConfigUrl is incorrect because it is that of the proxy.

Is there a way to stop springdoc doing this and to make it recall the buildConfigUrl() each time?

Thanks for the help! :)

bnasslahsen commented 4 years ago

Hi @kjc14f,

The behaviour you are describing will be corrected in order to recall buildConfigUrl each time. Also, in order to have the correct url on the servers list, you will have to set the following property:

springdoc.cache.disabled= true

All this will be available with next release: v1.2.26.

kjc14f commented 4 years ago

Thanks @bnasslahsen, that sounds perfect! Thanks for the quick response :)