-
-
Notifications
You must be signed in to change notification settings - Fork 501
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1849bc7
commit 57fb0f6
Showing
22 changed files
with
339 additions
and
227 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
springdoc-openapi-common/src/main/java/org/springdoc/core/SpringDocUIConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/* | ||
* | ||
* * | ||
* * * | ||
* * * * | ||
* * * * * Copyright 2019-2022 the original author or authors. | ||
* * * * * | ||
* * * * * Licensed under the Apache License, Version 2.0 (the "License"); | ||
* * * * * you may not use this file except in compliance with the License. | ||
* * * * * You may obtain a copy of the License at | ||
* * * * * | ||
* * * * * https://www.apache.org/licenses/LICENSE-2.0 | ||
* * * * * | ||
* * * * * Unless required by applicable law or agreed to in writing, software | ||
* * * * * distributed under the License is distributed on an "AS IS" BASIS, | ||
* * * * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* * * * * See the License for the specific language governing permissions and | ||
* * * * * limitations under the License. | ||
* * * * | ||
* * * | ||
* * | ||
* | ||
*/ | ||
|
||
package org.springdoc.core; | ||
|
||
import java.io.IOException; | ||
import java.util.Optional; | ||
import java.util.Properties; | ||
|
||
import org.apache.commons.lang3.StringUtils; | ||
|
||
import org.springframework.beans.factory.InitializingBean; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.context.annotation.Lazy; | ||
import org.springframework.core.io.ClassPathResource; | ||
import org.springframework.core.io.Resource; | ||
import org.springframework.core.io.support.PropertiesLoaderUtils; | ||
import org.springframework.util.AntPathMatcher; | ||
|
||
/** | ||
* The type Spring doc Native Configuration. | ||
* @author bnasslahsen | ||
*/ | ||
@Lazy(false) | ||
@ConditionalOnExpression("${springdoc.api-docs.enabled:true}") | ||
@ConditionalOnWebApplication | ||
@Configuration(proxyBeanMethods = false) | ||
@ConditionalOnBean(SpringDocConfiguration.class) | ||
public class SpringDocUIConfiguration implements InitializingBean { | ||
|
||
/** | ||
* The constant SPRINGDOC_CONFIG_PROPERTIES. | ||
*/ | ||
public static final String SPRINGDOC_CONFIG_PROPERTIES = "springdoc.config.properties"; | ||
|
||
/** | ||
* The constant SPRINGDOC_SWAGGERUI_VERSION. | ||
*/ | ||
private static final String SPRINGDOC_SWAGGER_UI_VERSION = "springdoc.swagger-ui.version"; | ||
|
||
/** | ||
* The Swagger ui config properties. | ||
*/ | ||
private final Optional<SwaggerUiConfigProperties> optionalSwaggerUiConfigProperties; | ||
|
||
/** | ||
* Instantiates a new Spring doc hints. | ||
* | ||
* @param optionalSwaggerUiConfigProperties the swagger ui config properties | ||
*/ | ||
public SpringDocUIConfiguration(Optional<SwaggerUiConfigProperties> optionalSwaggerUiConfigProperties) { | ||
this.optionalSwaggerUiConfigProperties = optionalSwaggerUiConfigProperties; | ||
} | ||
|
||
@Override | ||
public void afterPropertiesSet() { | ||
optionalSwaggerUiConfigProperties.ifPresent(swaggerUiConfigProperties -> { | ||
if (StringUtils.isEmpty(swaggerUiConfigProperties.getVersion())) { | ||
try { | ||
Resource resource = new ClassPathResource(AntPathMatcher.DEFAULT_PATH_SEPARATOR + SPRINGDOC_CONFIG_PROPERTIES); | ||
Properties props = PropertiesLoaderUtils.loadProperties(resource); | ||
swaggerUiConfigProperties.setVersion(props.getProperty(SPRINGDOC_SWAGGER_UI_VERSION)); | ||
} | ||
catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
}); | ||
} | ||
} | ||
|
75 changes: 75 additions & 0 deletions
75
springdoc-openapi-common/src/main/java/org/springdoc/ui/AbstractSwaggerResourceResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package org.springdoc.ui; | ||
|
||
import java.io.File; | ||
|
||
import org.springdoc.core.SwaggerUiConfigProperties; | ||
|
||
import org.springframework.lang.Nullable; | ||
|
||
/** | ||
* The type Web jars version resource resolver. | ||
* | ||
* @author bnasslahsen | ||
*/ | ||
public class AbstractSwaggerResourceResolver { | ||
|
||
/** | ||
* The Swagger ui config properties. | ||
*/ | ||
private final SwaggerUiConfigProperties swaggerUiConfigProperties; | ||
|
||
/** | ||
* Instantiates a new Web jars version resource resolver. | ||
* | ||
* @param swaggerUiConfigProperties the swagger ui config properties | ||
*/ | ||
public AbstractSwaggerResourceResolver(SwaggerUiConfigProperties swaggerUiConfigProperties) { | ||
this.swaggerUiConfigProperties = swaggerUiConfigProperties; | ||
} | ||
|
||
/** | ||
* Find web jar resource path string. | ||
* | ||
* @param path the path | ||
* @return the string | ||
*/ | ||
@Nullable | ||
protected String findWebJarResourcePath(String path) { | ||
String webjar = webjar(path); | ||
if (webjar.length() > 0) { | ||
String version = swaggerUiConfigProperties.getVersion(); | ||
if (version != null) { | ||
String partialPath = path(webjar, path); | ||
return webjar + File.separator + version + File.separator + partialPath; | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
/** | ||
* Webjar string. | ||
* | ||
* @param path the path | ||
* @return the string | ||
*/ | ||
private String webjar(String path) { | ||
int startOffset = (path.startsWith("/") ? 1 : 0); | ||
int endOffset = path.indexOf('/', 1); | ||
return endOffset != -1 ? path.substring(startOffset, endOffset) : path; | ||
} | ||
|
||
|
||
/** | ||
* Path string. | ||
* | ||
* @param webjar the webjar | ||
* @param path the path | ||
* @return the string | ||
*/ | ||
private String path(String webjar, String path) { | ||
if (path.startsWith(webjar)) { | ||
path = path.substring(webjar.length() + 1); | ||
} | ||
return path; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
springdoc-openapi-common/src/main/resources/springdoc.config.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
springdoc.swagger-ui.version=@swagger-ui.version@ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
springdoc-openapi-ui/src/main/java/org/springdoc/webmvc/ui/SwaggerResourceResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package org.springdoc.webmvc.ui; | ||
|
||
import java.util.List; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
|
||
import org.springdoc.core.SwaggerUiConfigProperties; | ||
import org.springdoc.ui.AbstractSwaggerResourceResolver; | ||
|
||
import org.springframework.core.io.Resource; | ||
import org.springframework.web.servlet.resource.ResourceResolver; | ||
import org.springframework.web.servlet.resource.ResourceResolverChain; | ||
|
||
/** | ||
* The type Web jars version resource resolver. | ||
* | ||
* @author bnasslahsen | ||
*/ | ||
public class SwaggerResourceResolver extends AbstractSwaggerResourceResolver implements ResourceResolver { | ||
|
||
/** | ||
* Instantiates a new Web jars version resource resolver. | ||
* | ||
* @param swaggerUiConfigProperties the swagger ui config properties | ||
*/ | ||
public SwaggerResourceResolver(SwaggerUiConfigProperties swaggerUiConfigProperties) { | ||
super(swaggerUiConfigProperties); | ||
} | ||
|
||
@Override | ||
public Resource resolveResource(HttpServletRequest request, String requestPath, List<? extends Resource> locations, ResourceResolverChain chain) { | ||
Resource resolved = chain.resolveResource(request, requestPath, locations); | ||
if (resolved == null) { | ||
String webJarResourcePath = findWebJarResourcePath(requestPath); | ||
if (webJarResourcePath != null) | ||
return chain.resolveResource(request, webJarResourcePath, locations); | ||
} | ||
return resolved; } | ||
|
||
@Override | ||
public String resolveUrlPath(String resourcePath, List<? extends Resource> locations, ResourceResolverChain chain) { | ||
String path = chain.resolveUrlPath(resourcePath, locations); | ||
if (path == null) { | ||
String webJarResourcePath = findWebJarResourcePath(resourcePath); | ||
if (webJarResourcePath != null) | ||
return chain.resolveUrlPath(webJarResourcePath, locations); | ||
} | ||
return path; | ||
} | ||
} |
Oops, something went wrong.