From 190205300295f056eb54be4a70040051b303b0f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Blomqvist?= Date: Thu, 20 Apr 2023 20:42:28 +0200 Subject: [PATCH 1/2] Fix test failing due dirty Spring context I don't fully understand the fix but forcing SpringBootTest runner to not reuse the context solves the issue with SpringDocApp1451Test.java always failing with the following. "WebClientRequestException: Connection refused: localhost/127.0.0.1:9086" BUGFIX --- .../test/org/springdoc/api/app145/SpringDocApp1451Test.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/springdoc-openapi-webflux-core/src/test/java/test/org/springdoc/api/app145/SpringDocApp1451Test.java b/springdoc-openapi-webflux-core/src/test/java/test/org/springdoc/api/app145/SpringDocApp1451Test.java index 940e500cf..2dd46d177 100644 --- a/springdoc-openapi-webflux-core/src/test/java/test/org/springdoc/api/app145/SpringDocApp1451Test.java +++ b/springdoc-openapi-webflux-core/src/test/java/test/org/springdoc/api/app145/SpringDocApp1451Test.java @@ -19,6 +19,7 @@ package test.org.springdoc.api.app145; import org.junit.jupiter.api.Test; +import org.springframework.test.annotation.DirtiesContext; import test.org.springdoc.api.AbstractSpringDocActuatorTest; import org.springframework.boot.autoconfigure.SpringBootApplication; @@ -32,6 +33,7 @@ import static org.skyscreamer.jsonassert.JSONAssert.assertEquals; +@DirtiesContext @SpringBootTest(webEnvironment = WebEnvironment.DEFINED_PORT, properties = { "management.endpoints.web.exposure.include=*", "server.port=55594", From 41a988e7f504d1c4428553139c576181ddf860bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Blomqvist?= Date: Thu, 20 Apr 2023 22:15:57 +0200 Subject: [PATCH 2/2] Add fix for using spec file and generated spec at the same time Being able to use both a spec file and generated spec stopped working in version 1.6.5. The test shows the behavior found 1.6.4 and the fix brings back that behavior. BUGFIX --- .../springdoc/ui/AbstractSwaggerWelcome.java | 5 +- .../ui/app1/SpringDocSwaggerConfigTest.java | 2 + ...rConfigWithBothFileGeneratedSpecsTest.java | 66 +++++++++++++++++++ 3 files changed, 71 insertions(+), 2 deletions(-) create mode 100644 springdoc-openapi-ui/src/test/java/test/org/springdoc/ui/app1/SpringDocSwaggerConfigWithBothFileGeneratedSpecsTest.java diff --git a/springdoc-openapi-common/src/main/java/org/springdoc/ui/AbstractSwaggerWelcome.java b/springdoc-openapi-common/src/main/java/org/springdoc/ui/AbstractSwaggerWelcome.java index a95739c92..089a11b6f 100644 --- a/springdoc-openapi-common/src/main/java/org/springdoc/ui/AbstractSwaggerWelcome.java +++ b/springdoc-openapi-common/src/main/java/org/springdoc/ui/AbstractSwaggerWelcome.java @@ -132,10 +132,11 @@ else if (swaggerUiConfigParameters.isValidUrl(swaggerUiUrl)) else swaggerUiConfigParameters.addUrl(apiDocsUrl); if (!CollectionUtils.isEmpty(swaggerUiConfig.getUrls())) { - swaggerUiConfigParameters.setUrls(swaggerUiConfig.cloneUrls()); - swaggerUiConfigParameters.getUrls().forEach(swaggerUrl -> { + swaggerUiConfig.cloneUrls().forEach(swaggerUrl -> { + swaggerUiConfigParameters.getUrls().remove(swaggerUrl); if (!swaggerUiConfigParameters.isValidUrl(swaggerUrl.getUrl())) swaggerUrl.setUrl(buildUrlWithContextPath(swaggerUrl.getUrl())); + swaggerUiConfigParameters.getUrls().add(swaggerUrl); }); } } diff --git a/springdoc-openapi-ui/src/test/java/test/org/springdoc/ui/app1/SpringDocSwaggerConfigTest.java b/springdoc-openapi-ui/src/test/java/test/org/springdoc/ui/app1/SpringDocSwaggerConfigTest.java index fc02c328f..8380cbfeb 100644 --- a/springdoc-openapi-ui/src/test/java/test/org/springdoc/ui/app1/SpringDocSwaggerConfigTest.java +++ b/springdoc-openapi-ui/src/test/java/test/org/springdoc/ui/app1/SpringDocSwaggerConfigTest.java @@ -19,6 +19,7 @@ package test.org.springdoc.ui.app1; import org.junit.jupiter.api.Test; +import org.springframework.test.annotation.DirtiesContext; import test.org.springdoc.ui.AbstractSpringDocActuatorTest; import org.springframework.boot.autoconfigure.SpringBootApplication; @@ -30,6 +31,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +@DirtiesContext @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, properties = { "spring.jackson.property-naming-strategy=UPPER_CAMEL_CASE", "springdoc.show-actuator=true", "management.endpoints.web.base-path=/management", diff --git a/springdoc-openapi-ui/src/test/java/test/org/springdoc/ui/app1/SpringDocSwaggerConfigWithBothFileGeneratedSpecsTest.java b/springdoc-openapi-ui/src/test/java/test/org/springdoc/ui/app1/SpringDocSwaggerConfigWithBothFileGeneratedSpecsTest.java new file mode 100644 index 000000000..49acca698 --- /dev/null +++ b/springdoc-openapi-ui/src/test/java/test/org/springdoc/ui/app1/SpringDocSwaggerConfigWithBothFileGeneratedSpecsTest.java @@ -0,0 +1,66 @@ +/* + * + * * 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 org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.annotation.DirtiesContext; +import test.org.springdoc.ui.AbstractSpringDocActuatorTest; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +/* + Test showing how specs generated at runtime and specs configured in can work at the same time. + + The expectation is that the openapi.yml file will be shown together with the generated ones. + */ +@DirtiesContext +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, + properties = { "spring.jackson.property-naming-strategy=UPPER_CAMEL_CASE", "springdoc.show-actuator=true", + "management.endpoints.web.base-path=/management", + "server.servlet.context-path=/demo/api", "management.server.port=9002", "management.server.base-path=/demo/api", + "springdoc.swagger-ui.urls[0].url=/api-docs/xxx/v1/openapi.yml", + "springdoc.swagger-ui.urls[0].name=toto", +}) +public class SpringDocSwaggerConfigWithBothFileGeneratedSpecsTest extends AbstractSpringDocActuatorTest { + + @Test + public void testIndexSwaggerConfig() throws Exception { + mockMvc.perform(get("/demo/api/v3/api-docs/swagger-config").contextPath("/demo/api")) + .andExpect(status().isOk()) + .andExpect(jsonPath("validatorUrl", equalTo(""))) + .andExpect(jsonPath("oauth2RedirectUrl", equalTo("http://localhost/demo/api/swagger-ui/oauth2-redirect.html"))) + .andExpect(jsonPath("url").doesNotExist()) + .andExpect(jsonPath("urls[0].url", equalTo("/demo/api/v3/api-docs/springdocDefault"))) + .andExpect(jsonPath("urls[0].name", equalTo("springdocDefault"))) + .andExpect(jsonPath("urls[1].url", equalTo("/demo/api/api-docs/xxx/v1/openapi.yml"))) + .andExpect(jsonPath("urls[1].name", equalTo("toto"))) + .andExpect(jsonPath("urls[2].url", equalTo("/demo/api/v3/api-docs/x-actuator"))) + .andExpect(jsonPath("urls[2].name", equalTo("x-actuator"))); + } + + + @SpringBootApplication + static class SpringDocTestApp {} + +} \ No newline at end of file