Skip to content

Commit f09fe07

Browse files
committed
Merge branch 'harsel-failing-servers-customizer-with-multiple-locales'
2 parents 542f12f + 85a7457 commit f09fe07

File tree

6 files changed

+222
-0
lines changed

6 files changed

+222
-0
lines changed

springdoc-openapi-common/src/main/java/org/springdoc/api/AbstractOpenApiResource.java

+2
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,8 @@ protected synchronized OpenAPI getOpenApi(Locale locale) {
339339
);
340340
if (!CollectionUtils.isEmpty(openAPI.getServers()))
341341
openAPIService.setServersPresent(true);
342+
else
343+
openAPIService.setServersPresent(false);
342344
openAPIService.updateServers(openAPI);
343345

344346
if (springDocConfigProperties.isRemoveBrokenReferenceDefinitions())

springdoc-openapi-common/src/test/java/org/springdoc/api/AbstractOpenApiResourceTest.java

+1
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ public void setUp() {
120120

121121
when(openAPIService.getContext()).thenReturn(context);
122122
when(openAPIService.build(any())).thenReturn(openAPI);
123+
doAnswer(new CallsRealMethods()).when(openAPIService).setServersPresent(false);
123124

124125
when(openAPIBuilderObjectFactory.getObject()).thenReturn(openAPIService);
125126
when(springDocProviders.jsonMapper()).thenReturn(Json.mapper());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
*
3+
* * Copyright 2019-2020 the original author or authors.
4+
* *
5+
* * Licensed under the Apache License, Version 2.0 (the "License");
6+
* * you may not use this file except in compliance with the License.
7+
* * You may obtain a copy of the License at
8+
* *
9+
* * https://www.apache.org/licenses/LICENSE-2.0
10+
* *
11+
* * Unless required by applicable law or agreed to in writing, software
12+
* * distributed under the License is distributed on an "AS IS" BASIS,
13+
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* * See the License for the specific language governing permissions and
15+
* * limitations under the License.
16+
*
17+
*/
18+
19+
package test.org.springdoc.api.app189;
20+
21+
import io.swagger.v3.oas.annotations.Operation;
22+
import io.swagger.v3.oas.annotations.Parameter;
23+
import io.swagger.v3.oas.annotations.media.Content;
24+
import io.swagger.v3.oas.annotations.responses.ApiResponse;
25+
import io.swagger.v3.oas.annotations.responses.ApiResponses;
26+
import org.springframework.http.MediaType;
27+
import org.springframework.http.codec.multipart.FilePart;
28+
import org.springframework.web.bind.annotation.PostMapping;
29+
import org.springframework.web.bind.annotation.RequestPart;
30+
import org.springframework.web.bind.annotation.RestController;
31+
import reactor.core.publisher.Mono;
32+
33+
34+
@RestController
35+
public class HelloController {
36+
37+
38+
@Operation(summary = "Parse Resume")
39+
@PostMapping(value = "/parse-resume", produces = { MediaType.APPLICATION_JSON_VALUE }, consumes = {
40+
MediaType.MULTIPART_FORM_DATA_VALUE })
41+
@ApiResponses({ @ApiResponse(responseCode = "400", description = "Invalid input") })
42+
public Mono<String> parse(
43+
@RequestPart(name = "resumeFile") @Parameter(description = "Resume file to be parsed", content = @Content(mediaType = MediaType.APPLICATION_OCTET_STREAM_VALUE)) FilePart resumeFile) {
44+
return null;
45+
}
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
*
3+
* * Copyright 2019-2020 the original author or authors.
4+
* *
5+
* * Licensed under the Apache License, Version 2.0 (the "License");
6+
* * you may not use this file except in compliance with the License.
7+
* * You may obtain a copy of the License at
8+
* *
9+
* * https://www.apache.org/licenses/LICENSE-2.0
10+
* *
11+
* * Unless required by applicable law or agreed to in writing, software
12+
* * distributed under the License is distributed on an "AS IS" BASIS,
13+
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* * See the License for the specific language governing permissions and
15+
* * limitations under the License.
16+
*
17+
*/
18+
19+
package test.org.springdoc.api.app189;
20+
21+
import org.json.JSONException;
22+
import org.junit.jupiter.api.Test;
23+
import org.springdoc.core.Constants;
24+
import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest;
25+
import org.springframework.test.web.reactive.server.EntityExchangeResult;
26+
import test.org.springdoc.api.AbstractCommonTest;
27+
28+
import java.time.Duration;
29+
30+
import static org.skyscreamer.jsonassert.JSONAssert.assertEquals;
31+
32+
@WebFluxTest
33+
public class SpringDocApp189Test extends AbstractCommonTest {
34+
35+
@Test
36+
public void testWithDifferentLocales() throws Exception {
37+
runTestWithLocale("en-GB");
38+
runTestWithLocale("de-DE");
39+
}
40+
41+
private void runTestWithLocale(String locale) throws JSONException {
42+
EntityExchangeResult<byte[]> getResult = webTestClient.mutate().responseTimeout(Duration.ofMinutes(1000)).build()
43+
.get().uri(Constants.DEFAULT_API_DOCS_URL)
44+
.header("Accept-Language", locale)
45+
.exchange()
46+
.expectStatus().isOk().expectBody().returnResult();
47+
48+
String result = new String(getResult.getResponseBody());
49+
String className = getClass().getSimpleName();
50+
String testNumber = className.replaceAll("[^0-9]", "");
51+
String expected = getContent("results/app" + testNumber + ".json");
52+
assertEquals(expected, result, true);
53+
}
54+
55+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
*
3+
* * Copyright 2019-2020 the original author or authors.
4+
* *
5+
* * Licensed under the Apache License, Version 2.0 (the "License");
6+
* * you may not use this file except in compliance with the License.
7+
* * You may obtain a copy of the License at
8+
* *
9+
* * https://www.apache.org/licenses/LICENSE-2.0
10+
* *
11+
* * Unless required by applicable law or agreed to in writing, software
12+
* * distributed under the License is distributed on an "AS IS" BASIS,
13+
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* * See the License for the specific language governing permissions and
15+
* * limitations under the License.
16+
*
17+
*/
18+
19+
package test.org.springdoc.api.app189;
20+
21+
import io.swagger.v3.oas.models.Components;
22+
import io.swagger.v3.oas.models.OpenAPI;
23+
import io.swagger.v3.oas.models.info.Info;
24+
import io.swagger.v3.oas.models.info.License;
25+
import io.swagger.v3.oas.models.security.SecurityScheme;
26+
import org.springdoc.core.customizers.OpenApiCustomiser;
27+
import org.springframework.boot.autoconfigure.SpringBootApplication;
28+
import org.springframework.context.annotation.Bean;
29+
import org.springframework.context.annotation.ComponentScan;
30+
31+
@SpringBootApplication
32+
@ComponentScan(basePackages = { "org.springdoc", "test.org.springdoc.api.app189" })
33+
public class SpringDocTestApp {
34+
@Bean
35+
public OpenAPI customOpenAPI() {
36+
return new OpenAPI()
37+
.components(new Components().addSecuritySchemes("basicScheme",
38+
new SecurityScheme().type(SecurityScheme.Type.HTTP).scheme("basic")))
39+
.info(new Info().title("Tweet API").version("v0")
40+
.license(new License().name("Apache 2.0").url("http://springdoc.org")));
41+
}
42+
43+
@Bean
44+
OpenApiCustomiser serverUrlCustomizer() {
45+
return openApi ->
46+
openApi.getServers().forEach(server -> {
47+
server.setDescription("customized description");
48+
server.setUrl("https://customized.url");
49+
});
50+
}
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
{
2+
"openapi": "3.0.1",
3+
"info": {
4+
"title": "Tweet API",
5+
"license": {
6+
"name": "Apache 2.0",
7+
"url": "http://springdoc.org"
8+
},
9+
"version": "v0"
10+
},
11+
"servers": [
12+
{
13+
"url": "https://customized.url",
14+
"description": "customized description"
15+
}
16+
],
17+
"paths": {
18+
"/parse-resume": {
19+
"post": {
20+
"tags": [
21+
"hello-controller"
22+
],
23+
"summary": "Parse Resume",
24+
"operationId": "parse",
25+
"requestBody": {
26+
"content": {
27+
"multipart/form-data": {
28+
"schema": {
29+
"required": [
30+
"resumeFile"
31+
],
32+
"type": "object",
33+
"properties": {
34+
"resumeFile": {
35+
"type": "string",
36+
"description": "Resume file to be parsed",
37+
"format": "binary"
38+
}
39+
}
40+
}
41+
}
42+
}
43+
},
44+
"responses": {
45+
"400": {
46+
"description": "Invalid input",
47+
"content": {
48+
"application/json": {
49+
"schema": {
50+
"type": "string"
51+
}
52+
}
53+
}
54+
}
55+
}
56+
}
57+
}
58+
},
59+
"components": {
60+
"securitySchemes": {
61+
"basicScheme": {
62+
"type": "http",
63+
"scheme": "basic"
64+
}
65+
}
66+
}
67+
}

0 commit comments

Comments
 (0)