Skip to content

Commit e7f6bfe

Browse files
committed
Merge branch 'master' into builder-customizer1
2 parents 7b77925 + f8fef19 commit e7f6bfe

File tree

24 files changed

+502
-40
lines changed

24 files changed

+502
-40
lines changed

springdoc-openapi-common/src/main/java/org/springdoc/core/GenericResponseBuilder.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,12 +255,12 @@ private Type getReturnType(Method method) {
255255
return returnType;
256256
}
257257

258-
private Schema<?> calculateSchema(Components components, Type returnType, JsonView jsonView) {
258+
public Schema calculateSchema(Components components, Type returnType, JsonView jsonView) {
259259
if (isVoid(returnType)) {
260260
// if void, no content
261261
return null;
262262
}
263-
Schema<?> schemaN = SpringDocAnnotationsUtils.extractSchema(components, returnType, jsonView);
263+
Schema schemaN = SpringDocAnnotationsUtils.extractSchema(components, returnType, jsonView);
264264
if (schemaN == null && returnType instanceof Class && !isResponseTypeToIgnore((Class) returnType)) {
265265
schemaN = AnnotationsUtils.resolveSchemaFromType((Class) returnType, null, jsonView);
266266
}

springdoc-openapi-common/src/main/java/org/springdoc/ui/AbstractSwaggerWelcome.java

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
package org.springdoc.ui;
2020

21-
import org.apache.commons.lang3.ArrayUtils;
2221
import org.apache.commons.lang3.StringUtils;
2322
import org.springdoc.core.SpringDocConfigProperties;
2423
import org.springdoc.core.SwaggerUiConfigProperties;
@@ -30,7 +29,6 @@
3029
import static org.springdoc.core.Constants.SPRINGDOC_OAUTH2_REDIRECT_URL_VALUE;
3130
import static org.springdoc.core.Constants.SPRINGDOC_SWAGGER_UI_CONFIG_URL_VALUE;
3231
import static org.springdoc.core.Constants.SPRINGDOC_SWAGGER_UI_URL_VALUE;
33-
import static org.springdoc.core.Constants.SWAGGER_UI_OAUTH_REDIRECT_URL;
3432
import static org.springdoc.core.Constants.SWAGGGER_CONFIG_FILE;
3533
import static org.springframework.util.AntPathMatcher.DEFAULT_PATH_SEPARATOR;
3634

@@ -47,7 +45,7 @@ public abstract class AbstractSwaggerWelcome implements InitializingBean {
4745
private String originConfigUrl;
4846

4947
@Value(SPRINGDOC_OAUTH2_REDIRECT_URL_VALUE)
50-
private String oauth2RedirectUrl;
48+
protected String oauth2RedirectUrl;
5149

5250
@Value(SPRINGDOC_SWAGGER_UI_URL_VALUE)
5351
private String swaggerUiUrl;
@@ -62,25 +60,14 @@ public void afterPropertiesSet() {
6260
calculateUiRootPath();
6361
}
6462

65-
66-
protected void calculateUiRootPath(StringBuilder... sbUrls) {
67-
StringBuilder sbUrl = new StringBuilder();
68-
if (ArrayUtils.isNotEmpty(sbUrls))
69-
sbUrl = sbUrls[0];
70-
String swaggerPath = swaggerUiConfig.getPath();
71-
if (swaggerPath.contains(DEFAULT_PATH_SEPARATOR))
72-
sbUrl.append(swaggerPath, 0, swaggerPath.lastIndexOf(DEFAULT_PATH_SEPARATOR));
73-
this.uiRootPath = sbUrl.toString();
74-
}
75-
7663
protected String buildUrl(String contextPath, final String docsUrl) {
7764
if (contextPath.endsWith(DEFAULT_PATH_SEPARATOR)) {
7865
return contextPath.substring(0, contextPath.length() - 1) + docsUrl;
7966
}
8067
return contextPath + docsUrl;
8168
}
8269

83-
void buildConfigUrl(String contextPath, UriComponentsBuilder uriComponentsBuilder) {
70+
protected void buildConfigUrl(String contextPath, UriComponentsBuilder uriComponentsBuilder) {
8471
String apiDocsUrl = springDocConfigProperties.getApiDocs().getPath();
8572
if (StringUtils.isEmpty(originConfigUrl)) {
8673
String url = buildUrl(contextPath, apiDocsUrl);
@@ -95,12 +82,11 @@ void buildConfigUrl(String contextPath, UriComponentsBuilder uriComponentsBuilde
9582
else
9683
SwaggerUiConfigProperties.addUrl(url);
9784
}
98-
if (StringUtils.isEmpty(oauth2RedirectUrl)) {
99-
swaggerUiConfig.setOauth2RedirectUrl(uriComponentsBuilder.path(this.uiRootPath).path(SWAGGER_UI_OAUTH_REDIRECT_URL).build().toString());
100-
}
101-
else if (!swaggerUiConfig.isValidUrl(swaggerUiConfig.getOauth2RedirectUrl())) {
102-
swaggerUiConfig.setOauth2RedirectUrl(uriComponentsBuilder.path(this.uiRootPath).path(swaggerUiConfig.getOauth2RedirectUrl()).build().toString());
103-
}
85+
calculateOauth2RedirectUrl(uriComponentsBuilder);
10486
}
10587

88+
abstract void calculateOauth2RedirectUrl(UriComponentsBuilder uriComponentsBuilder);
89+
90+
abstract void calculateUiRootPath(StringBuilder... sbUrls);
91+
10692
}

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@
3434
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
3535

3636
import static org.springdoc.core.Constants.CLASSPATH_RESOURCE_LOCATION;
37+
import static org.springdoc.core.Constants.DEFAULT_WEB_JARS_PREFIX_URL;
3738
import static org.springdoc.core.Constants.SPRINGDOC_SWAGGER_UI_ENABLED;
3839
import static org.springdoc.core.Constants.SWAGGER_UI_PATH;
39-
import static org.springdoc.core.Constants.WEB_JARS_PREFIX_URL;
4040
import static org.springframework.util.AntPathMatcher.DEFAULT_PATH_SEPARATOR;
4141

4242

@@ -49,9 +49,6 @@ public class SwaggerConfig extends WebMvcConfigurerAdapter { // NOSONAR
4949
@Value(SWAGGER_UI_PATH)
5050
private String swaggerPath;
5151

52-
@Value(WEB_JARS_PREFIX_URL)
53-
private String webJarsPrefixUrl;
54-
5552
@Autowired
5653
private SwaggerIndexTransformer swaggerIndexTransformer;
5754

@@ -62,8 +59,8 @@ public void addResourceHandlers(ResourceHandlerRegistry registry) {
6259
uiRootPath.append(swaggerPath, 0, swaggerPath.lastIndexOf('/'));
6360
}
6461
uiRootPath.append("/**");
65-
String webJarsLocation = webJarsPrefixUrl + DEFAULT_PATH_SEPARATOR;
66-
registry.addResourceHandler(uiRootPath + "/swagger-ui/**").addResourceLocations(CLASSPATH_RESOURCE_LOCATION + webJarsLocation)
62+
registry.addResourceHandler(uiRootPath + "/swagger-ui/**")
63+
.addResourceLocations(CLASSPATH_RESOURCE_LOCATION + DEFAULT_WEB_JARS_PREFIX_URL+DEFAULT_PATH_SEPARATOR)
6764
.resourceChain(false)
6865
.addTransformer(swaggerIndexTransformer);
6966
}

springdoc-openapi-ui/src/main/java/org/springdoc/ui/SwaggerWelcome.java

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import javax.servlet.http.HttpServletRequest;
2424

2525
import io.swagger.v3.oas.annotations.Operation;
26+
import org.apache.commons.lang3.ArrayUtils;
2627
import org.apache.commons.lang3.StringUtils;
2728
import org.springdoc.core.SpringDocConfigProperties;
2829
import org.springdoc.core.SpringDocConfiguration;
@@ -41,8 +42,10 @@
4142
import static org.springdoc.core.Constants.MVC_SERVLET_PATH;
4243
import static org.springdoc.core.Constants.SPRINGDOC_SWAGGER_UI_ENABLED;
4344
import static org.springdoc.core.Constants.SWAGGER_CONFIG_URL;
45+
import static org.springdoc.core.Constants.SWAGGER_UI_OAUTH_REDIRECT_URL;
4446
import static org.springdoc.core.Constants.SWAGGER_UI_PATH;
4547
import static org.springdoc.core.Constants.SWAGGER_UI_URL;
48+
import static org.springframework.util.AntPathMatcher.DEFAULT_PATH_SEPARATOR;
4649
import static org.springframework.web.servlet.view.UrlBasedViewResolver.REDIRECT_URL_PREFIX;
4750

4851
@Controller
@@ -75,17 +78,32 @@ public Map<String, Object> openapiYaml(HttpServletRequest request) {
7578
}
7679

7780
@Override
78-
protected void calculateUiRootPath(StringBuilder... sbUrls) {
81+
void calculateUiRootPath(StringBuilder... sbUrls) {
7982
StringBuilder sbUrl = new StringBuilder();
8083
if (StringUtils.isNotBlank(mvcServletPath))
8184
sbUrl.append(mvcServletPath);
82-
super.calculateUiRootPath(sbUrl);
85+
if (ArrayUtils.isNotEmpty(sbUrls))
86+
sbUrl = sbUrls[0];
87+
String swaggerPath = swaggerUiConfig.getPath();
88+
if (swaggerPath.contains(DEFAULT_PATH_SEPARATOR))
89+
sbUrl.append(swaggerPath, 0, swaggerPath.lastIndexOf(DEFAULT_PATH_SEPARATOR));
90+
this.uiRootPath = sbUrl.toString();
8391
}
8492

8593
@Override
8694
protected String buildUrl(String contextPath, final String docsUrl) {
8795
if (StringUtils.isNotBlank(mvcServletPath))
8896
contextPath += mvcServletPath;
89-
return super.buildUrl(contextPath,docsUrl);
97+
return super.buildUrl(contextPath, docsUrl);
98+
}
99+
100+
@Override
101+
void calculateOauth2RedirectUrl(UriComponentsBuilder uriComponentsBuilder) {
102+
if (StringUtils.isEmpty(oauth2RedirectUrl)) {
103+
swaggerUiConfig.setOauth2RedirectUrl(uriComponentsBuilder.path(this.uiRootPath).path(SWAGGER_UI_OAUTH_REDIRECT_URL).build().toString());
104+
}
105+
else if (!swaggerUiConfig.isValidUrl(swaggerUiConfig.getOauth2RedirectUrl())) {
106+
swaggerUiConfig.setOauth2RedirectUrl(uriComponentsBuilder.path(this.uiRootPath).path(swaggerUiConfig.getOauth2RedirectUrl()).build().toString());
107+
}
90108
}
91109
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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.app1;
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.test.context.TestPropertySource;
26+
27+
import static org.hamcrest.CoreMatchers.equalTo;
28+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
29+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
30+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
31+
32+
@TestPropertySource(properties = {
33+
"springdoc.swagger-ui.path=/documentation/swagger-ui.html",
34+
"springdoc.api-docs.path=/documentation/v3/api-docs",
35+
"springdoc.webjars.prefix= /webjars-pref"
36+
})
37+
public class SpringDocAppRedirectWithPrefixTest extends AbstractSpringDocTest {
38+
39+
@Test
40+
public void shouldRedirectWithPrefix() throws Exception {
41+
42+
mockMvc.perform(get("/documentation/v3/api-docs/swagger-config"))
43+
.andExpect(status().isOk())
44+
.andExpect(jsonPath("validatorUrl", equalTo("")))
45+
.andExpect(jsonPath("oauth2RedirectUrl", equalTo("http://localhost/documentation/swagger-ui/oauth2-redirect.html")));
46+
}
47+
48+
@SpringBootApplication
49+
static class SpringDocTestApp {}
50+
51+
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import reactor.core.publisher.Mono;
4040

4141
import org.springframework.beans.factory.annotation.Value;
42+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
4243
import org.springframework.http.MediaType;
4344
import org.springframework.http.server.reactive.ServerHttpRequest;
4445
import org.springframework.web.bind.annotation.GetMapping;
@@ -56,6 +57,7 @@
5657
import static org.springframework.util.AntPathMatcher.DEFAULT_PATH_SEPARATOR;
5758

5859
@RestController
60+
@ConditionalOnMissingBean(name = "openApiResource")
5961
public class OpenApiResource extends AbstractOpenApiResource {
6062

6163
private final RequestMappingInfoHandlerMapping requestMappingHandlerMapping;

springdoc-openapi-webflux-core/src/main/java/org/springdoc/core/MultipleOpenApiWebFluxConfiguration.java

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

2525
import org.springframework.beans.factory.ObjectFactory;
2626
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
27+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
2728
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
2829
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
2930
import org.springframework.context.annotation.Bean;
@@ -40,6 +41,7 @@
4041
public class MultipleOpenApiWebFluxConfiguration {
4142

4243
@Bean
44+
@ConditionalOnMissingBean
4345
public MultipleOpenApiResource multipleOpenApiResource(List<GroupedOpenApi> groupedOpenApis,
4446
ObjectFactory<OpenAPIBuilder> defaultOpenAPIBuilder, AbstractRequestBuilder requestBuilder,
4547
GenericResponseBuilder responseBuilder, OperationBuilder operationParser,

springdoc-openapi-webflux-core/src/main/java/org/springdoc/core/SpringDocWebFluxConfiguration.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
public class SpringDocWebFluxConfiguration {
4444

4545
@Bean
46+
@ConditionalOnMissingBean
4647
public OpenApiResource openApiResource(OpenAPIBuilder openAPIBuilder, AbstractRequestBuilder requestBuilder,
4748
GenericResponseBuilder responseBuilder, OperationBuilder operationParser,
4849
RequestMappingInfoHandlerMapping requestMappingHandlerMapping,

springdoc-openapi-webflux-core/src/test/java/test/org/springdoc/api/app66/SpringDocTestApp.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public OpenAPI customOpenAPI() {
4747
}
4848

4949
@Bean
50-
public GroupedOpenApi stramOpenApi() {
50+
public GroupedOpenApi streamOpenApi() {
5151
String[] paths = { "/stream/**" };
5252
String[] packagedToMatch = { "test.org.springdoc.api.app66" };
5353
return GroupedOpenApi.builder().setGroup("stream").pathsToMatch(paths)
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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 org.springdoc.ui;
20+
21+
import org.springframework.beans.factory.annotation.Value;
22+
import org.springframework.context.annotation.Configuration;
23+
import org.springframework.web.reactive.config.ResourceHandlerRegistry;
24+
import org.springframework.web.reactive.config.WebFluxConfigurer;
25+
26+
import static org.springdoc.core.Constants.CLASSPATH_RESOURCE_LOCATION;
27+
import static org.springdoc.core.Constants.DEFAULT_WEB_JARS_PREFIX_URL;
28+
import static org.springdoc.core.Constants.SWAGGER_UI_PATH;
29+
import static org.springdoc.core.Constants.WEB_JARS_PREFIX_URL;
30+
import static org.springframework.util.AntPathMatcher.DEFAULT_PATH_SEPARATOR;
31+
32+
33+
@Configuration
34+
public class SwaggerConfig implements WebFluxConfigurer {
35+
36+
@Value(SWAGGER_UI_PATH)
37+
private String swaggerPath;
38+
39+
@Value(WEB_JARS_PREFIX_URL)
40+
private String webJarsPrefixUrl;
41+
42+
43+
@Override
44+
public void addResourceHandlers(ResourceHandlerRegistry registry) {
45+
StringBuilder uiRootPath = new StringBuilder();
46+
if (swaggerPath.contains("/")) {
47+
uiRootPath.append(swaggerPath, 0, swaggerPath.lastIndexOf('/'));
48+
}
49+
registry.addResourceHandler(uiRootPath + webJarsPrefixUrl+"/**")
50+
.addResourceLocations(CLASSPATH_RESOURCE_LOCATION + DEFAULT_WEB_JARS_PREFIX_URL+DEFAULT_PATH_SEPARATOR)
51+
.resourceChain(false);
52+
}
53+
54+
}

0 commit comments

Comments
 (0)