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

Incorrect generated Server URL (always returns cached url based on first incoming request after app startup) #1031

Closed
kardashov opened this issue Jan 21, 2021 · 3 comments
Labels
bug Something isn't working

Comments

@kardashov
Copy link

kardashov commented Jan 21, 2021

Bug description
This issue was already described in #283
The first time the /v3/api-docs endpoint is called, the server servers[0].url property is calculated and after that it is used from a cache for all subsequent calls (direct, via SwaggerUI etc.).
This issue will not manifest itself when there is only one valid path to your application (e.g. via reverse proxy url https://mydomain.com). But when there are multiple paths (like public https + plaintext http requests from internal infrastructure) this problem arises.

To Reproduce
Start the application:
send request №1:

curl --location --request GET 'http://localhost:8080/v3/api-docs' \
--header 'X-Forwarded-Proto: https' \
--header 'X-Forwarded-Host: myhost.com'

in resulting json generated servers entry is correct servers[0].url=https://myhost.com .

Send request №2:

curl --location --request GET 'http://localhost:8080/v3/api-docs' 

Expected: In generated json spec servers[0].url=http://localhost:8080 .
Actual: In generated json spec still servers[0].url=https://myhost.com

Now RESTART application and send previous requests in REVERSE ORDER (request №2, then request №1).
This time request №2 will generate correct servers[0].url=http://localhost:8080
But subsequent request №1 also shows http://localhost:8080 when https://myhost.com expected.

Expected behavior
Correct servers.url property generated in openapi.json for each incoming request (based on request's x-forwarded-* headers or their absense) not only the first one.

Additional context
SpringDoc version used 1.5.2
Changing server.forward-headers-strategy Spring property and other tricks from FAQ do not affect this behavior.
Situation even more confusing when there are multiple Api Groups defined. They will cache servers[].url property independently from each other.

@kguelzau
Copy link

kguelzau commented Apr 15, 2021

This is reproducible since 1.4.4 and was introduced with 26458dd

So since 1.4.4 the servers of a cachedOpenAPI are never updated.
updateServers() sets the servers of the cachedOpenAPI so setServersPresent(true) is called which prevents any action in updateServers()

if (!CollectionUtils.isEmpty(openAPIBuilder.getCachedOpenAPI().getServers()))
  openAPIBuilder.setServersPresent(true);
			openApi = openAPIBuilder.updateServers(openAPIBuilder.getCachedOpenAPI());

@bnasslahsen from my perspective I don't think this is intended behaviour and should be fixed.

Disabling the cache as mentioned in the faq since springdoc/springdoc.github.io@7fb2523 is not an option because of performance reasons.

So long we stick with version 1.4.3

bnasslahsen added a commit that referenced this issue Apr 15, 2021
@bnasslahsen
Copy link
Contributor

@kguelzau,

I have added a fix for it.

@kguelzau
Copy link

wow, this was fast 👍

@bnasslahsen bnasslahsen added the bug Something isn't working label Jan 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants