-
-
Notifications
You must be signed in to change notification settings - Fork 504
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
Calling Swagger UI via different context paths fails #2642
Comments
I have the same problem. I used 2.1.0 version. But now is 2.6.0. And without any changes. As I understand, the problem is openapi caches swagger-initializer.js. It is easy to reproduce. At first you need to be shure your application.yml has property |
This description is quite unclear. Try adding a failing test similar to what is available here, to showcase the issue you are facing: This ticket will be closed, but can be reopened if your provide all the elements. |
@bnasslahsen Sure, no problem. @Test
public void shouldReturnCorrectInitializerJSWhenChangingForwardedPrefixHeader() throws Exception {
// Request swagger-initializer.js with some X-Forwarded-Prefix header.
mockMvc.perform(get("/swagger-ui/swagger-initializer.js")
.header("X-Forwarded-Prefix", X_FORWARD_PREFIX));
// When performing a second request with another X-Forwarded-Prefix header
// the changed path should be reflected in the configUrl.
mockMvc.perform(get("/swagger-ui/swagger-initializer.js")
.header("X-Forwarded-Prefix", "/path/prefix2"))
.andExpect(status().isOk())
.andExpect(content().string(
containsString("\"configUrl\" : \"/path/prefix2/v3/api-docs/swagger-config\",")
));
} When adding this to To sum up: A request to |
I have added a fix for this request. |
Thank you @bnasslahsen . This fixes the described problem for sequential requests, which is a good progress. Unfortunately there is still a problem when running requests with different context paths concurrently. I could indeed write a test which sometimes fails due to concurrency issues: @Test
public void shouldReturnCorrectInitializerJSWhenChangingForwardedPrefixHeader() throws Exception {
var tasks = IntStream.range(0, 100).mapToObj(i -> CompletableFuture.runAsync(() -> {
try {
mockMvc.perform(get("/swagger-ui/swagger-initializer.js")
.header("X-Forwarded-Prefix", "/path/prefix" + i))
.andExpect(status().isOk())
.andExpect(content().string(
containsString("\"configUrl\" : \"/path/prefix" + i + "/v3/api-docs/swagger-config\",")
));
} catch (Exception e) {
throw new RuntimeException(e);
}
})).toArray(CompletableFuture<?>[]::new);
CompletableFuture.allOf(tasks).join();
} Test error:
I guess this is due to The @Test
public void shouldCalculateUrlsBehindProxyWhenChangingForwardedPrefixHeader() {
var tasks = IntStream.range(0, 100).mapToObj(i -> CompletableFuture.runAsync(() -> {
try {
mockMvc.perform(get("/v3/api-docs/swagger-config")
.header("X-Forwarded-Prefix", X_FORWARD_PREFIX + i))
.andExpect(status().isOk())
.andExpect(jsonPath("url",
equalTo("/path/prefix" + i + "/v3/api-docs")
))
.andExpect(jsonPath("configUrl",
equalTo("/path/prefix" + i + "/v3/api-docs/swagger-config")
));
} catch (Exception e) {
throw new RuntimeException(e);
}
})).toArray(CompletableFuture<?>[]::new);
CompletableFuture.allOf(tasks).join();
} Test error:
|
This one forces more changes :) |
Requesting the Swagger UI from two different public URLs having different context paths causes the second UI initialization to fail. The second UI call will have a wrong path written in
swagger-initializer.js
:configUrl
is based on the path from the first request.To Reproduce
Setup a reverse proxy (for example at localhost:80) in front of a backend service BS (for example at localhost:8080) which adds a prefix to all the backend service's URLs. So the backend service endpoints are available at
http://localhost:80/service/**
(reverse proxy with path prefix/service
) andhttp://localhost:8080/**
(backend service).The backend service should be configured to provide a Swagger UI.
The following scenarios, each executed on a freshly restarted backend service, reproduce the problem:
I'm using
springdoc-openapi-starter-webmvc-ui
version2.6.0
.Reason
SwaggerIndexPageTransformer
initializes the singletonSwaggerUiConfigParameters
by callingswaggerWelcomeCommon.buildFromCurrentContextPath(request)
if the configuration'sconfigUrl
isnull
. TheconfigUrl
(and perhaps some other properties) is derived from the current request's context path.Subsequent requests, even if they have a different context path, will use the initialized configuration without re-evaluating it.
The browser will then try to load the configuration from an URL which is not available.
Expected behavior
The
configUrl
should be re-evaluated (if necessary) to generate a correctswagger-initializer.js
.Ideas
Perhaps the
SwaggerUiConfigParameters
shouldn't be modified at all by the transformer.This is to avoid concurrency problems caused by a hypothetical re-evaluation overwriting the configuration.
I'm not sure whether
SwaggerUiConfigParameters.configUrl
is a relevant public API for anybody. Changing or removing it is probably a breaking change.The text was updated successfully, but these errors were encountered: