Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Marker interface for swagger ui index transformer #755

Merged
merged 2 commits into from
Jun 29, 2020
Merged
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 @@ -31,6 +31,8 @@
import org.springdoc.core.Constants;
import org.springdoc.core.SwaggerUiConfigProperties;
import org.springdoc.core.SwaggerUiOAuthProperties;
import org.springframework.core.io.Resource;
import org.springframework.util.CollectionUtils;

/**
* The type Abstract swagger index transformer.
Expand Down Expand Up @@ -109,4 +111,21 @@ protected String readFullyAsString(InputStream inputStream)
protected String overwriteSwaggerDefaultUrl(String html) {
return html.replace(Constants.SWAGGER_UI_DEFAULT_URL, StringUtils.EMPTY);
}

protected boolean hasDefaultTransformations() {
boolean oauth2Configured = !CollectionUtils.isEmpty(swaggerUiOAuthProperties.getConfigParameters());
return oauth2Configured || swaggerUiConfig.isDisableSwaggerDefaultUrl();
}

protected String defaultTransformations(InputStream inputStream) throws IOException {
String html = readFullyAsString(inputStream);
if (!CollectionUtils.isEmpty(swaggerUiOAuthProperties.getConfigParameters())) {
html = addInitOauth(html);
}
if (swaggerUiConfig.isDisableSwaggerDefaultUrl()) {
html = overwriteSwaggerDefaultUrl(html);
}

return html;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ SwaggerWelcome swaggerWelcome(SwaggerUiConfigProperties swaggerUiConfig, SpringD
*/
@Bean
@ConditionalOnMissingBean
SwaggerIndexTransformer indexPageTransformer(SwaggerUiConfigProperties swaggerUiConfig, SwaggerUiOAuthProperties swaggerUiOAuthProperties, ObjectMapper objectMapper) {
SwaggerIndexPageTransformer indexPageTransformer(SwaggerUiConfigProperties swaggerUiConfig, SwaggerUiOAuthProperties swaggerUiOAuthProperties, ObjectMapper objectMapper) {
return new SwaggerIndexTransformer(swaggerUiConfig, swaggerUiOAuthProperties, objectMapper);
}

Expand All @@ -82,7 +82,7 @@ SwaggerIndexTransformer indexPageTransformer(SwaggerUiConfigProperties swaggerUi
*/
@Bean
@ConditionalOnMissingBean
SwaggerWebMvcConfigurer swaggerWebMvcConfigurer(SwaggerUiConfigParameters swaggerUiConfigParameters, SwaggerIndexTransformer swaggerIndexTransformer) {
SwaggerWebMvcConfigurer swaggerWebMvcConfigurer(SwaggerUiConfigParameters swaggerUiConfigParameters, SwaggerIndexPageTransformer swaggerIndexTransformer) {
return new SwaggerWebMvcConfigurer(swaggerUiConfigParameters, swaggerIndexTransformer);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
*
* *
* * * Copyright 2019-2020 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.webmvc.ui;

import org.springframework.web.servlet.resource.ResourceTransformer;

/**
* The type Swagger index page transformer.
* @author pverdage
*/
public interface SwaggerIndexPageTransformer extends ResourceTransformer {

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,14 @@
import org.springframework.core.io.Resource;
import org.springframework.util.AntPathMatcher;
import org.springframework.util.CollectionUtils;
import org.springframework.web.servlet.resource.ResourceTransformer;
import org.springframework.web.servlet.resource.ResourceTransformerChain;
import org.springframework.web.servlet.resource.TransformedResource;

/**
* The type Swagger index transformer.
* @author bnasslahsen
*/
public class SwaggerIndexTransformer extends AbstractSwaggerIndexTransformer implements ResourceTransformer {
public class SwaggerIndexTransformer extends AbstractSwaggerIndexTransformer implements SwaggerIndexPageTransformer {

/**
* Instantiates a new Swagger index transformer.
Expand All @@ -58,20 +57,9 @@ public Resource transform(HttpServletRequest request, Resource resource,
ResourceTransformerChain transformerChain) throws IOException {
final AntPathMatcher antPathMatcher = new AntPathMatcher();
boolean isIndexFound = antPathMatcher.match("**/swagger-ui/**/index.html", resource.getURL().toString());
if (isIndexFound && !CollectionUtils.isEmpty(swaggerUiOAuthProperties.getConfigParameters()) && swaggerUiConfig.isDisableSwaggerDefaultUrl()) {
String html = readFullyAsString(resource.getInputStream());
html = addInitOauth(html);
html = overwriteSwaggerDefaultUrl(html);
return new TransformedResource(resource, html.getBytes());
}
else if (isIndexFound && !CollectionUtils.isEmpty(swaggerUiOAuthProperties.getConfigParameters())) {
String html = readFullyAsString(resource.getInputStream());
html = addInitOauth(html);
return new TransformedResource(resource, html.getBytes());
}
else if (isIndexFound && swaggerUiConfig.isDisableSwaggerDefaultUrl()) {
String html = readFullyAsString(resource.getInputStream());
html = overwriteSwaggerDefaultUrl(html);

if (isIndexFound && hasDefaultTransformations()) {
String html = defaultTransformations(resource.getInputStream());
return new TransformedResource(resource, html.getBytes());
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ public class SwaggerWebMvcConfigurer extends WebMvcConfigurerAdapter { // NOSONA
/**
* The Swagger index transformer.
*/
private SwaggerIndexTransformer swaggerIndexTransformer;
private SwaggerIndexPageTransformer swaggerIndexTransformer;

/**
* Instantiates a new Swagger web mvc configurer.
*
* @param swaggerUiConfigParameters the swagger ui calculated config
* @param swaggerIndexTransformer the swagger index transformer
*/
public SwaggerWebMvcConfigurer(SwaggerUiConfigParameters swaggerUiConfigParameters, SwaggerIndexTransformer swaggerIndexTransformer) {
public SwaggerWebMvcConfigurer(SwaggerUiConfigParameters swaggerUiConfigParameters, SwaggerIndexPageTransformer swaggerIndexTransformer) {
this.swaggerPath = swaggerUiConfigParameters.getPath();
this.swaggerIndexTransformer = swaggerIndexTransformer;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public class SwaggerConfig implements WebFluxConfigurer {
*/
@Bean
@ConditionalOnMissingBean
SwaggerWebFluxConfigurer swaggerWebFluxConfigurer(SwaggerUiConfigParameters swaggerUiConfigParameters, SpringDocConfigProperties springDocConfigProperties, SwaggerIndexTransformer swaggerIndexTransformer) {
SwaggerWebFluxConfigurer swaggerWebFluxConfigurer(SwaggerUiConfigParameters swaggerUiConfigParameters, SpringDocConfigProperties springDocConfigProperties, SwaggerIndexPageTransformer swaggerIndexTransformer) {
return new SwaggerWebFluxConfigurer(swaggerUiConfigParameters, springDocConfigProperties, swaggerIndexTransformer);
}

Expand All @@ -70,7 +70,7 @@ SwaggerWebFluxConfigurer swaggerWebFluxConfigurer(SwaggerUiConfigParameters swag
*/
@Bean
@ConditionalOnMissingBean
SwaggerIndexTransformer indexPageTransformer(SwaggerUiConfigProperties swaggerUiConfig ,SwaggerUiOAuthProperties swaggerUiOAuthProperties, ObjectMapper objectMapper) {
SwaggerIndexPageTransformer indexPageTransformer(SwaggerUiConfigProperties swaggerUiConfig ,SwaggerUiOAuthProperties swaggerUiOAuthProperties, ObjectMapper objectMapper) {
return new SwaggerIndexTransformer(swaggerUiConfig, swaggerUiOAuthProperties, objectMapper);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
*
* *
* * * Copyright 2019-2020 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.webflux.ui;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springdoc.core.SwaggerUiConfigProperties;
import org.springdoc.core.SwaggerUiOAuthProperties;
import org.springdoc.ui.AbstractSwaggerIndexTransformer;
import org.springdoc.ui.SpringDocUIException;
import reactor.core.publisher.Mono;

import org.springframework.core.io.Resource;
import org.springframework.util.AntPathMatcher;
import org.springframework.util.CollectionUtils;
import org.springframework.web.reactive.resource.ResourceTransformer;
import org.springframework.web.reactive.resource.ResourceTransformerChain;
import org.springframework.web.reactive.resource.TransformedResource;
import org.springframework.web.server.ServerWebExchange;

/**
* The type Swagger index transformer.
* @author pverdage
*/
public interface SwaggerIndexPageTransformer extends ResourceTransformer {

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import org.springframework.core.io.Resource;
import org.springframework.util.AntPathMatcher;
import org.springframework.util.CollectionUtils;
import org.springframework.web.reactive.resource.ResourceTransformer;
import org.springframework.web.reactive.resource.ResourceTransformerChain;
import org.springframework.web.reactive.resource.TransformedResource;
import org.springframework.web.server.ServerWebExchange;
Expand All @@ -39,7 +38,7 @@
* The type Swagger index transformer.
* @author bnasslahsen
*/
public class SwaggerIndexTransformer extends AbstractSwaggerIndexTransformer implements ResourceTransformer {
public class SwaggerIndexTransformer extends AbstractSwaggerIndexTransformer implements SwaggerIndexPageTransformer {

/**
* Instantiates a new Swagger index transformer.
Expand All @@ -55,23 +54,11 @@ public SwaggerIndexTransformer(SwaggerUiConfigProperties swaggerUiConfig, Swagge
@Override
public Mono<Resource> transform(ServerWebExchange serverWebExchange, Resource resource, ResourceTransformerChain resourceTransformerChain) {
final AntPathMatcher antPathMatcher = new AntPathMatcher();
boolean isIndexFound = false;

try {
isIndexFound = antPathMatcher.match("**/swagger-ui/**/index.html", resource.getURL().toString());
if (isIndexFound && !CollectionUtils.isEmpty(swaggerUiOAuthProperties.getConfigParameters()) && swaggerUiConfig.isDisableSwaggerDefaultUrl()) {
String html = readFullyAsString(resource.getInputStream());
html = addInitOauth(html);
html = overwriteSwaggerDefaultUrl(html);
return Mono.just(new TransformedResource(resource, html.getBytes()));
}
else if (isIndexFound && !CollectionUtils.isEmpty(swaggerUiOAuthProperties.getConfigParameters())) {
String html = readFullyAsString(resource.getInputStream());
html = addInitOauth(html);
return Mono.just(new TransformedResource(resource, html.getBytes()));
}
else if (isIndexFound && swaggerUiConfig.isDisableSwaggerDefaultUrl()) {
String html = readFullyAsString(resource.getInputStream());
html = overwriteSwaggerDefaultUrl(html);
boolean isIndexFound = antPathMatcher.match("**/swagger-ui/**/index.html", resource.getURL().toString());
if (isIndexFound && hasDefaultTransformations()) {
String html = defaultTransformations(resource.getInputStream());
return Mono.just(new TransformedResource(resource, html.getBytes()));
}
else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public class SwaggerWebFluxConfigurer implements WebFluxConfigurer {
/**
* The Swagger index transformer.
*/
private SwaggerIndexTransformer swaggerIndexTransformer;
private SwaggerIndexPageTransformer swaggerIndexTransformer;

/**
* Instantiates a new Swagger web flux configurer.
Expand All @@ -58,7 +58,7 @@ public class SwaggerWebFluxConfigurer implements WebFluxConfigurer {
* @param springDocConfigProperties the spring doc config properties
* @param swaggerIndexTransformer the swagger index transformer
*/
public SwaggerWebFluxConfigurer(SwaggerUiConfigParameters swaggerUiConfigParameters, SpringDocConfigProperties springDocConfigProperties, SwaggerIndexTransformer swaggerIndexTransformer) {
public SwaggerWebFluxConfigurer(SwaggerUiConfigParameters swaggerUiConfigParameters, SpringDocConfigProperties springDocConfigProperties, SwaggerIndexPageTransformer swaggerIndexTransformer) {
this.swaggerPath = swaggerUiConfigParameters.getPath();
this.webJarsPrefixUrl = springDocConfigProperties.getWebjars().getPrefix();
this.swaggerIndexTransformer = swaggerIndexTransformer;
Expand Down