Skip to content

Commit 8348800

Browse files
committed
Merge branch 'feat_preloading_locales' of github.com:uc4w6c/springdoc-openapi into uc4w6c-feat_preloading_locales
2 parents 8b04fec + 5300c49 commit 8348800

File tree

5 files changed

+418
-6
lines changed

5 files changed

+418
-6
lines changed

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

+10-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* *
44
* * *
55
* * * *
6-
* * * * * Copyright 2019-2022 the original author or authors.
6+
* * * * * Copyright 2019-2023 the original author or authors.
77
* * * * *
88
* * * * * Licensed under the Apache License, Version 2.0 (the "License");
99
* * * * * you may not use this file except in compliance with the License.
@@ -226,8 +226,15 @@ protected AbstractOpenApiResource(String groupName, ObjectFactory<OpenAPIService
226226
this.springDocProviders = springDocProviders;
227227
this.springDocCustomizers = springDocCustomizers;
228228
this.springDocConfigProperties = springDocConfigProperties;
229-
if (springDocConfigProperties.isPreLoadingEnabled())
230-
Executors.newSingleThreadExecutor().execute(this::getOpenApi);
229+
if (springDocConfigProperties.isPreLoadingEnabled()) {
230+
if (CollectionUtils.isEmpty(springDocConfigProperties.getPreLoadingLocales())) {
231+
Executors.newSingleThreadExecutor().execute(this::getOpenApi);
232+
} else {
233+
for (String locale : springDocConfigProperties.getPreLoadingLocales()) {
234+
Executors.newSingleThreadExecutor().execute(() -> this.getOpenApi(Locale.forLanguageTag(locale)));
235+
}
236+
}
237+
}
231238
}
232239

233240
/**

springdoc-openapi-starter-common/src/main/java/org/springdoc/core/properties/SpringDocConfigProperties.java

+21-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* *
44
* * *
55
* * * *
6-
* * * * * Copyright 2019-2022 the original author or authors.
6+
* * * * * Copyright 2019-2023 the original author or authors.
77
* * * * *
88
* * * * * Licensed under the Apache License, Version 2.0 (the "License");
99
* * * * * you may not use this file except in compliance with the License.
@@ -167,6 +167,11 @@ public class SpringDocConfigProperties {
167167
*/
168168
private boolean preLoadingEnabled;
169169

170+
/**
171+
* locale list to pre-loading
172+
*/
173+
private List<String> preLoadingLocales;
174+
170175
/**
171176
* If set to true, exposes the swagger-ui on the actuator management port.
172177
*/
@@ -949,14 +954,27 @@ public boolean isPreLoadingEnabled() {
949954
}
950955

951956
/**
952-
* Sets pre loading enabled.
957+
* locale list to pre-loading.
958+
*
959+
* @return the Locales
960+
*/
961+
public List<String> getPreLoadingLocales() {
962+
return preLoadingLocales;
963+
}
964+
965+
/**
966+
* Sets locale list to pre-loading.
953967
*
954-
* @param preLoadingEnabled the pre loading enabled
968+
* @param preLoadingEnabled the Locales
955969
*/
956970
public void setPreLoadingEnabled(boolean preLoadingEnabled) {
957971
this.preLoadingEnabled = preLoadingEnabled;
958972
}
959973

974+
public void setPreLoadingLocales(List<String> preLoadingLocales) {
975+
this.preLoadingLocales = preLoadingLocales;
976+
}
977+
960978
/**
961979
* The type Model converters.
962980
*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
*
3+
* *
4+
* * *
5+
* * * *
6+
* * * * * Copyright 2019-2023 the original author or authors.
7+
* * * * *
8+
* * * * * Licensed under the Apache License, Version 2.0 (the "License");
9+
* * * * * you may not use this file except in compliance with the License.
10+
* * * * * You may obtain a copy of the License at
11+
* * * * *
12+
* * * * * https://www.apache.org/licenses/LICENSE-2.0
13+
* * * * *
14+
* * * * * Unless required by applicable law or agreed to in writing, software
15+
* * * * * distributed under the License is distributed on an "AS IS" BASIS,
16+
* * * * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* * * * * See the License for the specific language governing permissions and
18+
* * * * * limitations under the License.
19+
* * * *
20+
* * *
21+
* *
22+
*
23+
*/
24+
25+
package test.org.springdoc.api.v30.app209;
26+
27+
import io.swagger.v3.oas.annotations.Parameter;
28+
import jakarta.validation.constraints.NegativeOrZero;
29+
import jakarta.validation.constraints.NotBlank;
30+
import jakarta.validation.constraints.NotEmpty;
31+
import jakarta.validation.constraints.PositiveOrZero;
32+
33+
import org.springframework.web.bind.annotation.GetMapping;
34+
import org.springframework.web.bind.annotation.RequestParam;
35+
import org.springframework.web.bind.annotation.RestController;
36+
37+
@RestController
38+
public class HelloController {
39+
40+
@GetMapping(value = "/persons")
41+
public String persons(@NotBlank String name) {
42+
return "OK";
43+
}
44+
45+
@GetMapping(value = "/persons2")
46+
public String persons2(@NotBlank @Parameter(description = "persons name") String name) {
47+
return "OK";
48+
}
49+
50+
@GetMapping(value = "/persons3")
51+
public String persons3(@NotBlank @Parameter(description = "persons name") @RequestParam String name) {
52+
return "OK";
53+
}
54+
55+
@GetMapping(value = "/persons4")
56+
public String persons4(@PositiveOrZero int age) {
57+
return "OK";
58+
}
59+
60+
@GetMapping(value = "/persons5")
61+
public String persons5(@NegativeOrZero int age) {
62+
return "OK";
63+
}
64+
65+
@GetMapping(value = "/persons6")
66+
public String persons6(@NotEmpty @Parameter(description = "persons name") String name) {
67+
return "OK";
68+
}
69+
70+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/*
2+
*
3+
* *
4+
* * *
5+
* * * *
6+
* * * * * Copyright 2019-2023 the original author or authors.
7+
* * * * *
8+
* * * * * Licensed under the Apache License, Version 2.0 (the "License");
9+
* * * * * you may not use this file except in compliance with the License.
10+
* * * * * You may obtain a copy of the License at
11+
* * * * *
12+
* * * * * https://www.apache.org/licenses/LICENSE-2.0
13+
* * * * *
14+
* * * * * Unless required by applicable law or agreed to in writing, software
15+
* * * * * distributed under the License is distributed on an "AS IS" BASIS,
16+
* * * * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* * * * * See the License for the specific language governing permissions and
18+
* * * * * limitations under the License.
19+
* * * *
20+
* * *
21+
* *
22+
*
23+
*/
24+
25+
package test.org.springdoc.api.v30.app209;
26+
27+
import java.util.List;
28+
import java.util.Locale;
29+
import java.util.Optional;
30+
31+
import io.swagger.v3.oas.models.OpenAPI;
32+
import org.junit.jupiter.api.Test;
33+
import org.skyscreamer.jsonassert.JSONAssert;
34+
import org.springdoc.core.customizers.OpenApiBuilderCustomizer;
35+
import org.springdoc.core.customizers.ServerBaseUrlCustomizer;
36+
import org.springdoc.core.properties.SpringDocConfigProperties;
37+
import org.springdoc.core.providers.JavadocProvider;
38+
import org.springdoc.core.service.OpenAPIService;
39+
import org.springdoc.core.service.SecurityService;
40+
import org.springdoc.core.utils.Constants;
41+
import org.springdoc.core.utils.PropertyResolverUtils;
42+
import test.org.springdoc.api.AbstractCommonTest;
43+
44+
import org.springframework.beans.factory.annotation.Autowired;
45+
import org.springframework.boot.autoconfigure.SpringBootApplication;
46+
import org.springframework.boot.test.context.SpringBootTest;
47+
import org.springframework.context.annotation.Bean;
48+
import org.springframework.http.HttpHeaders;
49+
import org.springframework.test.web.servlet.MvcResult;
50+
51+
import static org.hamcrest.Matchers.is;
52+
import static org.junit.jupiter.api.Assertions.assertEquals;
53+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
54+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
55+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
56+
57+
@SpringBootTest(properties = {
58+
"springdoc.pre-loading-enabled=true",
59+
"springdoc.pre-loading-locales=ja"
60+
})
61+
public class SpringDocApp209Test extends AbstractCommonTest {
62+
public static String className;
63+
64+
static {
65+
System.setProperty("user.country", "JP");
66+
System.setProperty("user.language", "ja");
67+
}
68+
69+
@Autowired
70+
private OpenAPIServiceMock openAPIService;
71+
72+
@SpringBootApplication
73+
static class SpringDocTestApp {
74+
@Bean("openAPIService")
75+
public OpenAPIServiceMock openAPIService(Optional<OpenAPI> openAPI, SecurityService securityParser, SpringDocConfigProperties springDocConfigProperties, PropertyResolverUtils propertyResolverUtils, Optional<List<OpenApiBuilderCustomizer>> openApiBuilderCustomizers, Optional<List<ServerBaseUrlCustomizer>> serverBaseUrlCustomizers, Optional<JavadocProvider> javadocProvider) {
76+
return new OpenAPIServiceMock(openAPI, securityParser, springDocConfigProperties, propertyResolverUtils, openApiBuilderCustomizers, serverBaseUrlCustomizers, javadocProvider);
77+
}
78+
}
79+
80+
public static class OpenAPIServiceMock extends OpenAPIService {
81+
private int numberOfTimesCalculatePathWasCalled;
82+
83+
public OpenAPIServiceMock(Optional<OpenAPI> openAPI, SecurityService securityParser, SpringDocConfigProperties springDocConfigProperties, PropertyResolverUtils propertyResolverUtils, Optional<List<OpenApiBuilderCustomizer>> openApiBuilderCustomizers, Optional<List<ServerBaseUrlCustomizer>> serverBaseUrlCustomizers, Optional<JavadocProvider> javadocProvider) {
84+
super(openAPI, securityParser, springDocConfigProperties, propertyResolverUtils, openApiBuilderCustomizers, serverBaseUrlCustomizers, javadocProvider);
85+
}
86+
87+
@Override
88+
public void setCachedOpenAPI(OpenAPI cachedOpenAPI, Locale locale) {
89+
numberOfTimesCalculatePathWasCalled++;
90+
super.setCachedOpenAPI(cachedOpenAPI, locale);
91+
}
92+
93+
public int getNumberOfTimesCalculatePathWasCalled() {
94+
return numberOfTimesCalculatePathWasCalled;
95+
}
96+
}
97+
98+
@Test
99+
public void shouldOnlyByCalledOnce() throws Exception {
100+
assertEquals(1, openAPIService.getNumberOfTimesCalculatePathWasCalled());
101+
102+
className = getClass().getSimpleName();
103+
String testNumber = className.replaceAll("[^0-9]", "");
104+
MvcResult mockMvcResult = mockMvc
105+
.perform(get(Constants.DEFAULT_API_DOCS_URL).header(HttpHeaders.ACCEPT_LANGUAGE, "ja"))
106+
.andExpect(status().isOk())
107+
.andExpect(jsonPath("$.openapi", is("3.0.1"))).andReturn();
108+
String result = mockMvcResult.getResponse().getContentAsString();
109+
String expected = getContent("results/3.0.1/app" + testNumber + ".json");
110+
JSONAssert.assertEquals(expected, result, true);
111+
112+
assertEquals(1, openAPIService.getNumberOfTimesCalculatePathWasCalled());
113+
}
114+
}

0 commit comments

Comments
 (0)