Skip to content

Commit edd1b98

Browse files
committed
code review
1 parent 8bd76b2 commit edd1b98

File tree

5 files changed

+26
-22
lines changed

5 files changed

+26
-22
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,7 @@ public static void addHiddenRestControllers(String... classes) {
295295
*/
296296
protected synchronized OpenAPI getOpenApi(Locale locale) {
297297
OpenAPI openApi;
298+
locale = locale == null ? Locale.getDefault() : locale;
298299
if (openAPIService.getCachedOpenAPI(locale) == null || springDocConfigProperties.isCacheDisabled()) {
299300
Instant start = Instant.now();
300301
openAPIService.build(locale);
@@ -1110,6 +1111,7 @@ else if (existingOperation != null) {
11101111
* Init open api builder.
11111112
*/
11121113
protected void initOpenAPIBuilder(Locale locale) {
1114+
locale = locale == null ? Locale.getDefault() : locale;
11131115
if (openAPIService.getCachedOpenAPI(locale) != null && springDocConfigProperties.isCacheDisabled()) {
11141116
openAPIService = openAPIBuilderObjectFactory.getObject();
11151117
}

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

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,7 @@ public class OpenAPIService {
127127
private OpenAPI openAPI;
128128

129129
/**
130-
* The Cached open api.
131-
*/
132-
/**
133-
* The Mappings map.
130+
* The Cached open api map.
134131
*/
135132
private final Map<String, OpenAPI> cachedOpenAPI = new HashMap<>();
136133

@@ -306,8 +303,8 @@ public Operation buildTags(HandlerMethod handlerMethod, Operation operation, Ope
306303
Set<io.swagger.v3.oas.models.tags.Tag> tags = new HashSet<>();
307304
Set<String> tagsStr = new HashSet<>();
308305

309-
buildTagsFromClass(handlerMethod.getBeanType(), tags, tagsStr,locale);
310-
buildTagsFromMethod(handlerMethod.getMethod(), tags, tagsStr,locale);
306+
buildTagsFromClass(handlerMethod.getBeanType(), tags, tagsStr, locale);
307+
buildTagsFromMethod(handlerMethod.getMethod(), tags, tagsStr, locale);
311308

312309
if (!CollectionUtils.isEmpty(tagsStr))
313310
tagsStr = tagsStr.stream()
@@ -732,11 +729,7 @@ public Map<String, Object> getControllerAdviceMap() {
732729
* @return the cached open api
733730
*/
734731
public OpenAPI getCachedOpenAPI(Locale locale) {
735-
Locale cachedLocale = (locale == null ? Locale.getDefault() : locale);
736-
if (cachedOpenAPI != null && cachedOpenAPI.containsKey(cachedLocale.getLanguage())) {
737-
return cachedOpenAPI.get(cachedLocale.getLanguage());
738-
}
739-
return null;
732+
return cachedOpenAPI.get(locale.getLanguage());
740733
}
741734

742735
/**
@@ -746,10 +739,7 @@ public OpenAPI getCachedOpenAPI(Locale locale) {
746739
* @param cachedOpenAPI the cached open api
747740
*/
748741
public void setCachedOpenAPI(OpenAPI cachedOpenAPI, Locale locale) {
749-
Locale cachedLocale = (locale == null ? Locale.getDefault() : locale);
750-
if (this.cachedOpenAPI != null) {
751-
this.cachedOpenAPI.put(cachedLocale.getLanguage(), cachedOpenAPI);
752-
}
742+
this.cachedOpenAPI.put(locale.getLanguage(), cachedOpenAPI);
753743
}
754744

755745
/**

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
package org.springdoc.api;
2222

23+
import java.util.HashMap;
2324
import java.util.List;
2425
import java.util.Locale;
2526
import java.util.Map;
@@ -52,6 +53,7 @@
5253

5354
import org.springframework.beans.factory.ObjectFactory;
5455
import org.springframework.context.ApplicationContext;
56+
import org.springframework.test.util.ReflectionTestUtils;
5557
import org.springframework.web.bind.annotation.RequestMethod;
5658

5759
import static java.util.Arrays.asList;
@@ -100,6 +102,7 @@ class AbstractOpenApiResourceTest {
100102
public void setUp() {
101103
openAPI = new OpenAPI();
102104
openAPI.setPaths(new Paths().addPathItem(PATH, new PathItem()));
105+
ReflectionTestUtils.setField(openAPIService, "cachedOpenAPI", new HashMap<>());
103106

104107
when(openAPIService.getCalculatedOpenAPI()).thenReturn(openAPI);
105108
when(openAPIService.getContext()).thenReturn(context);

springdoc-openapi-webmvc-core/src/test/java/test/org/springdoc/api/app161/SpringDocApp161Test.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package test.org.springdoc.api.app161;
22

3+
import java.util.Locale;
4+
5+
import org.junit.jupiter.api.Test;
36
import test.org.springdoc.api.AbstractSpringDocTest;
47

58
import org.springframework.boot.autoconfigure.SpringBootApplication;
@@ -9,5 +12,10 @@ public class SpringDocApp161Test extends AbstractSpringDocTest {
912
@SpringBootApplication
1013
static class SpringDocTestApp {}
1114

15+
@Test
16+
public void testApp() throws Exception {
17+
Locale.setDefault(Locale.US);
18+
super.testApp();
19+
}
1220

1321
}

springdoc-openapi-webmvc-core/src/test/java/test/org/springdoc/api/app171/SpringDocApp171Test.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,22 @@
1818

1919
package test.org.springdoc.api.app171;
2020

21-
import static org.hamcrest.Matchers.is;
22-
import static org.skyscreamer.jsonassert.JSONAssert.assertEquals;
23-
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
24-
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
25-
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
26-
2721
import java.util.Locale;
2822

2923
import org.junit.jupiter.api.Test;
3024
import org.springdoc.core.Constants;
25+
import test.org.springdoc.api.AbstractSpringDocTest;
26+
3127
import org.springframework.boot.autoconfigure.SpringBootApplication;
3228
import org.springframework.http.HttpHeaders;
3329
import org.springframework.test.context.TestPropertySource;
3430
import org.springframework.test.web.servlet.MvcResult;
3531

36-
import test.org.springdoc.api.AbstractSpringDocTest;
32+
import static org.hamcrest.Matchers.is;
33+
import static org.skyscreamer.jsonassert.JSONAssert.assertEquals;
34+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
35+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
36+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
3737

3838
@TestPropertySource(properties = Constants.SPRINGDOC_CACHE_DISABLED + "=false")
3939
public class SpringDocApp171Test extends AbstractSpringDocTest {
@@ -45,6 +45,7 @@ static class SpringDocTestApp {
4545
@Test
4646
@Override
4747
public void testApp() throws Exception {
48+
Locale.setDefault(Locale.US);
4849
testApp(Locale.US);
4950
testApp(Locale.FRANCE);
5051
}

0 commit comments

Comments
 (0)