Skip to content

Commit b057b2a

Browse files
authored
Merge pull request #2970 from lagoshny/ISSUE-2969
ISSUE-2969 fix path to register resource handler to work SwaggerIndexPageTransformer considering /webjar path prefix
2 parents 367f1c6 + 6c3ac25 commit b057b2a

File tree

3 files changed

+103
-2
lines changed

3 files changed

+103
-2
lines changed

springdoc-openapi-starter-webflux-ui/src/main/java/org/springdoc/webflux/ui/SwaggerWebFluxConfigurer.java

+10-2
Original file line numberDiff line numberDiff line change
@@ -103,16 +103,24 @@ public void addResourceHandlers(ResourceHandlerRegistry registry) {
103103
uiRootPath.append(actuatorProvider.get().getBasePath());
104104

105105
String webjarsPrefix = springDocConfigProperties.getWebjars().getPrefix();
106-
String resourcePath,swaggerUiPrefix;
106+
String resourcePath,swaggerUiPrefix,swaggerUiWebjarsPrefix;
107107

108108
if (DEFAULT_WEB_JARS_PREFIX_URL.equals(webjarsPrefix)) {
109109
swaggerUiPrefix = SWAGGER_UI_PREFIX;
110-
resourcePath = webjarsPrefix + SWAGGER_UI_PREFIX + DEFAULT_PATH_SEPARATOR + swaggerUiConfigProperties.getVersion() + DEFAULT_PATH_SEPARATOR;
110+
resourcePath = webjarsPrefix + SWAGGER_UI_PREFIX + DEFAULT_PATH_SEPARATOR + swaggerUiConfigProperties.getVersion() + DEFAULT_PATH_SEPARATOR;
111+
swaggerUiWebjarsPrefix = webjarsPrefix + swaggerUiPrefix;
111112
} else {
112113
swaggerUiPrefix = webjarsPrefix;
113114
resourcePath = DEFAULT_WEB_JARS_PREFIX_URL + DEFAULT_PATH_SEPARATOR;
115+
swaggerUiWebjarsPrefix = swaggerUiPrefix;
114116
}
115117

118+
registry.addResourceHandler(uiRootPath + swaggerUiWebjarsPrefix + ALL_PATTERN)
119+
.addResourceLocations(CLASSPATH_RESOURCE_LOCATION + resourcePath)
120+
.resourceChain(false)
121+
.addResolver(swaggerResourceResolver)
122+
.addTransformer(swaggerIndexTransformer);
123+
116124
registry.addResourceHandler(uiRootPath + swaggerUiPrefix + ALL_PATTERN)
117125
.addResourceLocations(CLASSPATH_RESOURCE_LOCATION + resourcePath)
118126
.resourceChain(false)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
*
3+
* *
4+
* * *
5+
* * * * Copyright 2019-2020 the original author or authors.
6+
* * * *
7+
* * * * Licensed under the Apache License, Version 2.0 (the "License");
8+
* * * * you may not use this file except in compliance with the License.
9+
* * * * You may obtain a copy of the License at
10+
* * * *
11+
* * * * https://www.apache.org/licenses/LICENSE-2.0
12+
* * * *
13+
* * * * Unless required by applicable law or agreed to in writing, software
14+
* * * * distributed under the License is distributed on an "AS IS" BASIS,
15+
* * * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* * * * See the License for the specific language governing permissions and
17+
* * * * limitations under the License.
18+
* * *
19+
* *
20+
*
21+
*
22+
*/
23+
24+
package test.org.springdoc.ui.app34;
25+
26+
import jakarta.validation.Valid;
27+
import jakarta.validation.constraints.Size;
28+
import org.springframework.web.bind.annotation.GetMapping;
29+
import org.springframework.web.bind.annotation.RequestParam;
30+
import org.springframework.web.bind.annotation.RestController;
31+
32+
@RestController
33+
public class HelloController {
34+
35+
@GetMapping(value = "/persons")
36+
public void persons(@Valid @RequestParam @Size(min = 4, max = 6) String name) {
37+
38+
}
39+
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
*
3+
* *
4+
* * *
5+
* * * * Copyright 2019-2020 the original author or authors.
6+
* * * *
7+
* * * * Licensed under the Apache License, Version 2.0 (the "License");
8+
* * * * you may not use this file except in compliance with the License.
9+
* * * * You may obtain a copy of the License at
10+
* * * *
11+
* * * * https://www.apache.org/licenses/LICENSE-2.0
12+
* * * *
13+
* * * * Unless required by applicable law or agreed to in writing, software
14+
* * * * distributed under the License is distributed on an "AS IS" BASIS,
15+
* * * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* * * * See the License for the specific language governing permissions and
17+
* * * * limitations under the License.
18+
* * *
19+
* *
20+
*
21+
*
22+
*/
23+
24+
package test.org.springdoc.ui.app34;
25+
26+
import org.junit.jupiter.api.Test;
27+
import org.springframework.boot.autoconfigure.SpringBootApplication;
28+
import org.springframework.test.context.TestPropertySource;
29+
import org.springframework.test.web.reactive.server.EntityExchangeResult;
30+
import test.org.springdoc.ui.AbstractSpringDocTest;
31+
32+
import static org.junit.jupiter.api.Assertions.assertFalse;
33+
import static org.junit.jupiter.api.Assertions.assertTrue;
34+
35+
@TestPropertySource(properties = "springdoc.swagger-ui.disable-swagger-default-url=true")
36+
public class SpringDocApp34Test extends AbstractSpringDocTest {
37+
38+
@Test
39+
void transformed_index_with_oauth() throws Exception {
40+
EntityExchangeResult<byte[]> getResult = webTestClient.get().uri("/webjars/swagger-ui/swagger-initializer.js")
41+
.exchange()
42+
.expectStatus().isOk()
43+
.expectBody().returnResult();
44+
45+
var responseContent = new String(getResult.getResponseBody());
46+
assertFalse(responseContent.contains("https://petstore.swagger.io/v2/swagger.json"));
47+
assertTrue(responseContent.contains("/v3/api-docs"));
48+
}
49+
50+
@SpringBootApplication
51+
static class SpringDocTestApp {}
52+
53+
}

0 commit comments

Comments
 (0)