Skip to content

Commit a5e53a8

Browse files
committed
OperationId generation problem. Fixes #1392
1 parent 99bb4c4 commit a5e53a8

File tree

4 files changed

+122
-2
lines changed

4 files changed

+122
-2
lines changed

springdoc-openapi-common/src/main/java/org/springdoc/core/OpenAPIService.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,10 @@
3838
import java.util.stream.Collectors;
3939
import java.util.stream.Stream;
4040

41+
import com.fasterxml.jackson.core.JsonProcessingException;
4142
import io.swagger.v3.core.jackson.TypeNameResolver;
4243
import io.swagger.v3.core.util.AnnotationsUtils;
44+
import io.swagger.v3.core.util.Json;
4345
import io.swagger.v3.oas.annotations.Hidden;
4446
import io.swagger.v3.oas.annotations.OpenAPIDefinition;
4547
import io.swagger.v3.oas.annotations.security.SecuritySchemes;
@@ -229,8 +231,15 @@ public void build(Locale locale) {
229231
this.calculatedOpenAPI.setComponents(new Components());
230232
this.calculatedOpenAPI.setPaths(new Paths());
231233
}
232-
else
233-
this.calculatedOpenAPI = openAPI;
234+
else {
235+
try {
236+
this.calculatedOpenAPI = Json.mapper()
237+
.readValue(Json.mapper().writeValueAsString(openAPI), OpenAPI.class);
238+
}
239+
catch (JsonProcessingException e) {
240+
LOGGER.warn("Json Processing Exception occurred: {}", e.getMessage());
241+
}
242+
}
234243

235244
if (apiDef.isPresent()) {
236245
buildOpenAPIWithOpenAPIDefinition(calculatedOpenAPI, apiDef.get(), locale);
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package test.org.springdoc.api.app173;
2+
3+
import org.springframework.web.bind.annotation.GetMapping;
4+
import org.springframework.web.bind.annotation.RestController;
5+
6+
@RestController
7+
public class HelloController {
8+
9+
@GetMapping("/test")
10+
public void printHello() {
11+
System.out.println("Hello");
12+
}
13+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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.app173;
20+
21+
import java.util.Locale;
22+
23+
import io.swagger.v3.oas.models.OpenAPI;
24+
import org.junit.jupiter.api.Test;
25+
import org.springdoc.core.Constants;
26+
import test.org.springdoc.api.AbstractSpringDocTest;
27+
28+
import org.springframework.boot.autoconfigure.SpringBootApplication;
29+
import org.springframework.context.annotation.Bean;
30+
import org.springframework.http.HttpHeaders;
31+
import org.springframework.test.web.servlet.MvcResult;
32+
33+
import static org.hamcrest.Matchers.is;
34+
import static org.skyscreamer.jsonassert.JSONAssert.assertEquals;
35+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
36+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
37+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
38+
39+
public class SpringDocApp173Test extends AbstractSpringDocTest {
40+
41+
@SpringBootApplication
42+
static class SpringDocTestApp {
43+
@Bean
44+
public OpenAPI openAPI() {
45+
return new OpenAPI();
46+
}
47+
}
48+
49+
@Test
50+
@Override
51+
public void testApp() throws Exception {
52+
Locale.setDefault(Locale.US);
53+
testApp(Locale.US);
54+
testApp(Locale.FRANCE);
55+
}
56+
57+
private void testApp(Locale locale) throws Exception {
58+
className = getClass().getSimpleName();
59+
String testNumber = className.replaceAll("[^0-9]", "");
60+
MvcResult mockMvcResult =
61+
mockMvc.perform(get(Constants.DEFAULT_API_DOCS_URL).locale(locale).header(HttpHeaders.ACCEPT_LANGUAGE, locale.toLanguageTag())).andExpect(status().isOk())
62+
.andExpect(jsonPath("$.openapi", is("3.0.1"))).andReturn();
63+
String result = mockMvcResult.getResponse().getContentAsString();
64+
String expected = getContent("results/app" + testNumber +".json");
65+
assertEquals(expected, result, true);
66+
}
67+
68+
69+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"openapi": "3.0.1",
3+
"info": {
4+
"title": "OpenAPI definition",
5+
"version": "v0"
6+
},
7+
"servers": [
8+
{
9+
"url": "http://localhost",
10+
"description": "Generated server url"
11+
}
12+
],
13+
"paths": {
14+
"/test": {
15+
"get": {
16+
"tags": [
17+
"hello-controller"
18+
],
19+
"operationId": "printHello",
20+
"responses": {
21+
"200": {
22+
"description": "OK"
23+
}
24+
}
25+
}
26+
}
27+
},
28+
"components": {}
29+
}

0 commit comments

Comments
 (0)