Skip to content

Commit 57fb0f6

Browse files
committed
Remove webjars-locator-core #2173
1 parent 1849bc7 commit 57fb0f6

File tree

22 files changed

+339
-227
lines changed

22 files changed

+339
-227
lines changed

pom.xml

-18
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,6 @@
7070
<swagger-api.version>2.2.9</swagger-api.version>
7171
<swagger-ui.version>4.18.1</swagger-ui.version>
7272
<spring-security-oauth2.version>2.5.2.RELEASE</spring-security-oauth2.version>
73-
<classgraph.version>4.8.149</classgraph.version>
74-
<webjars-locator-core.version>0.52</webjars-locator-core.version>
7573
<gmavenplus-plugin.version>1.8.1</gmavenplus-plugin.version>
7674
<jaxb-impl.version>2.1</jaxb-impl.version>
7775
<javax.jws-api.version>1.1</javax.jws-api.version>
@@ -96,17 +94,6 @@
9694
<artifactId>swagger-ui</artifactId>
9795
<version>${swagger-ui.version}</version>
9896
</dependency>
99-
<dependency>
100-
<groupId>org.webjars</groupId>
101-
<artifactId>webjars-locator-core</artifactId>
102-
<version>${webjars-locator-core.version}</version>
103-
<exclusions>
104-
<exclusion>
105-
<groupId>io.github.classgraph</groupId>
106-
<artifactId>classgraph</artifactId>
107-
</exclusion>
108-
</exclusions>
109-
</dependency>
11097
<dependency>
11198
<groupId>org.springframework.security.oauth</groupId>
11299
<artifactId>spring-security-oauth2</artifactId>
@@ -117,11 +104,6 @@
117104
<artifactId>spring-security-oauth2-authorization-server</artifactId>
118105
<version>${spring-security-oauth2-authorization-server.version}</version>
119106
</dependency>
120-
<dependency>
121-
<groupId>io.github.classgraph</groupId>
122-
<artifactId>classgraph</artifactId>
123-
<version>${classgraph.version}</version>
124-
</dependency>
125107
<dependency>
126108
<groupId>javax.xml</groupId>
127109
<artifactId>jaxb-impl</artifactId>

springdoc-openapi-common/pom.xml

+6
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@
4444
</dependency>
4545
</dependencies>
4646
<build>
47+
<resources>
48+
<resource>
49+
<directory>src/main/resources</directory>
50+
<filtering>true</filtering>
51+
</resource>
52+
</resources>
4753
<plugins>
4854
<plugin>
4955
<groupId>org.apache.maven.plugins</groupId>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
*
3+
* *
4+
* * *
5+
* * * *
6+
* * * * * Copyright 2019-2022 the original author or authors.
7+
* * * * *
8+
* * * * * Licensed under the Apache License, Version 2.0 (the "License");
9+
* * * * * you may not use this file except in compliance with the License.
10+
* * * * * You may obtain a copy of the License at
11+
* * * * *
12+
* * * * * https://www.apache.org/licenses/LICENSE-2.0
13+
* * * * *
14+
* * * * * Unless required by applicable law or agreed to in writing, software
15+
* * * * * distributed under the License is distributed on an "AS IS" BASIS,
16+
* * * * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* * * * * See the License for the specific language governing permissions and
18+
* * * * * limitations under the License.
19+
* * * *
20+
* * *
21+
* *
22+
*
23+
*/
24+
25+
package org.springdoc.core;
26+
27+
import java.io.IOException;
28+
import java.util.Optional;
29+
import java.util.Properties;
30+
31+
import org.apache.commons.lang3.StringUtils;
32+
33+
import org.springframework.beans.factory.InitializingBean;
34+
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
35+
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
36+
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
37+
import org.springframework.context.annotation.Configuration;
38+
import org.springframework.context.annotation.Lazy;
39+
import org.springframework.core.io.ClassPathResource;
40+
import org.springframework.core.io.Resource;
41+
import org.springframework.core.io.support.PropertiesLoaderUtils;
42+
import org.springframework.util.AntPathMatcher;
43+
44+
/**
45+
* The type Spring doc Native Configuration.
46+
* @author bnasslahsen
47+
*/
48+
@Lazy(false)
49+
@ConditionalOnExpression("${springdoc.api-docs.enabled:true}")
50+
@ConditionalOnWebApplication
51+
@Configuration(proxyBeanMethods = false)
52+
@ConditionalOnBean(SpringDocConfiguration.class)
53+
public class SpringDocUIConfiguration implements InitializingBean {
54+
55+
/**
56+
* The constant SPRINGDOC_CONFIG_PROPERTIES.
57+
*/
58+
public static final String SPRINGDOC_CONFIG_PROPERTIES = "springdoc.config.properties";
59+
60+
/**
61+
* The constant SPRINGDOC_SWAGGERUI_VERSION.
62+
*/
63+
private static final String SPRINGDOC_SWAGGER_UI_VERSION = "springdoc.swagger-ui.version";
64+
65+
/**
66+
* The Swagger ui config properties.
67+
*/
68+
private final Optional<SwaggerUiConfigProperties> optionalSwaggerUiConfigProperties;
69+
70+
/**
71+
* Instantiates a new Spring doc hints.
72+
*
73+
* @param optionalSwaggerUiConfigProperties the swagger ui config properties
74+
*/
75+
public SpringDocUIConfiguration(Optional<SwaggerUiConfigProperties> optionalSwaggerUiConfigProperties) {
76+
this.optionalSwaggerUiConfigProperties = optionalSwaggerUiConfigProperties;
77+
}
78+
79+
@Override
80+
public void afterPropertiesSet() {
81+
optionalSwaggerUiConfigProperties.ifPresent(swaggerUiConfigProperties -> {
82+
if (StringUtils.isEmpty(swaggerUiConfigProperties.getVersion())) {
83+
try {
84+
Resource resource = new ClassPathResource(AntPathMatcher.DEFAULT_PATH_SEPARATOR + SPRINGDOC_CONFIG_PROPERTIES);
85+
Properties props = PropertiesLoaderUtils.loadProperties(resource);
86+
swaggerUiConfigProperties.setVersion(props.getProperty(SPRINGDOC_SWAGGER_UI_VERSION));
87+
}
88+
catch (IOException e) {
89+
throw new RuntimeException(e);
90+
}
91+
}
92+
});
93+
}
94+
}
95+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package org.springdoc.ui;
2+
3+
import java.io.File;
4+
5+
import org.springdoc.core.SwaggerUiConfigProperties;
6+
7+
import org.springframework.lang.Nullable;
8+
9+
/**
10+
* The type Web jars version resource resolver.
11+
*
12+
* @author bnasslahsen
13+
*/
14+
public class AbstractSwaggerResourceResolver {
15+
16+
/**
17+
* The Swagger ui config properties.
18+
*/
19+
private final SwaggerUiConfigProperties swaggerUiConfigProperties;
20+
21+
/**
22+
* Instantiates a new Web jars version resource resolver.
23+
*
24+
* @param swaggerUiConfigProperties the swagger ui config properties
25+
*/
26+
public AbstractSwaggerResourceResolver(SwaggerUiConfigProperties swaggerUiConfigProperties) {
27+
this.swaggerUiConfigProperties = swaggerUiConfigProperties;
28+
}
29+
30+
/**
31+
* Find web jar resource path string.
32+
*
33+
* @param path the path
34+
* @return the string
35+
*/
36+
@Nullable
37+
protected String findWebJarResourcePath(String path) {
38+
String webjar = webjar(path);
39+
if (webjar.length() > 0) {
40+
String version = swaggerUiConfigProperties.getVersion();
41+
if (version != null) {
42+
String partialPath = path(webjar, path);
43+
return webjar + File.separator + version + File.separator + partialPath;
44+
}
45+
}
46+
return null;
47+
}
48+
49+
/**
50+
* Webjar string.
51+
*
52+
* @param path the path
53+
* @return the string
54+
*/
55+
private String webjar(String path) {
56+
int startOffset = (path.startsWith("/") ? 1 : 0);
57+
int endOffset = path.indexOf('/', 1);
58+
return endOffset != -1 ? path.substring(startOffset, endOffset) : path;
59+
}
60+
61+
62+
/**
63+
* Path string.
64+
*
65+
* @param webjar the webjar
66+
* @param path the path
67+
* @return the string
68+
*/
69+
private String path(String webjar, String path) {
70+
if (path.startsWith(webjar)) {
71+
path = path.substring(webjar.length() + 1);
72+
}
73+
return path;
74+
}
75+
}

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

+3-13
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,14 @@
3131
import org.springframework.util.CollectionUtils;
3232
import org.springframework.web.util.UriComponentsBuilder;
3333

34-
import static org.springdoc.core.Constants.INDEX_PAGE;
35-
import static org.springdoc.core.Constants.OAUTH_REDIRECT_PAGE;
3634
import static org.springdoc.core.Constants.SWAGGER_UI_OAUTH_REDIRECT_URL;
37-
import static org.springdoc.core.Constants.SWAGGER_UI_PREFIX;
3835
import static org.springdoc.core.Constants.SWAGGER_UI_URL;
3936
import static org.springframework.util.AntPathMatcher.DEFAULT_PATH_SEPARATOR;
4037

4138

4239
/**
4340
* The type Abstract swagger welcome.
41+
*
4442
* @author bnasslashen
4543
*/
4644
public abstract class AbstractSwaggerWelcome {
@@ -222,12 +220,7 @@ protected void calculateUiRootCommon(StringBuilder sbUrl, StringBuilder[] sbUrls
222220
* @return the oauth2 redirect url
223221
*/
224222
protected String getOauth2RedirectUrl() {
225-
if (StringUtils.isNotEmpty(swaggerUiConfig.getVersion())) {
226-
return StringUtils.defaultIfBlank(swaggerUiConfig.getOauth2RedirectUrl(), SWAGGER_UI_PREFIX + DEFAULT_PATH_SEPARATOR + swaggerUiConfig.getVersion() + OAUTH_REDIRECT_PAGE);
227-
}
228-
else {
229-
return StringUtils.defaultIfBlank(swaggerUiConfig.getOauth2RedirectUrl(), SWAGGER_UI_OAUTH_REDIRECT_URL);
230-
}
223+
return StringUtils.defaultIfBlank(swaggerUiConfig.getOauth2RedirectUrl(), SWAGGER_UI_OAUTH_REDIRECT_URL);
231224
}
232225

233226
/**
@@ -236,9 +229,6 @@ protected String getOauth2RedirectUrl() {
236229
* @return the swagger ui url
237230
*/
238231
protected String getSwaggerUiUrl() {
239-
if (StringUtils.isNotEmpty(swaggerUiConfig.getVersion()))
240-
return SWAGGER_UI_PREFIX + DEFAULT_PATH_SEPARATOR + swaggerUiConfig.getVersion() + INDEX_PAGE;
241-
else
242-
return SWAGGER_UI_URL;
232+
return SWAGGER_UI_URL;
243233
}
244234
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
springdoc.swagger-ui.version[email protected]@

springdoc-openapi-ui/pom.xml

-8
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,6 @@
2323
<groupId>org.webjars</groupId>
2424
<artifactId>swagger-ui</artifactId>
2525
</dependency>
26-
<dependency>
27-
<groupId>org.webjars</groupId>
28-
<artifactId>webjars-locator-core</artifactId>
29-
</dependency>
30-
<dependency>
31-
<groupId>io.github.classgraph</groupId>
32-
<artifactId>classgraph</artifactId>
33-
</dependency>
3426
<dependency>
3527
<groupId>org.springframework.boot</groupId>
3628
<artifactId>spring-boot-starter-security</artifactId>

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

+11-2
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555

5656
/**
5757
* The type Swagger config.
58+
*
5859
* @author bnasslahsen
5960
*/
6061
@Lazy(false)
@@ -149,8 +150,9 @@ SwaggerIndexTransformer indexPageTransformer(SwaggerUiConfigProperties swaggerUi
149150
@Bean
150151
@ConditionalOnMissingBean
151152
@Lazy(false)
152-
SwaggerWebMvcConfigurer swaggerWebMvcConfigurer(SwaggerUiConfigParameters swaggerUiConfigParameters, SwaggerIndexTransformer swaggerIndexTransformer, Optional<ActuatorProvider> actuatorProvider) {
153-
return new SwaggerWebMvcConfigurer(swaggerUiConfigParameters, swaggerIndexTransformer, actuatorProvider);
153+
SwaggerWebMvcConfigurer swaggerWebMvcConfigurer(SwaggerUiConfigParameters swaggerUiConfigParameters, SwaggerIndexTransformer swaggerIndexTransformer,
154+
Optional<ActuatorProvider> actuatorProvider, SwaggerResourceResolver swaggerResourceResolver) {
155+
return new SwaggerWebMvcConfigurer(swaggerUiConfigParameters, swaggerIndexTransformer, actuatorProvider, swaggerResourceResolver);
154156
}
155157

156158
/**
@@ -166,6 +168,12 @@ SwaggerUiConfigParameters swaggerUiConfigParameters(SwaggerUiConfigProperties sw
166168
return new SwaggerUiConfigParameters(swaggerUiConfig);
167169
}
168170

171+
@Bean
172+
@ConditionalOnMissingBean
173+
@Lazy(false)
174+
SwaggerResourceResolver swaggerResourceResolver(SwaggerUiConfigProperties swaggerUiConfigProperties) {
175+
return new SwaggerResourceResolver(swaggerUiConfigProperties);
176+
}
169177
/**
170178
* The type Swagger actuator welcome configuration.
171179
*/
@@ -190,4 +198,5 @@ SwaggerWelcomeActuator swaggerActuatorWelcome(SwaggerUiConfigProperties swaggerU
190198
return new SwaggerWelcomeActuator(swaggerUiConfig, springDocConfigProperties, swaggerUiConfigParameters, webEndpointProperties);
191199
}
192200
}
201+
193202
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package org.springdoc.webmvc.ui;
2+
3+
import java.util.List;
4+
5+
import javax.servlet.http.HttpServletRequest;
6+
7+
import org.springdoc.core.SwaggerUiConfigProperties;
8+
import org.springdoc.ui.AbstractSwaggerResourceResolver;
9+
10+
import org.springframework.core.io.Resource;
11+
import org.springframework.web.servlet.resource.ResourceResolver;
12+
import org.springframework.web.servlet.resource.ResourceResolverChain;
13+
14+
/**
15+
* The type Web jars version resource resolver.
16+
*
17+
* @author bnasslahsen
18+
*/
19+
public class SwaggerResourceResolver extends AbstractSwaggerResourceResolver implements ResourceResolver {
20+
21+
/**
22+
* Instantiates a new Web jars version resource resolver.
23+
*
24+
* @param swaggerUiConfigProperties the swagger ui config properties
25+
*/
26+
public SwaggerResourceResolver(SwaggerUiConfigProperties swaggerUiConfigProperties) {
27+
super(swaggerUiConfigProperties);
28+
}
29+
30+
@Override
31+
public Resource resolveResource(HttpServletRequest request, String requestPath, List<? extends Resource> locations, ResourceResolverChain chain) {
32+
Resource resolved = chain.resolveResource(request, requestPath, locations);
33+
if (resolved == null) {
34+
String webJarResourcePath = findWebJarResourcePath(requestPath);
35+
if (webJarResourcePath != null)
36+
return chain.resolveResource(request, webJarResourcePath, locations);
37+
}
38+
return resolved; }
39+
40+
@Override
41+
public String resolveUrlPath(String resourcePath, List<? extends Resource> locations, ResourceResolverChain chain) {
42+
String path = chain.resolveUrlPath(resourcePath, locations);
43+
if (path == null) {
44+
String webJarResourcePath = findWebJarResourcePath(resourcePath);
45+
if (webJarResourcePath != null)
46+
return chain.resolveUrlPath(webJarResourcePath, locations);
47+
}
48+
return path;
49+
}
50+
}

0 commit comments

Comments
 (0)