Skip to content

Commit 58272e7

Browse files
committed
update SwaggerUiHome for webflux
1 parent a3f14c3 commit 58272e7

File tree

4 files changed

+136
-3
lines changed

4 files changed

+136
-3
lines changed

springdoc-openapi-webflux-ui/src/main/java/org/springdoc/webflux/ui/SwaggerConfig.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
4242
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
4343
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
44+
import org.springframework.boot.autoconfigure.web.reactive.WebFluxProperties;
4445
import org.springframework.context.annotation.Bean;
4546
import org.springframework.context.annotation.Configuration;
4647
import org.springframework.context.annotation.Lazy;
@@ -90,16 +91,18 @@ SwaggerWelcomeWebFlux swaggerWelcome(SwaggerUiConfigProperties swaggerUiConfig,
9091
SwaggerConfigResource swaggerConfigResource(SwaggerWelcomeCommon swaggerWelcomeCommon){
9192
return new SwaggerConfigResource(swaggerWelcomeCommon);
9293
}
94+
9395
/**
9496
* Swagger ui home swagger ui home.
9597
*
98+
* @param optionalWebFluxProperties the optional web flux properties
9699
* @return the swagger ui home
97100
*/
98101
@Bean
99102
@ConditionalOnMissingBean
100103
@ConditionalOnProperty(name = SPRINGDOC_USE_ROOT_PATH, havingValue = "true")
101-
SwaggerUiHome swaggerUiHome(){
102-
return new SwaggerUiHome();
104+
SwaggerUiHome swaggerUiHome(Optional<WebFluxProperties> optionalWebFluxProperties){
105+
return new SwaggerUiHome(optionalWebFluxProperties);
103106
}
104107

105108
/**

springdoc-openapi-webflux-ui/src/main/java/org/springdoc/webflux/ui/SwaggerUiHome.java

+28-1
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,14 @@
2121
package org.springdoc.webflux.ui;
2222

2323
import java.net.URI;
24+
import java.util.Optional;
2425

2526
import io.swagger.v3.oas.annotations.Operation;
27+
import org.apache.commons.lang3.StringUtils;
2628
import reactor.core.publisher.Mono;
2729

2830
import org.springframework.beans.factory.annotation.Value;
31+
import org.springframework.boot.autoconfigure.web.reactive.WebFluxProperties;
2932
import org.springframework.http.HttpStatus;
3033
import org.springframework.http.server.reactive.ServerHttpResponse;
3134
import org.springframework.stereotype.Controller;
@@ -37,17 +40,41 @@
3740

3841
/**
3942
* Home redirection to swagger api documentation
43+
* @author bnasslahsen
4044
*/
4145
@Controller
4246
public class SwaggerUiHome {
4347

48+
/**
49+
* The Swagger ui path.
50+
*/
4451
@Value(SWAGGER_UI_PATH)
4552
private String swaggerUiPath;
4653

54+
/**
55+
* The Base path.
56+
*/
57+
private String basePath = StringUtils.EMPTY;
58+
59+
/**
60+
* Instantiates a new Swagger ui home.
61+
*
62+
* @param optionalWebFluxProperties the optional web flux properties
63+
*/
64+
public SwaggerUiHome(Optional<WebFluxProperties> optionalWebFluxProperties) {
65+
optionalWebFluxProperties.ifPresent(webFluxProperties -> this.basePath = webFluxProperties.getBasePath());
66+
}
67+
68+
/**
69+
* Index mono.
70+
*
71+
* @param response the response
72+
* @return the mono
73+
*/
4774
@GetMapping(DEFAULT_PATH_SEPARATOR)
4875
@Operation(hidden = true)
4976
public Mono<Void> index(ServerHttpResponse response) {
50-
UriComponentsBuilder uriBuilder = UriComponentsBuilder.fromUriString(swaggerUiPath);
77+
UriComponentsBuilder uriBuilder = UriComponentsBuilder.fromUriString(this.basePath + swaggerUiPath);
5178
response.setStatusCode(HttpStatus.FOUND);
5279
response.getHeaders().setLocation(URI.create(uriBuilder.build().encode().toString()));
5380
return response.setComplete();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
*
3+
* * Copyright 2019-2020 the original author or authors.
4+
* *
5+
* * Licensed under the Apache License, Version 2.0 (the "License");
6+
* * you may not use this file except in compliance with the License.
7+
* * You may obtain a copy of the License at
8+
* *
9+
* * https://www.apache.org/licenses/LICENSE-2.0
10+
* *
11+
* * Unless required by applicable law or agreed to in writing, software
12+
* * distributed under the License is distributed on an "AS IS" BASIS,
13+
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* * See the License for the specific language governing permissions and
15+
* * limitations under the License.
16+
*
17+
*/
18+
19+
package test.org.springdoc.ui.app23;
20+
21+
22+
import org.springframework.web.bind.annotation.GetMapping;
23+
import org.springframework.web.bind.annotation.RequestMapping;
24+
import org.springframework.web.bind.annotation.RestController;
25+
26+
27+
@RequestMapping("/foo/bar")
28+
@RestController
29+
public class HelloController {
30+
31+
@GetMapping("/test")
32+
public String doSomethingInteresting() {
33+
return "OK";
34+
}
35+
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
*
3+
* * Copyright 2019-2020 the original author or authors.
4+
* *
5+
* * Licensed under the Apache License, Version 2.0 (the "License");
6+
* * you may not use this file except in compliance with the License.
7+
* * You may obtain a copy of the License at
8+
* *
9+
* * https://www.apache.org/licenses/LICENSE-2.0
10+
* *
11+
* * Unless required by applicable law or agreed to in writing, software
12+
* * distributed under the License is distributed on an "AS IS" BASIS,
13+
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* * See the License for the specific language governing permissions and
15+
* * limitations under the License.
16+
*
17+
*/
18+
19+
package test.org.springdoc.ui.app23;
20+
21+
import javax.annotation.PostConstruct;
22+
23+
import org.assertj.core.api.Assertions;
24+
import org.junit.jupiter.api.Test;
25+
import reactor.core.publisher.Mono;
26+
import test.org.springdoc.ui.AbstractCommonTest;
27+
28+
import org.springframework.boot.autoconfigure.SpringBootApplication;
29+
import org.springframework.boot.test.context.SpringBootTest;
30+
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
31+
import org.springframework.boot.web.server.LocalServerPort;
32+
import org.springframework.http.HttpStatus;
33+
import org.springframework.web.reactive.function.client.WebClient;
34+
35+
36+
@SpringBootTest(webEnvironment = WebEnvironment.DEFINED_PORT,
37+
properties = { "spring.webflux.base-path=/test",
38+
"springdoc.swagger-ui.use-root-path=true",
39+
"server.port=9029" })
40+
class SpringDocApp23Test extends AbstractCommonTest {
41+
42+
@LocalServerPort
43+
private int port;
44+
45+
private WebClient webClient;
46+
47+
@SpringBootApplication
48+
static class SpringDocTestApp {}
49+
50+
@PostConstruct
51+
void init() {
52+
webClient = WebClient.builder().baseUrl("http://localhost:" + port)
53+
.build();
54+
}
55+
56+
@Test
57+
public void testIndex() throws Exception {
58+
HttpStatus httpStatusRoot = webClient.get().uri("/test/")
59+
.exchangeToMono(clientResponse -> Mono.just(clientResponse.statusCode())).block();
60+
Assertions.assertThat(httpStatusRoot).isEqualTo(HttpStatus.FOUND);
61+
62+
HttpStatus httpStatusMono = webClient.get().uri("/test/swagger-ui.html")
63+
.exchangeToMono(clientResponse -> Mono.just(clientResponse.statusCode())).block();
64+
Assertions.assertThat(httpStatusMono).isEqualTo(HttpStatus.FOUND);
65+
66+
}
67+
}

0 commit comments

Comments
 (0)