I find a way to disable default URL on springdoc 1.6.9 with springboot 2.6.6.
I just got the same issue on the latest springdoc version 1.6.9, that it always points to that default petstore swagger URL even the springdoc properties (disable default URL) have been configured.
Thanks to Arpit Agrawal, I find that springdoc change this default URL with the Transformer, in other words, it matches the request URL of the SWAGGER_INITIALIZER_JS = "swagger-initializer.js"
js file which contains the default petstore URL, and then replace the default URL to empty string as html.replace(Constants.SWAGGER_UI_DEFAULT_URL, StringUtils.EMPTY);
in the Transformer.
But on 1.6.9 this snippet code doesn't work since the swagger default URL has been moved into the webjars/swagger-ui/index.html in that swagger-initializer.js file has been merged into index.html, and this js file is removed, then that replacement will never happen.
I have to use 1.6.9 for that it added the feature I need, so I change the code to let the default URL is disabled by overriding the SwaggerIndexTransformer
Bean. The config URL better to set due to the original code can't work.
@Bean
SwaggerIndexTransformer indexPageTransformer(SwaggerUiConfigProperties swaggerUiConfig, SwaggerUiOAuthProperties swaggerUiOAuthProperties,
SwaggerUiConfigParameters swaggerUiConfigParameters, SwaggerWelcomeCommon swaggerWelcomeCommon, ObjectMapperProvider objectMapperProvider) {
return new SwaggerIndexPageTransformer(swaggerUiConfig, swaggerUiOAuthProperties, swaggerUiConfigParameters, swaggerWelcomeCommon, objectMapperProvider){
@Override
public Resource transform(HttpServletRequest request, Resource resource,
ResourceTransformerChain transformerChain) throws IOException {
if (swaggerUiConfigParameters.getConfigUrl() == null){
System.out.println("You should set config url in case geting into this block");
// swaggerWelcomeCommon.buildFromCurrentContextPath(request);
//buildFromCurrentContextPath is not allowed to be invoked.
}
final AntPathMatcher antPathMatcher = new AntPathMatcher();
boolean isIndexFound = antPathMatcher.match("**/swagger-ui/index.html", resource.getURL().toString());
if (isIndexFound) {
String html = defaultTransformations(resource.getInputStream());
return new TransformedResource(resource, html.getBytes());
}
else
return resource;
}
};
}
Put this code in a @Configuration class, it can override that Bean in springdoc jar. And add those properties in application.properties file:
spring.main.allow-bean-definition-overriding=true
springdoc.swagger-ui.disable-swagger-default-url=true
springdoc.swagger-ui.configUrl=/{contextpath}/v3/api-docs/swagger-config
springdoc.swagger-ui.url=/{contextpath}/v3/api-docs