Skip to content

Commit e92172f

Browse files
committed
Add support for caching based on Locale
1 parent d4e3b68 commit e92172f

File tree

6 files changed

+34
-24
lines changed

6 files changed

+34
-24
lines changed

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ public static void addHiddenRestControllers(String... classes) {
294294
*/
295295
protected synchronized OpenAPI getOpenApi(Locale locale) {
296296
OpenAPI openApi;
297-
if (openAPIService.getCachedOpenAPI() == null || springDocConfigProperties.isCacheDisabled()) {
297+
if (openAPIService.getCachedOpenAPI(locale) == null || springDocConfigProperties.isCacheDisabled()) {
298298
Instant start = Instant.now();
299299
openAPIService.build(locale);
300300
Map<String, Object> mappingsMap = openAPIService.getMappingsMap().entrySet().stream()
@@ -325,15 +325,15 @@ protected synchronized OpenAPI getOpenApi(Locale locale) {
325325
if ((!CollectionUtils.isEmpty(openApi.getServers()) && !Objects.equals(servers, openApi.getServers())))
326326
openAPIService.setServersPresent(true);
327327

328-
openAPIService.setCachedOpenAPI(openApi);
328+
openAPIService.setCachedOpenAPI(openApi, locale);
329329
openAPIService.resetCalculatedOpenAPI();
330330

331331
LOGGER.info("Init duration for springdoc-openapi is: {} ms",
332332
Duration.between(start, Instant.now()).toMillis());
333333
}
334334
else {
335335
LOGGER.debug("Fetching openApi document from cache");
336-
openApi = openAPIService.updateServers(openAPIService.getCachedOpenAPI());
336+
openApi = openAPIService.updateServers(openAPIService.getCachedOpenAPI(locale));
337337
}
338338
return openApi;
339339
}
@@ -1099,8 +1099,8 @@ else if (existingOperation != null) {
10991099
/**
11001100
* Init open api builder.
11011101
*/
1102-
protected void initOpenAPIBuilder() {
1103-
if (openAPIService.getCachedOpenAPI() != null && springDocConfigProperties.isCacheDisabled()) {
1102+
protected void initOpenAPIBuilder(Locale locale) {
1103+
if (openAPIService.getCachedOpenAPI(locale) != null && springDocConfigProperties.isCacheDisabled()) {
11041104
openAPIService = openAPIBuilderObjectFactory.getObject();
11051105
}
11061106
}

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

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,10 @@ public class OpenAPIService {
129129
/**
130130
* The Cached open api.
131131
*/
132-
private OpenAPI cachedOpenAPI;
132+
/**
133+
* The Mappings map.
134+
*/
135+
private final Map<String, OpenAPI> cachedOpenAPI = new HashMap<>();
133136

134137
/**
135138
* The Calculated open api.
@@ -722,19 +725,26 @@ public Map<String, Object> getControllerAdviceMap() {
722725
/**
723726
* Gets cached open api.
724727
*
728+
* @param locale associated the the cache entry
725729
* @return the cached open api
726730
*/
727-
public OpenAPI getCachedOpenAPI() {
728-
return cachedOpenAPI;
731+
public OpenAPI getCachedOpenAPI(Locale locale) {
732+
if (cachedOpenAPI != null && cachedOpenAPI.containsKey(locale.getLanguage())) {
733+
return cachedOpenAPI.get(locale.getLanguage());
734+
}
735+
return null;
729736
}
730737

731738
/**
732739
* Sets cached open api.
733740
*
741+
* @param locale associated the the cache entry
734742
* @param cachedOpenAPI the cached open api
735743
*/
736-
public void setCachedOpenAPI(OpenAPI cachedOpenAPI) {
737-
this.cachedOpenAPI = cachedOpenAPI;
744+
public void setCachedOpenAPI(OpenAPI cachedOpenAPI, Locale locale) {
745+
if (this.cachedOpenAPI != null) {
746+
this.cachedOpenAPI.put(locale.getLanguage(), cachedOpenAPI);
747+
}
738748
}
739749

740750
/**

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,10 +168,10 @@ void calculatePathFromRouterOperation() {
168168
@Test
169169
void preLoadingModeShouldNotOverwriteServers() throws InterruptedException {
170170
when(openAPIService.updateServers(any())).thenCallRealMethod();
171-
when(openAPIService.getCachedOpenAPI()).thenCallRealMethod();
171+
when(openAPIService.getCachedOpenAPI(any())).thenCallRealMethod();
172172
doAnswer(new CallsRealMethods()).when(openAPIService).setServersPresent(true);
173173
doAnswer(new CallsRealMethods()).when(openAPIService).setServerBaseUrl(any());
174-
doAnswer(new CallsRealMethods()).when(openAPIService).setCachedOpenAPI(any());
174+
doAnswer(new CallsRealMethods()).when(openAPIService).setCachedOpenAPI(any(), any());
175175

176176
String customUrl = "https://custom.com";
177177
String generatedUrl = "https://generated.com";
@@ -197,8 +197,8 @@ void preLoadingModeShouldNotOverwriteServers() throws InterruptedException {
197197
// emulate generating base url
198198
openAPIService.setServerBaseUrl(generatedUrl);
199199
openAPIService.updateServers(openAPI);
200-
201-
OpenAPI after = resource.getOpenApi(Locale.getDefault());
200+
Locale locale = Locale.US;
201+
OpenAPI after = resource.getOpenApi(locale);
202202

203203
assertThat(after.getServers().get(0).getUrl(), is(customUrl));
204204
}

springdoc-openapi-webflux-core/src/main/java/org/springdoc/webflux/api/OpenApiActuatorResource.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,8 @@ public Mono<String> openapiYaml(ServerHttpRequest serverHttpRequest, Locale loca
122122
}
123123

124124
@Override
125-
protected void calculateServerUrl(ServerHttpRequest serverHttpRequest, String apiDocsUrl) {
126-
super.initOpenAPIBuilder();
125+
protected void calculateServerUrl(ServerHttpRequest serverHttpRequest, String apiDocsUrl, Locale locale) {
126+
super.initOpenAPIBuilder(locale);
127127
URI uri = getActuatorURI(serverHttpRequest.getURI().getScheme(), serverHttpRequest.getURI().getHost());
128128
openAPIService.setServerBaseUrl(uri.toString());
129129
}

springdoc-openapi-webflux-core/src/main/java/org/springdoc/webflux/api/OpenApiResource.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public OpenApiResource(ObjectFactory<OpenAPIService> openAPIBuilderObjectFactory
122122
*/
123123
protected Mono<String> openapiJson(ServerHttpRequest serverHttpRequest, String apiDocsUrl, Locale locale)
124124
throws JsonProcessingException {
125-
calculateServerUrl(serverHttpRequest, apiDocsUrl);
125+
calculateServerUrl(serverHttpRequest, apiDocsUrl, locale);
126126
OpenAPI openAPI = this.getOpenApi(locale);
127127
return Mono.just(writeJsonValue(openAPI));
128128
}
@@ -138,7 +138,7 @@ protected Mono<String> openapiJson(ServerHttpRequest serverHttpRequest, String a
138138
*/
139139
protected Mono<String> openapiYaml(ServerHttpRequest serverHttpRequest, String apiDocsUrl, Locale locale)
140140
throws JsonProcessingException {
141-
calculateServerUrl(serverHttpRequest, apiDocsUrl);
141+
calculateServerUrl(serverHttpRequest, apiDocsUrl, locale);
142142
OpenAPI openAPI = this.getOpenApi(locale);
143143
return Mono.just(writeYamlValue(openAPI));
144144
}
@@ -232,8 +232,8 @@ protected void getWebFluxRouterFunctionPaths(Locale locale) {
232232
* @param serverHttpRequest the server http request
233233
* @param apiDocsUrl the api docs url
234234
*/
235-
protected void calculateServerUrl(ServerHttpRequest serverHttpRequest, String apiDocsUrl) {
236-
initOpenAPIBuilder();
235+
protected void calculateServerUrl(ServerHttpRequest serverHttpRequest, String apiDocsUrl, Locale locale) {
236+
initOpenAPIBuilder(locale);
237237
String serverUrl = getServerUrl(serverHttpRequest,apiDocsUrl);
238238
openAPIService.setServerBaseUrl(serverUrl);
239239
}

springdoc-openapi-webmvc-core/src/main/java/org/springdoc/webmvc/api/OpenApiResource.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ public OpenApiResource(ObjectFactory<OpenAPIService> openAPIBuilderObjectFactory
157157
public String openapiJson(HttpServletRequest request,
158158
String apiDocsUrl, Locale locale)
159159
throws JsonProcessingException {
160-
calculateServerUrl(request, apiDocsUrl);
160+
calculateServerUrl(request, apiDocsUrl, locale);
161161
OpenAPI openAPI = this.getOpenApi(locale);
162162
return writeJsonValue(openAPI);
163163
}
@@ -174,7 +174,7 @@ public String openapiJson(HttpServletRequest request,
174174
public String openapiYaml(HttpServletRequest request,
175175
String apiDocsUrl, Locale locale)
176176
throws JsonProcessingException {
177-
calculateServerUrl(request, apiDocsUrl);
177+
calculateServerUrl(request, apiDocsUrl, locale);
178178
OpenAPI openAPI = this.getOpenApi(locale);
179179
return writeYamlValue(openAPI);
180180
}
@@ -290,8 +290,8 @@ private Comparator<Map.Entry<RequestMappingInfo, HandlerMethod>> byReversedReque
290290
* @param request the request
291291
* @param apiDocsUrl the api docs url
292292
*/
293-
protected void calculateServerUrl(HttpServletRequest request, String apiDocsUrl) {
294-
super.initOpenAPIBuilder();
293+
protected void calculateServerUrl(HttpServletRequest request, String apiDocsUrl, Locale locale) {
294+
super.initOpenAPIBuilder(locale);
295295
String calculatedUrl = getServerUrl(request, apiDocsUrl);
296296
openAPIService.setServerBaseUrl(calculatedUrl);
297297
}

0 commit comments

Comments
 (0)