Skip to content

Commit 6c86b61

Browse files
committed
Incorrect configUrl due to null path prefix. Fixes #1364
1 parent d627397 commit 6c86b61

File tree

11 files changed

+189
-30
lines changed

11 files changed

+189
-30
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424

2525
import com.fasterxml.jackson.databind.ObjectMapper;
2626
import org.springdoc.core.ActuatorProvider;
27-
import org.springdoc.core.OpenAPIService;
2827
import org.springdoc.core.SpringDocConfigProperties;
2928
import org.springdoc.core.SpringDocConfiguration;
3029
import org.springdoc.core.SwaggerUiConfigParameters;
@@ -39,6 +38,7 @@
3938
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
4039
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
4140
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
41+
import org.springframework.context.ApplicationContext;
4242
import org.springframework.context.annotation.Bean;
4343
import org.springframework.context.annotation.Configuration;
4444
import org.springframework.context.annotation.Lazy;
@@ -64,14 +64,14 @@ public class SwaggerConfig {
6464
* @param swaggerUiConfig the swagger ui config
6565
* @param springDocConfigProperties the spring doc config properties
6666
* @param swaggerUiConfigParameters the swagger ui config parameters
67-
* @param openAPIService the open api service
67+
* @param applicationContext the application context
6868
* @return the swagger welcome web mvc
6969
*/
7070
@Bean
7171
@ConditionalOnMissingBean
7272
@ConditionalOnProperty(name = SPRINGDOC_USE_MANAGEMENT_PORT, havingValue = "false", matchIfMissing = true)
73-
SwaggerWelcomeWebMvc swaggerWelcome(SwaggerUiConfigProperties swaggerUiConfig, SpringDocConfigProperties springDocConfigProperties, SwaggerUiConfigParameters swaggerUiConfigParameters, OpenAPIService openAPIService) {
74-
return new SwaggerWelcomeWebMvc(swaggerUiConfig, springDocConfigProperties,swaggerUiConfigParameters, openAPIService);
73+
SwaggerWelcomeWebMvc swaggerWelcome(SwaggerUiConfigProperties swaggerUiConfig, SpringDocConfigProperties springDocConfigProperties, SwaggerUiConfigParameters swaggerUiConfigParameters, ApplicationContext applicationContext) {
74+
return new SwaggerWelcomeWebMvc(swaggerUiConfig, springDocConfigProperties,swaggerUiConfigParameters, applicationContext);
7575
}
7676

7777
/**

springdoc-openapi-ui/src/main/java/org/springdoc/webmvc/ui/SwaggerWelcomeWebMvc.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@
2424

2525
import io.swagger.v3.oas.annotations.Operation;
2626
import org.apache.commons.lang3.StringUtils;
27-
import org.springdoc.core.OpenAPIService;
2827
import org.springdoc.core.SpringDocConfigProperties;
2928
import org.springdoc.core.SwaggerUiConfigParameters;
3029
import org.springdoc.core.SwaggerUiConfigProperties;
3130

3231
import org.springframework.beans.factory.annotation.Value;
32+
import org.springframework.context.ApplicationContext;
3333
import org.springframework.http.ResponseEntity;
3434
import org.springframework.stereotype.Controller;
3535
import org.springframework.web.bind.annotation.GetMapping;
@@ -62,18 +62,18 @@ public class SwaggerWelcomeWebMvc extends SwaggerWelcomeCommon {
6262
/**
6363
* The Open api service.
6464
*/
65-
private final OpenAPIService openAPIService;
65+
private final ApplicationContext applicationContext;
6666

6767
/**
6868
* Instantiates a new Swagger welcome web mvc.
6969
* @param swaggerUiConfig the swagger ui config
7070
* @param springDocConfigProperties the spring doc config properties
7171
* @param swaggerUiConfigParameters the swagger ui config parameters
72-
* @param openAPIService the open api service
72+
* @param applicationContext the application context
7373
*/
74-
public SwaggerWelcomeWebMvc(SwaggerUiConfigProperties swaggerUiConfig, SpringDocConfigProperties springDocConfigProperties, SwaggerUiConfigParameters swaggerUiConfigParameters, OpenAPIService openAPIService) {
74+
public SwaggerWelcomeWebMvc(SwaggerUiConfigProperties swaggerUiConfig, SpringDocConfigProperties springDocConfigProperties, SwaggerUiConfigParameters swaggerUiConfigParameters, ApplicationContext applicationContext) {
7575
super(swaggerUiConfig, springDocConfigProperties, swaggerUiConfigParameters);
76-
this.openAPIService = openAPIService;
76+
this.applicationContext = applicationContext;
7777
}
7878

7979
/**
@@ -124,7 +124,7 @@ protected String buildUrl(String contextPath, final String docsUrl) {
124124
@Override
125125
protected String buildApiDocUrl() {
126126
if (this.pathPrefix == null)
127-
this.pathPrefix = findPathPrefix(this.openAPIService, this.springDocConfigProperties);
127+
this.pathPrefix = findPathPrefix(this.applicationContext, this.springDocConfigProperties);
128128
return buildUrl(contextPath + pathPrefix, springDocConfigProperties.getApiDocs().getPath());
129129
}
130130

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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.app22;
20+
21+
import org.junit.jupiter.api.Test;
22+
import test.org.springdoc.ui.AbstractSpringDocTest;
23+
24+
import org.springframework.test.context.TestPropertySource;
25+
26+
import static org.hamcrest.CoreMatchers.equalTo;
27+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
28+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
29+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
30+
31+
@TestPropertySource(properties ={
32+
"springdoc.api-docs.enabled=false",
33+
"springdoc.api-docs.path=/api-docs",
34+
"springdoc.swagger-ui.url=/api-docs/xxx/v1/openapi.yml",
35+
} )
36+
public class SpringDocApp22Test extends AbstractSpringDocTest {
37+
38+
@Test
39+
public void test_apidocs_disabled() throws Exception {
40+
mockMvc.perform(get("/api-docs/swagger-config"))
41+
.andExpect(status().isOk())
42+
.andExpect(jsonPath("url", equalTo("/api-docs/xxx/v1/openapi.yml")))
43+
.andExpect(jsonPath("configUrl", equalTo("/api-docs/swagger-config")))
44+
.andExpect(jsonPath("validatorUrl", equalTo("")))
45+
.andExpect(jsonPath("oauth2RedirectUrl", equalTo("http://localhost/swagger-ui/oauth2-redirect.html")));
46+
}
47+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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.app22;
20+
21+
import org.springdoc.core.SpringDocConfigProperties;
22+
import org.springdoc.core.SpringDocConfiguration;
23+
24+
import org.springframework.boot.SpringApplication;
25+
import org.springframework.boot.autoconfigure.SpringBootApplication;
26+
import org.springframework.context.annotation.Bean;
27+
28+
@SpringBootApplication
29+
public class SpringDocTestApp {
30+
31+
public static void main(String[] args) {
32+
SpringApplication.run(SpringDocTestApp.class, args);
33+
}
34+
35+
@Bean
36+
SpringDocConfiguration springDocConfiguration(){
37+
return new SpringDocConfiguration();
38+
}
39+
40+
@Bean
41+
public SpringDocConfigProperties springDocConfigProperties() {
42+
return new SpringDocConfigProperties();
43+
}
44+
45+
}

springdoc-openapi-webflux-core/src/main/java/org/springdoc/webflux/api/OpenApiWebfluxResource.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,19 +145,18 @@ public Mono<String> openapiYaml(ServerHttpRequest serverHttpRequest,
145145
@Override
146146
protected String getServerUrl(ServerHttpRequest serverHttpRequest, String apiDocsUrl) {
147147
String requestUrl = decode(serverHttpRequest.getURI().toString());
148-
final String prefix = findPathPrefix(this.openAPIService, this.springDocConfigProperties);
148+
final String prefix = findPathPrefix(this.openAPIService.getContext(), this.springDocConfigProperties);
149149
return requestUrl.substring(0, requestUrl.length() - apiDocsUrl.length()- prefix.length());
150150
}
151151

152152
/**
153153
* Finds path prefix.
154154
*
155-
* @param openAPIService the open api service
155+
* @param applicationContext the application context
156156
* @param springDocConfigProperties the spring doc config properties
157157
* @return the path prefix
158158
*/
159-
public static String findPathPrefix(OpenAPIService openAPIService, SpringDocConfigProperties springDocConfigProperties) {
160-
ApplicationContext applicationContext = openAPIService.getContext();
159+
public static String findPathPrefix(ApplicationContext applicationContext, SpringDocConfigProperties springDocConfigProperties) {
161160
Map<String, RequestMappingHandlerMapping> beansOfTypeRequestMappingHandlerMapping = applicationContext.getBeansOfType(RequestMappingHandlerMapping.class);
162161
for (RequestMappingHandlerMapping requestMappingHandlerMapping : beansOfTypeRequestMappingHandlerMapping.values()) {
163162
Map<RequestMappingInfo, HandlerMethod> map = requestMappingHandlerMapping.getHandlerMethods();

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424

2525
import com.fasterxml.jackson.databind.ObjectMapper;
2626
import org.springdoc.core.ActuatorProvider;
27-
import org.springdoc.core.OpenAPIService;
2827
import org.springdoc.core.SpringDocConfigProperties;
2928
import org.springdoc.core.SpringDocConfiguration;
3029
import org.springdoc.core.SwaggerUiConfigParameters;
@@ -41,6 +40,7 @@
4140
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
4241
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
4342
import org.springframework.boot.autoconfigure.web.reactive.WebFluxProperties;
43+
import org.springframework.context.ApplicationContext;
4444
import org.springframework.context.annotation.Bean;
4545
import org.springframework.context.annotation.Configuration;
4646
import org.springframework.context.annotation.Lazy;
@@ -67,14 +67,14 @@ public class SwaggerConfig implements WebFluxConfigurer {
6767
* @param swaggerUiConfig the swagger ui config
6868
* @param springDocConfigProperties the spring doc config properties
6969
* @param swaggerUiConfigParameters the swagger ui config parameters
70-
* @param openAPIService the open api service
70+
* @param applicationContext the application context
7171
* @return the swagger welcome web flux
7272
*/
7373
@Bean
7474
@ConditionalOnMissingBean
7575
@ConditionalOnProperty(name = SPRINGDOC_USE_MANAGEMENT_PORT, havingValue = "false", matchIfMissing = true)
76-
SwaggerWelcomeWebFlux swaggerWelcome(SwaggerUiConfigProperties swaggerUiConfig, SpringDocConfigProperties springDocConfigProperties,SwaggerUiConfigParameters swaggerUiConfigParameters, OpenAPIService openAPIService) {
77-
return new SwaggerWelcomeWebFlux(swaggerUiConfig,springDocConfigProperties,swaggerUiConfigParameters,openAPIService);
76+
SwaggerWelcomeWebFlux swaggerWelcome(SwaggerUiConfigProperties swaggerUiConfig, SpringDocConfigProperties springDocConfigProperties,SwaggerUiConfigParameters swaggerUiConfigParameters, ApplicationContext applicationContext) {
77+
return new SwaggerWelcomeWebFlux(swaggerUiConfig,springDocConfigProperties,swaggerUiConfigParameters,applicationContext);
7878
}
7979

8080
/**

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@
2121
package org.springdoc.webflux.ui;
2222

2323
import io.swagger.v3.oas.annotations.Operation;
24-
import org.springdoc.core.OpenAPIService;
2524
import org.springdoc.core.SpringDocConfigProperties;
2625
import org.springdoc.core.SwaggerUiConfigParameters;
2726
import org.springdoc.core.SwaggerUiConfigProperties;
2827
import reactor.core.publisher.Mono;
2928

29+
import org.springframework.context.ApplicationContext;
3030
import org.springframework.http.server.reactive.ServerHttpRequest;
3131
import org.springframework.http.server.reactive.ServerHttpResponse;
3232
import org.springframework.stereotype.Controller;
@@ -47,7 +47,7 @@ public class SwaggerWelcomeWebFlux extends SwaggerWelcomeCommon {
4747
/**
4848
* The Open api service.
4949
*/
50-
private final OpenAPIService openAPIService;
50+
private final ApplicationContext applicationContext;
5151

5252
/**
5353
* The Path prefix.
@@ -60,12 +60,12 @@ public class SwaggerWelcomeWebFlux extends SwaggerWelcomeCommon {
6060
* @param swaggerUiConfig the swagger ui config
6161
* @param springDocConfigProperties the spring doc config properties
6262
* @param swaggerUiConfigParameters the swagger ui config parameters
63-
* @param openAPIService the open api service
63+
* @param applicationContext the application context
6464
*/
6565
public SwaggerWelcomeWebFlux(SwaggerUiConfigProperties swaggerUiConfig, SpringDocConfigProperties springDocConfigProperties,
66-
SwaggerUiConfigParameters swaggerUiConfigParameters, OpenAPIService openAPIService) {
66+
SwaggerUiConfigParameters swaggerUiConfigParameters, ApplicationContext applicationContext) {
6767
super(swaggerUiConfig, springDocConfigProperties, swaggerUiConfigParameters);
68-
this.openAPIService = openAPIService;
68+
this.applicationContext = applicationContext;
6969
}
7070

7171

@@ -115,7 +115,7 @@ protected void calculateOauth2RedirectUrl(UriComponentsBuilder uriComponentsBuil
115115
@Override
116116
protected String buildApiDocUrl() {
117117
if (this.pathPrefix == null)
118-
this.pathPrefix = findPathPrefix(openAPIService, springDocConfigProperties);
118+
this.pathPrefix = findPathPrefix(this.applicationContext, springDocConfigProperties);
119119
return buildUrl(this.contextPath + this.pathPrefix, springDocConfigProperties.getApiDocs().getPath());
120120
}
121121

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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.app24;
20+
21+
import org.junit.jupiter.api.Test;
22+
import test.org.springdoc.ui.AbstractSpringDocTest;
23+
24+
import org.springframework.boot.autoconfigure.SpringBootApplication;
25+
import org.springframework.context.annotation.Import;
26+
import org.springframework.test.context.TestPropertySource;
27+
28+
@TestPropertySource(properties = {
29+
"springdoc.api-docs.enabled=false",
30+
"springdoc.api-docs.path=/api-docs",
31+
"springdoc.swagger-ui.url=/api-docs/xxx/v1/openapi.yml",
32+
})
33+
@Import(SpringDocConfig.class)
34+
public class SpringDocApp24Test extends AbstractSpringDocTest {
35+
36+
@SpringBootApplication
37+
static class SpringDocTestApp {}
38+
39+
@Test
40+
public void test_apidocs_disabled() {
41+
webTestClient.get().uri("/api-docs/swagger-config").exchange()
42+
.expectStatus().isOk().expectBody()
43+
.jsonPath("$.url").isEqualTo("/api-docs/xxx/v1/openapi.yml")
44+
.jsonPath("$.configUrl").isEqualTo("/api-docs/swagger-config")
45+
.jsonPath("$.validatorUrl").isEqualTo("")
46+
.jsonPath("$.oauth2RedirectUrl").isEqualTo("/webjars/swagger-ui/oauth2-redirect.html");
47+
}
48+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package test.org.springdoc.ui.app24;
2+
3+
import org.springdoc.core.SpringDocConfigProperties;
4+
import org.springdoc.core.SpringDocConfiguration;
5+
6+
import org.springframework.context.annotation.Bean;
7+
import org.springframework.context.annotation.Configuration;
8+
9+
@Configuration
10+
public class SpringDocConfig {
11+
12+
@Bean
13+
SpringDocConfiguration springDocConfiguration(){
14+
return new SpringDocConfiguration();
15+
}
16+
17+
@Bean
18+
public SpringDocConfigProperties springDocConfigProperties() {
19+
return new SpringDocConfigProperties();
20+
}
21+
}

springdoc-openapi-webmvc-core/src/main/java/org/springdoc/webmvc/api/OpenApiWebMvcResource.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,19 +152,18 @@ public String openapiYaml(HttpServletRequest request, @Value(DEFAULT_API_DOCS_UR
152152
@Override
153153
protected String getServerUrl(HttpServletRequest request, String apiDocsUrl) {
154154
String requestUrl = decode(request.getRequestURL().toString());
155-
final String prefix = findPathPrefix(this.openAPIService, this.springDocConfigProperties);
155+
final String prefix = findPathPrefix(this.openAPIService.getContext(), this.springDocConfigProperties);
156156
return requestUrl.substring(0, requestUrl.length() - apiDocsUrl.length() - prefix.length());
157157
}
158158

159159
/**
160160
* Finds path prefix.
161161
*
162-
* @param openAPIService the open api service
162+
* @param applicationContext the application context
163163
* @param springDocConfigProperties the spring doc config properties
164164
* @return the path prefix
165165
*/
166-
public static String findPathPrefix(OpenAPIService openAPIService, SpringDocConfigProperties springDocConfigProperties) {
167-
ApplicationContext applicationContext = openAPIService.getContext();
166+
public static String findPathPrefix(ApplicationContext applicationContext, SpringDocConfigProperties springDocConfigProperties) {
168167
Map<String, RequestMappingHandlerMapping> beansOfTypeRequestMappingHandlerMapping = applicationContext.getBeansOfType(RequestMappingHandlerMapping.class);
169168
for (RequestMappingHandlerMapping requestMappingHandlerMapping : beansOfTypeRequestMappingHandlerMapping.values()) {
170169
Map<RequestMappingInfo, HandlerMethod> map = requestMappingHandlerMapping.getHandlerMethods();

springdoc-openapi-webmvc-core/src/test/java/test/org/springdoc/api/app171/HelloLocaleController.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@
2121
import javax.validation.Valid;
2222
import javax.validation.constraints.NotBlank;
2323

24+
import io.swagger.v3.oas.annotations.tags.Tag;
25+
2426
import org.springframework.http.HttpEntity;
2527
import org.springframework.web.bind.annotation.GetMapping;
2628
import org.springframework.web.bind.annotation.RestController;
2729

28-
import io.swagger.v3.oas.annotations.tags.Tag;
29-
3030
@RestController
3131
@Tag(name = "greeting", description = "test")
3232
public class HelloLocaleController {

0 commit comments

Comments
 (0)