Skip to content

Commit

Permalink
Prevent swagger-config from being loaded twice in case of no groups. F…
Browse files Browse the repository at this point in the history
…ixes #688, #349, #545.
  • Loading branch information
bnasslahsen committed Jun 7, 2020
1 parent 2f7c4c6 commit 46dff5b
Show file tree
Hide file tree
Showing 6 changed files with 231 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ public class SwaggerUiConfigProperties {

public static final String FILTER_PROPERTY = "filter";

public static final String URLS_PROPERTY = "urls";

public static final String OAUTH2_REDIRECT_URL_PROPERTY = "oauth2RedirectUrl";
/**
* The path for the Swagger UI pages to load. Will redirect to the springdoc.webjars.prefix property.
*/
Expand Down Expand Up @@ -169,6 +172,10 @@ public class SwaggerUiConfigProperties {

private boolean disableSwaggerDefaultUrl;

private boolean displayQueryParams;

private boolean displayQueryParamsWithoutOauth2;

public void addGroup(String group) {
SwaggerUrl swaggerUrl = new SwaggerUrl(group);
urls.add(swaggerUrl);
Expand Down Expand Up @@ -212,7 +219,7 @@ public Map<String, Object> getConfigParameters() {
SpringDocPropertiesUtils.put("supportedSubmitMethods", supportedSubmitMethods.toString(), params);
SpringDocPropertiesUtils.put("oauth2RedirectUrl", oauth2RedirectUrl, params);
SpringDocPropertiesUtils.put("url", url, params);
put("urls", urls, params);
put(URLS_PROPERTY, urls, params);
SpringDocPropertiesUtils.put("urls.primaryName", urlsPrimaryName, params);
return params;
}
Expand Down Expand Up @@ -385,6 +392,22 @@ public void setDisableSwaggerDefaultUrl(boolean disableSwaggerDefaultUrl) {
this.disableSwaggerDefaultUrl = disableSwaggerDefaultUrl;
}

public boolean isDisplayQueryParams() {
return displayQueryParams;
}

public void setDisplayQueryParams(boolean displayQueryParams) {
this.displayQueryParams = displayQueryParams;
}

public boolean isDisplayQueryParamsWithoutOauth2() {
return displayQueryParamsWithoutOauth2;
}

public void setDisplayQueryParamsWithoutOauth2(boolean displayQueryParamsWithoutOauth2) {
this.displayQueryParamsWithoutOauth2 = displayQueryParamsWithoutOauth2;
}

public boolean isValidUrl(String url) {
try {
new URL(url).toURI();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,27 @@ protected void buildConfigUrl(String contextPath, UriComponentsBuilder uriCompon

protected UriComponentsBuilder getUriComponentsBuilder(String sbUrl) {
UriComponentsBuilder uriBuilder = UriComponentsBuilder.fromUriString(sbUrl);
uriBuilder.queryParam(SwaggerUiConfigProperties.CONFIG_URL_PROPERTY, swaggerUiConfig.getConfigUrl());
if (StringUtils.isNotEmpty(swaggerUiConfig.getLayout()))
uriBuilder.queryParam(SwaggerUiConfigProperties.LAYOUT_PROPERTY, swaggerUiConfig.getLayout());
if (StringUtils.isNotEmpty(swaggerUiConfig.getFilter()))
uriBuilder.queryParam(SwaggerUiConfigProperties.FILTER_PROPERTY, swaggerUiConfig.getFilter());

if (swaggerUiConfig.isDisplayQueryParams() && StringUtils.isNotEmpty(swaggerUiConfig.getUrl())) {
swaggerUiConfig.getConfigParameters().entrySet().stream()
.filter(entry -> !SwaggerUiConfigProperties.CONFIG_URL_PROPERTY.equals(entry.getKey()))
.filter(entry -> !entry.getKey().startsWith(SwaggerUiConfigProperties.URLS_PROPERTY))
.filter(entry -> StringUtils.isNotEmpty((String) entry.getValue()))
.forEach(entry -> uriBuilder.queryParam(entry.getKey(), entry.getValue()));
} else if (swaggerUiConfig.isDisplayQueryParamsWithoutOauth2() && StringUtils.isNotEmpty(swaggerUiConfig.getUrl())) {
swaggerUiConfig.getConfigParameters().entrySet().stream()
.filter(entry -> !SwaggerUiConfigProperties.CONFIG_URL_PROPERTY.equals(entry.getKey()))
.filter(entry -> !SwaggerUiConfigProperties.OAUTH2_REDIRECT_URL_PROPERTY.equals(entry.getKey()))
.filter(entry -> !entry.getKey().startsWith(SwaggerUiConfigProperties.URLS_PROPERTY))
.filter(entry -> StringUtils.isNotEmpty((String) entry.getValue()))
.forEach(entry -> uriBuilder.queryParam(entry.getKey(), entry.getValue()));
}
else {
uriBuilder.queryParam(SwaggerUiConfigProperties.CONFIG_URL_PROPERTY, swaggerUiConfig.getConfigUrl());
if (StringUtils.isNotEmpty(swaggerUiConfig.getLayout()))
uriBuilder.queryParam(SwaggerUiConfigProperties.LAYOUT_PROPERTY, swaggerUiConfig.getLayout());
if (StringUtils.isNotEmpty(swaggerUiConfig.getFilter()))
uriBuilder.queryParam(SwaggerUiConfigProperties.FILTER_PROPERTY, swaggerUiConfig.getFilter());
}
return uriBuilder;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
*
* * 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 test.org.springdoc.ui.app1;

import org.junit.jupiter.api.Test;
import test.org.springdoc.ui.AbstractSpringDocTest;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.web.servlet.MvcResult;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@TestPropertySource(properties = "springdoc.swagger-ui.display-query-params-without-oauth2=true")
public class SpringDocRedirectQueryParams1Test extends AbstractSpringDocTest {

@Test
public void shouldRedirectWithQueryParamsWithoutOauth2() throws Exception {
MvcResult mvcResult = mockMvc.perform(get("/swagger-ui.html"))
.andExpect(status().isFound()).andReturn();

String locationHeader = mvcResult.getResponse().getHeader("Location");
assertEquals("/swagger-ui/index.html?url=/v3/api-docs", locationHeader);
}

@SpringBootApplication
static class SpringDocTestApp {}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
*
* * 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 test.org.springdoc.ui.app1;

import org.junit.jupiter.api.Test;
import test.org.springdoc.ui.AbstractSpringDocTest;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.web.servlet.MvcResult;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@TestPropertySource(properties = "springdoc.swagger-ui.display-query-params=true")
public class SpringDocRedirectQueryParams2Test extends AbstractSpringDocTest {

@Test
public void shouldRedirectWithQueryParams() throws Exception {
MvcResult mvcResult = mockMvc.perform(get("/swagger-ui.html"))
.andExpect(status().isFound()).andReturn();

String locationHeader = mvcResult.getResponse().getHeader("Location");
assertEquals("/swagger-ui/index.html?oauth2RedirectUrl=http://localhost/swagger-ui/oauth2-redirect.html&url=/v3/api-docs", locationHeader);
}

@SpringBootApplication
static class SpringDocTestApp {}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
*
* * 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 test.org.springdoc.ui.app1;

import org.hamcrest.Matchers;
import org.junit.jupiter.api.Test;
import test.org.springdoc.ui.AbstractSpringDocTest;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.web.reactive.server.WebTestClient;


@TestPropertySource(properties = "springdoc.swagger-ui.display-query-params-without-oauth2=true")
public class SpringDocApp1RedirectQueryParams1Test extends AbstractSpringDocTest {

@Test
public void shouldRedirectWithQueryParamsWithoutOauth2() throws Exception {

WebTestClient.ResponseSpec responseSpec = webTestClient.get().uri("/swagger-ui.html").exchange()
.expectStatus().isTemporaryRedirect();
responseSpec.expectHeader()
.value("Location", Matchers.is("/webjars/swagger-ui/index.html?url=/v3/api-docs"));

}

@SpringBootApplication
static class SpringDocTestApp {}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
*
* * 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 test.org.springdoc.ui.app1;

import org.hamcrest.Matchers;
import org.junit.jupiter.api.Test;
import test.org.springdoc.ui.AbstractSpringDocTest;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.web.reactive.server.WebTestClient;


@TestPropertySource(properties = "springdoc.swagger-ui.display-query-params=true")
public class SpringDocApp1RedirectQueryParams2Test extends AbstractSpringDocTest {

@Test
public void shouldRedirectWithQueryParams() throws Exception {

WebTestClient.ResponseSpec responseSpec = webTestClient.get().uri("/swagger-ui.html").exchange()
.expectStatus().isTemporaryRedirect();
responseSpec.expectHeader()
.value("Location", Matchers.is("/webjars/swagger-ui/index.html?oauth2RedirectUrl=/webjars/swagger-ui/oauth2-redirect.html&url=/v3/api-docs"));

}

@SpringBootApplication
static class SpringDocTestApp {}

}

0 comments on commit 46dff5b

Please sign in to comment.