Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
public abstract class AbstractSwaggerUiConfigProperties {

/**
* The path for the Swagger UI pages to load. Will redirect to the springdoc.webjars.prefix property.
* The path for the Swagger UI pages to load.
*/
protected String path = Constants.DEFAULT_SWAGGER_UI_PATH;

Expand Down Expand Up @@ -814,4 +814,4 @@ public String toString() {
}
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
import org.springframework.context.annotation.Lazy;
import org.springframework.http.MediaType;

import static org.springdoc.core.utils.Constants.DEFAULT_WEB_JARS_PREFIX_URL;
import static org.springdoc.core.utils.Constants.SPRINGDOC_ENABLED;

/**
Expand All @@ -64,11 +63,6 @@ public class SpringDocConfigProperties {
*/
private boolean showActuator;

/**
* The Webjars.
*/
private Webjars webjars = new Webjars();

/**
* The Api docs.
*/
Expand Down Expand Up @@ -786,24 +780,6 @@ public void setShowActuator(boolean showActuator) {
this.showActuator = showActuator;
}

/**
* Gets webjars.
*
* @return the webjars
*/
public Webjars getWebjars() {
return webjars;
}

/**
* Sets webjars.
*
* @param webjars the webjars
*/
public void setWebjars(Webjars webjars) {
this.webjars = webjars;
}

/**
* Gets api docs.
*
Expand Down Expand Up @@ -1390,36 +1366,6 @@ public void setEnabled(boolean enabled) {
}
}

/**
* The type Webjars.
*
* @author bnasslahsen
*/
public static class Webjars {
/**
* The Prefix.
*/
private String prefix = DEFAULT_WEB_JARS_PREFIX_URL;

/**
* Gets prefix.
*
* @return the prefix
*/
public String getPrefix() {
return prefix;
}

/**
* Sets prefix.
*
* @param prefix the prefix
*/
public void setPrefix(String prefix) {
this.prefix = prefix;
}
}

/**
* The type Api docs.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@

package org.springdoc.core.utils;

import org.springframework.util.ResourceUtils;

import static org.springframework.util.AntPathMatcher.DEFAULT_PATH_SEPARATOR;
import static org.springframework.util.ResourceUtils.CLASSPATH_URL_PREFIX;

/**
* The type Constants.
Expand Down Expand Up @@ -70,7 +69,7 @@ public final class Constants {
/**
* The constant SWAGGER_CONFIG_URL.
*/
public static final String SWAGGER_CONFIG_URL = API_DOCS_URL + DEFAULT_PATH_SEPARATOR + SWAGGER_CONFIG_FILE;
public static final String SWAGGER_CONFIG_URL = API_DOCS_URL + "/" + SWAGGER_CONFIG_FILE;

/**
* The constant YAML.
Expand Down Expand Up @@ -177,21 +176,25 @@ public final class Constants {
*/
public static final String SPRINGDOC_ACTUATOR_DOC_DESCRIPTION = "Spring Boot Actuator Web API Documentation";

/**
* The constant DEFAULT_WEB_JARS_PREFIX_URL.
*/
public static final String DEFAULT_WEB_JARS_PREFIX_URL = "/webjars";
/**
* The constant CLASSPATH_RESOURCE_LOCATION.
*/
public static final String CLASSPATH_RESOURCE_LOCATION = CLASSPATH_URL_PREFIX + "META-INF" + DEFAULT_PATH_SEPARATOR + "resources" + DEFAULT_PATH_SEPARATOR;

/**
* The constant CLASSPATH_RESOURCE_LOCATION.
*/
public static final String CLASSPATH_RESOURCE_LOCATION = ResourceUtils.CLASSPATH_URL_PREFIX + "/META-INF/resources";
/**
* The constant WEBJARS_RESOURCE_LOCATION.
*/
public static final String WEBJARS_RESOURCE_LOCATION = CLASSPATH_RESOURCE_LOCATION + "webjars" + DEFAULT_PATH_SEPARATOR;

/**
* The constant SWAGGER_UI_WEBJAR_NAME.
*/
public static final String SWAGGER_UI_WEBJAR_NAME = "swagger-ui";

/**
* The constant SWAGGER_UI_PREFIX.
*/
public static final String SWAGGER_UI_PREFIX = "/swagger-ui";
public static final String SWAGGER_UI_PREFIX = "/" + SWAGGER_UI_WEBJAR_NAME;

/**
* The constant INDEX_PAGE.
Expand Down Expand Up @@ -231,7 +234,7 @@ public final class Constants {
/**
* The constant DEFAULT_SWAGGER_UI_PATH.
*/
public static final String DEFAULT_SWAGGER_UI_PATH = DEFAULT_PATH_SEPARATOR + "swagger-ui.html";
public static final String DEFAULT_SWAGGER_UI_PATH = "/swagger-ui.html";

/**
* The constant SWAGGER_UI_PATH.
Expand Down Expand Up @@ -363,6 +366,21 @@ public final class Constants {
*/
public static final String ALL_PATTERN = "/**";

/**
* The constant SWAGGER_UI_WEBJAR_NAME_PATTERN.
*/
public static final String SWAGGER_UI_WEBJAR_NAME_PATTERN = "/*" + SWAGGER_UI_WEBJAR_NAME;

/**
* The constant SWAGGER_INITIALIZER_PATTERN.
*/
public static final String SWAGGER_INITIALIZER_PATTERN = "/*" + SWAGGER_INITIALIZER_JS;

/**
* The constant SWAGGER_RESOURCE_CACHE_NAME.
*/
public static final String SWAGGER_RESOURCE_CACHE_NAME = "swagger-resource-chain-cache";

/**
* The constant HEALTH_PATTERN.
*/
Expand Down Expand Up @@ -397,7 +415,7 @@ public final class Constants {
/**
* The constant DEFAULT_YAML_API_DOCS_ACTUATOR_PATH.
*/
public static final String DEFAULT_YAML_API_DOCS_ACTUATOR_PATH = DEFAULT_PATH_SEPARATOR + YAML;
public static final String DEFAULT_YAML_API_DOCS_ACTUATOR_PATH = "/" + YAML;

/**
* The constant ACTUATOR_DEFAULT_GROUP.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
package org.springdoc.ui;

import org.springdoc.core.properties.SwaggerUiConfigProperties;
import org.springframework.boot.autoconfigure.web.WebProperties;
import org.springframework.web.util.pattern.PathPattern;
import org.springframework.web.util.pattern.PathPatternParser;
import org.springframework.web.util.pattern.PatternParseException;

import java.util.Arrays;

import static org.springdoc.core.utils.Constants.ALL_PATTERN;
import static org.springdoc.core.utils.Constants.SWAGGER_INITIALIZER_PATTERN;
import static org.springdoc.core.utils.Constants.SWAGGER_UI_PREFIX;
import static org.springdoc.core.utils.Constants.SWAGGER_UI_WEBJAR_NAME;
import static org.springdoc.core.utils.Constants.SWAGGER_UI_WEBJAR_NAME_PATTERN;
import static org.springdoc.core.utils.Constants.WEBJARS_RESOURCE_LOCATION;
import static org.springframework.util.AntPathMatcher.DEFAULT_PATH_SEPARATOR;

/**
* The type Abstract swagger configurer.
*/
public abstract class AbstractSwaggerConfigurer {

/**
* The Swagger ui config properties.
*/
private final SwaggerUiConfigProperties swaggerUiConfigProperties;

/**
* The Spring Web config properties.
*/
private final WebProperties springWebProperties;

/**
* The path pattern parser.
*/
private final PathPatternParser parser = new PathPatternParser();

/**
* Instantiates a new Abstract swagger configurer.
*
* @param swaggerUiConfigProperties the swagger ui calculated config
* @param springWebProperties the spring web config
*/
protected AbstractSwaggerConfigurer(SwaggerUiConfigProperties swaggerUiConfigProperties, WebProperties springWebProperties) {
this.swaggerUiConfigProperties = swaggerUiConfigProperties;
this.springWebProperties = springWebProperties;
}

/**
* Gets the handler configs for mapping Swagger UI resources.
*
* @return the Swagger UI handler configs.
*/
protected SwaggerResourceHandlerConfig[] getSwaggerHandlerConfigs() {
String swaggerUiPattern = getUiRootPath() + SWAGGER_UI_PREFIX + ALL_PATTERN;
String swaggerUiInitializerPattern = combinePatterns(swaggerUiPattern, SWAGGER_INITIALIZER_PATTERN);
String swaggerUiResourceLocation = WEBJARS_RESOURCE_LOCATION + SWAGGER_UI_WEBJAR_NAME + DEFAULT_PATH_SEPARATOR +
swaggerUiConfigProperties.getVersion() + DEFAULT_PATH_SEPARATOR;

return new SwaggerResourceHandlerConfig[]{
SwaggerResourceHandlerConfig.createCached()
.setPatterns(swaggerUiPattern)
.setLocations(swaggerUiResourceLocation),
SwaggerResourceHandlerConfig.createUncached()
.setPatterns(swaggerUiInitializerPattern)
.setLocations(swaggerUiResourceLocation)
};
}

/**
* Gets the handler configs for mapping webjar resources for the Swagger UI.
*
* @return the Swagger UI webjar handler configs.
*/
protected SwaggerResourceHandlerConfig[] getSwaggerWebjarHandlerConfigs() {
if (!springWebProperties.getResources().isAddMappings()) return new SwaggerResourceHandlerConfig[]{};

String swaggerUiWebjarPattern = combinePatterns(getWebjarsPathPattern(), SWAGGER_UI_WEBJAR_NAME_PATTERN) + ALL_PATTERN;
String swaggerUiWebjarInitializerPattern = combinePatterns(swaggerUiWebjarPattern, SWAGGER_INITIALIZER_PATTERN);
String swaggerUiWebjarVersionInitializerPattern = combinePatterns(swaggerUiWebjarPattern,
swaggerUiConfigProperties.getVersion() + SWAGGER_INITIALIZER_PATTERN);
String swaggerUiWebjarResourceLocation = WEBJARS_RESOURCE_LOCATION;

return new SwaggerResourceHandlerConfig[]{
SwaggerResourceHandlerConfig.createCached()
.setPatterns(swaggerUiWebjarPattern)
.setLocations(swaggerUiWebjarResourceLocation),
SwaggerResourceHandlerConfig.createUncached()
.setPatterns(swaggerUiWebjarInitializerPattern, swaggerUiWebjarVersionInitializerPattern)
.setLocations(swaggerUiWebjarResourceLocation)
};
}

/**
* Gets the root path for the Swagger UI.
*
* @return the Swagger UI root path.
*/
protected abstract String getUiRootPath();

/**
* Gets the path pattern for webjar resources.
*
* @return the webjars path pattern.
*/
protected abstract String getWebjarsPathPattern();

/**
* Combines pattern strings into a new pattern according to the rules of {@link PathPattern#combine}.
*
* <p>For example:
* <ul>
* <li><code>/webjars/&#42;&#42;</code> + <code>/swagger-ui/&#42;&#42;</code> => <code>/webjars/swagger-ui/&#42;&#42;</code></li>
* <li><code>/documentation/swagger-ui&#42;/&#42;&#42;</code> + <code>/&#42;.js</code> => <code>/documentation/swagger-ui&#42;/&#42;.js</code></li>
* </ul>
*
* @param patterns the patterns to combine.
*
* @return the combination of the patterns strings.
*
* @throws IllegalArgumentException if the patterns cannot be combined.
*
* @see PathPattern#combine
*/
protected String combinePatterns(String... patterns) {
return Arrays.stream(patterns)
.map(this::parsePattern)
.reduce(PathPattern::combine)
.map(PathPattern::getPatternString)
.orElseThrow(IllegalArgumentException::new);
}

/**
* Parses a pattern string as a path pattern.
*
* @param pattern the pattern string.
*
* @return the parsed path pattern.
*
* @throws PatternParseException if the pattern string cannot be parsed.
*/
private PathPattern parsePattern(String pattern) {
return parser.parse(parser.initFullPathPattern(pattern));
}

/**
* The type Swagger resource handler config.
*
* @param cacheResources whether to cache resources.
* @param patterns the patterns to match.
* @param locations the locations to use.
*/
protected record SwaggerResourceHandlerConfig(boolean cacheResources, String[] patterns, String[] locations) {

private SwaggerResourceHandlerConfig(boolean cacheResources) {
this(cacheResources, new String[]{}, new String[]{});
}

/**
* Sets the patterns.
*
* @param patterns the patterns to match.
*
* @return the updated config.
*/
public SwaggerResourceHandlerConfig setPatterns(String... patterns) {
return new SwaggerResourceHandlerConfig(cacheResources, patterns, locations);
}

/**
* Sets the locations.
*
* @param locations the locations to use.
*
* @return the updated config.
*/
public SwaggerResourceHandlerConfig setLocations(String... locations) {
return new SwaggerResourceHandlerConfig(cacheResources, patterns, locations);
}

/**
* Create a Swagger resource handler config with resource caching enabled.
*/
public static SwaggerResourceHandlerConfig createCached() {
return new SwaggerResourceHandlerConfig(true);
}

/**
* Create a Swagger resource handler config with resource caching disabled.
*/
public static SwaggerResourceHandlerConfig createUncached() {
return new SwaggerResourceHandlerConfig(false);
}
}
}
Loading