From e02115f4bd7cdb3880286e96f169d864e4c9caf0 Mon Sep 17 00:00:00 2001 From: Harald Seltner Date: Tue, 21 Mar 2023 20:25:35 +0100 Subject: [PATCH] Add failing test with multiple locales and customizer accessing getServers() --- .../springdoc/api/app189/HelloController.java | 46 +++++++++++++ .../api/app189/SpringDocApp189Test.java | 55 +++++++++++++++ .../api/app189/SpringDocTestApp.java | 57 ++++++++++++++++ .../src/test/resources/results/app189.json | 67 +++++++++++++++++++ 4 files changed, 225 insertions(+) create mode 100644 springdoc-openapi-webflux-core/src/test/java/test/org/springdoc/api/app189/HelloController.java create mode 100644 springdoc-openapi-webflux-core/src/test/java/test/org/springdoc/api/app189/SpringDocApp189Test.java create mode 100644 springdoc-openapi-webflux-core/src/test/java/test/org/springdoc/api/app189/SpringDocTestApp.java create mode 100644 springdoc-openapi-webflux-core/src/test/resources/results/app189.json diff --git a/springdoc-openapi-webflux-core/src/test/java/test/org/springdoc/api/app189/HelloController.java b/springdoc-openapi-webflux-core/src/test/java/test/org/springdoc/api/app189/HelloController.java new file mode 100644 index 000000000..78083fd47 --- /dev/null +++ b/springdoc-openapi-webflux-core/src/test/java/test/org/springdoc/api/app189/HelloController.java @@ -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.api.app189; + +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.Parameter; +import io.swagger.v3.oas.annotations.media.Content; +import io.swagger.v3.oas.annotations.responses.ApiResponse; +import io.swagger.v3.oas.annotations.responses.ApiResponses; +import org.springframework.http.MediaType; +import org.springframework.http.codec.multipart.FilePart; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestPart; +import org.springframework.web.bind.annotation.RestController; +import reactor.core.publisher.Mono; + + +@RestController +public class HelloController { + + + @Operation(summary = "Parse Resume") + @PostMapping(value = "/parse-resume", produces = { MediaType.APPLICATION_JSON_VALUE }, consumes = { + MediaType.MULTIPART_FORM_DATA_VALUE }) + @ApiResponses({ @ApiResponse(responseCode = "400", description = "Invalid input") }) + public Mono parse( + @RequestPart(name = "resumeFile") @Parameter(description = "Resume file to be parsed", content = @Content(mediaType = MediaType.APPLICATION_OCTET_STREAM_VALUE)) FilePart resumeFile) { + return null; + } +} diff --git a/springdoc-openapi-webflux-core/src/test/java/test/org/springdoc/api/app189/SpringDocApp189Test.java b/springdoc-openapi-webflux-core/src/test/java/test/org/springdoc/api/app189/SpringDocApp189Test.java new file mode 100644 index 000000000..bebb73da2 --- /dev/null +++ b/springdoc-openapi-webflux-core/src/test/java/test/org/springdoc/api/app189/SpringDocApp189Test.java @@ -0,0 +1,55 @@ +/* + * + * * 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.api.app189; + +import org.json.JSONException; +import org.junit.jupiter.api.Test; +import org.springdoc.core.Constants; +import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest; +import org.springframework.test.web.reactive.server.EntityExchangeResult; +import test.org.springdoc.api.AbstractCommonTest; + +import java.time.Duration; + +import static org.skyscreamer.jsonassert.JSONAssert.assertEquals; + +@WebFluxTest +public class SpringDocApp189Test extends AbstractCommonTest { + + @Test + public void testWithDifferentLocales() throws Exception { + runTestWithLocale("en-GB"); + runTestWithLocale("de-DE"); + } + + private void runTestWithLocale(String locale) throws JSONException { + EntityExchangeResult getResult = webTestClient.mutate().responseTimeout(Duration.ofMinutes(1000)).build() + .get().uri(Constants.DEFAULT_API_DOCS_URL) + .header("Accept-Language", locale) + .exchange() + .expectStatus().isOk().expectBody().returnResult(); + + String result = new String(getResult.getResponseBody()); + String className = getClass().getSimpleName(); + String testNumber = className.replaceAll("[^0-9]", ""); + String expected = getContent("results/app" + testNumber + ".json"); + assertEquals(expected, result, true); + } + +} diff --git a/springdoc-openapi-webflux-core/src/test/java/test/org/springdoc/api/app189/SpringDocTestApp.java b/springdoc-openapi-webflux-core/src/test/java/test/org/springdoc/api/app189/SpringDocTestApp.java new file mode 100644 index 000000000..5de65f5dd --- /dev/null +++ b/springdoc-openapi-webflux-core/src/test/java/test/org/springdoc/api/app189/SpringDocTestApp.java @@ -0,0 +1,57 @@ +/* + * + * * 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.api.app189; + +import io.swagger.v3.oas.models.Components; +import io.swagger.v3.oas.models.OpenAPI; +import io.swagger.v3.oas.models.info.Info; +import io.swagger.v3.oas.models.info.License; +import io.swagger.v3.oas.models.security.SecurityScheme; +import org.springdoc.core.customizers.OpenApiCustomiser; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; + +@SpringBootApplication +@ComponentScan(basePackages = { "org.springdoc", "test.org.springdoc.api.app189" }) +public class SpringDocTestApp { + public static void main(String[] args) { + SpringApplication.run(SpringDocTestApp.class, args); + } + + @Bean + public OpenAPI customOpenAPI() { + return new OpenAPI() + .components(new Components().addSecuritySchemes("basicScheme", + new SecurityScheme().type(SecurityScheme.Type.HTTP).scheme("basic"))) + .info(new Info().title("Tweet API").version("v0") + .license(new License().name("Apache 2.0").url("http://springdoc.org"))); + } + + @Bean + OpenApiCustomiser serverUrlCustomizer() { + return openApi -> + openApi.getServers().forEach(server -> { + server.setDescription("customized description"); + server.setUrl("https://customized.url"); + System.out.println(server); + }); + } +} diff --git a/springdoc-openapi-webflux-core/src/test/resources/results/app189.json b/springdoc-openapi-webflux-core/src/test/resources/results/app189.json new file mode 100644 index 000000000..99a636d30 --- /dev/null +++ b/springdoc-openapi-webflux-core/src/test/resources/results/app189.json @@ -0,0 +1,67 @@ +{ + "openapi": "3.0.1", + "info": { + "title": "Tweet API", + "license": { + "name": "Apache 2.0", + "url": "http://springdoc.org" + }, + "version": "v0" + }, + "servers": [ + { + "url": "https://customized.url", + "description": "customized description" + } + ], + "paths": { + "/parse-resume": { + "post": { + "tags": [ + "hello-controller" + ], + "summary": "Parse Resume", + "operationId": "parse", + "requestBody": { + "content": { + "multipart/form-data": { + "schema": { + "required": [ + "resumeFile" + ], + "type": "object", + "properties": { + "resumeFile": { + "type": "string", + "description": "Resume file to be parsed", + "format": "binary" + } + } + } + } + } + }, + "responses": { + "400": { + "description": "Invalid input", + "content": { + "application/json": { + "schema": { + "type": "string" + } + } + } + } + } + } + } + }, + "components": { + "securitySchemes": { + "basicScheme": { + "type": "http", + "scheme": "basic" + } + } + } +}