Comments (13)
Hi @leoherbie, we use the following workaround to prevent serving petstore:
@Configuration
public class SpringdocSwaggerFix implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**/*.html")
.addResourceLocations("classpath:/META-INF/resources/webjars/")
.resourceChain(false)
.addResolver(new WebJarsResourceResolver())
.addResolver(new PathResourceResolver())
.addTransformer(new IndexPageTransformer());
}
public class IndexPageTransformer implements ResourceTransformer {
@Override
public Resource transform(HttpServletRequest request, Resource resource,
ResourceTransformerChain transformerChain) throws IOException {
if (resource.getURL().toString().endsWith("/index.html")) {
String html = getHtmlContent(resource);
html = overwritePetStore(html);
return new TransformedResource(resource, html.getBytes());
} else {
return resource;
}
}
private String overwritePetStore(String html) {
return html.replace("https://petstore.swagger.io/v2/swagger.json",
"/v3/api-docs");
}
}
from springdoc-openapi.
should be option available for replace default page instead
from springdoc-openapi.
It's also noteworthy that there is a difference between http://localhost:8085/swagger-ui/index.html
and http://localhost:8085/swagger-ui.html
. The latter URL redirects to the former URL (the actual swagger-ui) with the configured configUrl
-parameter e.g. http://localhost:8085/swagger-ui/index.html?configUrl=/v3/api-docs/swagger-config
from springdoc-openapi.
To disable the swagger-ui default petstore url, just use the following property:
springdoc.swagger-ui.disable-swagger-default-url=true
More details, are available on the springdoc-openapi properties list:
from springdoc-openapi.
Hi @anksngp
Sure:
private String getHtmlContent(Resource resource) {
try {
InputStream inputStream = resource.getInputStream();
java.util.Scanner s = new java.util.Scanner(inputStream).useDelimiter("\\A");
String content = s.next();
inputStream.close();
return content;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
from springdoc-openapi.
Hi,
We rely on swagger-ui official webjars. We build our library on top of it.
The index.html is available inside the webjar and it contains a JS call to https://petstore.swagger.io/v2/swagger.json, if url parameter is absent.
You can submit an enhancement on the swagger-ui project:
Also, you can rebuild the webjars for your own needs, if you don't want to have any reference to petsotre.
Please, note that in the normal behavior, users will have access to index.html without query parameters, only if they remove the url query parameter manually.
from springdoc-openapi.
I figured as much. I just wondered if there was a way to prevent it from being served or available. Sounds like there isn't. Bummer.
from springdoc-openapi.
Hi @StephGit I tried the way you have mentioned in the above comment, can you also post the contents of the method getHtmlContent
from springdoc-openapi.
Hi @StephGit , @bnasslahsen
When trying the above snippet I noticed that, the newly generated swagger document on calling just localhost:8080/swagger/swagger-ui/index.html
(after replacing https://petstore.swagger.io/v2/swagger.json
with v3/api-docs
) does not have the application-properties that were set on swagger document like
springdoc.swagger-ui.doc-expansion=none
springdoc.swagger-ui.operationsSorter=alpha
springdoc.swagger-ui.tagsSorter=alpha
Is there any way to get the properties applied to swagger document as well ?
P.S. On calling the complete url http://localhost:8080/swagger/swagger-ui/index.html?configUrl=/v3/api-docs/swagger-config#/
the application properties are applied to the swagger document.
from springdoc-openapi.
I'm using custom pathes for Swagger-UI and api-docs and to following application.yaml solved this issue for me (without using any of the above Workarounds):
springdoc:
api-docs:
enabled: true
path: '/my/custom/path/api-docs'
swagger-ui:
path: '/my/custom/path/swagger-ui'
configUrl: '/my/custom/path/api-docs/swagger-config'
urls:
- name: My Custom API
url: '/my/custom/path/api-docs'
Note the list of URLs under springdoc.swagger-ui.urls
since I have multiple api-docs. If you just have one, replace the urls-part with:
springdoc.swagger-ui.url='/my/custom/path/api-docs'
from springdoc-openapi.
I'm using custom pathes for Swagger-UI and api-docs and to following application.yaml solved this issue for me (without using any of the above Workarounds):
springdoc: api-docs: enabled: true path: '/my/custom/path/api-docs' swagger-ui: path: '/my/custom/path/swagger-ui' configUrl: '/my/custom/path/api-docs/swagger-config' urls: - name: My Custom API url: '/my/custom/path/api-docs'
Note the list of URLs under
springdoc.swagger-ui.urls
since I have multiple api-docs. If you just have one, replace the urls-part with:
springdoc.swagger-ui.url='/my/custom/path/api-docs'
its not working for me, my configuration is as follows
springdoc:
api-docs:
enabled: true
path: /v3/api-docs
swagger-ui:
path: /swagger-ui.html
display-request-duration: false
tags-sorter: alpha
operations-sorter: alpha
defaultModelRendering: example
doc-expansion: none
config-url: /v3/api-docs/swagger-config
url: /v3/api-docs
and as a dependency I am using the below
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>1.3.9</version>
</dependency>
<dependency>
<groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-annotations</artifactId>
<version>2.1.2</version>
</dependency>
from springdoc-openapi.
Hi @leoherbie, we use the following workaround to prevent serving petstore:
@Configuration public class SpringdocSwaggerFix implements WebMvcConfigurer { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/**/*.html") .addResourceLocations("classpath:/META-INF/resources/webjars/") .resourceChain(false) .addResolver(new WebJarsResourceResolver()) .addResolver(new PathResourceResolver()) .addTransformer(new IndexPageTransformer()); } public class IndexPageTransformer implements ResourceTransformer { @Override public Resource transform(HttpServletRequest request, Resource resource, ResourceTransformerChain transformerChain) throws IOException { if (resource.getURL().toString().endsWith("/index.html")) { String html = getHtmlContent(resource); html = overwritePetStore(html); return new TransformedResource(resource, html.getBytes()); } else { return resource; } } private String overwritePetStore(String html) { return html.replace("https://petstore.swagger.io/v2/swagger.json", "/v3/api-docs"); } }
I found this issue. Beware. Checking for ending with /index.html is not enough to disable petstore. You can get there with:
/swagger-ui/index.html?configUrl=
and a few other patterns I've found as well.
from springdoc-openapi.
StephGit's solution works better, because it replaces default page. Instead disabling by config file (disable-swagger-default-url) which makes page look like broken swagger page...
from springdoc-openapi.
Related Issues (20)
- How to configure springdoc with a non-spring-boot project? HOT 3
- ConverterNotFoundException when upgrading from 2.4.0 to 2.5.0
- ClassNotFoundException after trying to use Springdoc in a non-spring-boot application
- Update springdoc-openapi-ui 1.7.0 to 1.8.0 - generate "string" example value
- Not able to exclude the path from custom api.yaml file for a given group name HOT 2
- Enhance SpringDoc documentation for file upload API configuration
- Add a generic way to customize serverBaseUrl based on request headers.
- ParameterObject annotation dosen't work when has RequestPart annotation
- When the openapi version is changed to 3.1.0 from 3.0.3, integer values are assigned the maximum value
- Custom ModelResolver and TypeNameResolver results in unexpected behaviour
- oneOf schema not generated > 2.3.0 HOT 2
- GroupedApi orders by displayName instead of name
- /v3/api-docs response error HOT 1
- Spring Data REST Excerpt projections are missing from spec
- java.lang.ClassNotFoundException: javax.xml.bind.annotation.XmlRootElement HOT 1
- Dynamically define ApiGroups does not work
- Is it bug? objectMapper redefinition causes Swagger Petstore page
- java.lang.NoSuchMethodError: 'io.swagger.v3.oas.annotations.StringToClassMapItem[] io.swagger.v3.oas.annotations.media.Schema.dependentSchemas()'
- Springdoc is adding a 404 status response for all endpoints, even if I don't add it explicitly
- Springdoc programatically dynamic API definition and display it's properties
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from springdoc-openapi.