Skip to content

Commit 41837d4

Browse files
committed
Add better support for locale when using "new OpenAPI()". Fixes #1360
1 parent 0fb9ee4 commit 41837d4

File tree

6 files changed

+74
-10
lines changed

6 files changed

+74
-10
lines changed

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

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
import org.springdoc.core.SpringDocConfigProperties.GroupConfig;
8888
import org.springdoc.core.annotations.RouterOperations;
8989
import org.springdoc.core.customizers.OpenApiCustomiser;
90+
import org.springdoc.core.customizers.OpenApiLocaleCustomizer;
9091
import org.springdoc.core.customizers.OperationCustomizer;
9192
import org.springdoc.core.fn.AbstractRouterFunctionVisitor;
9293
import org.springdoc.core.fn.RouterFunctionData;
@@ -196,6 +197,11 @@ public abstract class AbstractOpenApiResource extends SpecFilter {
196197
*/
197198
private static Class<?> modelAndViewClass;
198199

200+
/**
201+
* The OpenApi with locale customizers.
202+
*/
203+
private final Map<String, OpenApiLocaleCustomizer> openApiLocaleCustomizers;
204+
199205
static {
200206
try {
201207
modelAndViewClass = Class.forName("org.springframework.web.servlet.ModelAndView");
@@ -243,6 +249,7 @@ protected AbstractOpenApiResource(String groupName, ObjectFactory<OpenAPIService
243249
this.optionalActuatorProvider = actuatorProvider;
244250
if (springDocConfigProperties.isPreLoadingEnabled())
245251
Executors.newSingleThreadExecutor().execute(this::getOpenApi);
252+
this.openApiLocaleCustomizers = openAPIService.getContext().getBeansOfType(OpenApiLocaleCustomizer.class);
246253
}
247254

248255
/**
@@ -295,10 +302,10 @@ public static void addHiddenRestControllers(String... classes) {
295302
*/
296303
protected synchronized OpenAPI getOpenApi(Locale locale) {
297304
OpenAPI openApi;
298-
locale = locale == null ? Locale.getDefault() : locale;
299-
if (openAPIService.getCachedOpenAPI(locale) == null || springDocConfigProperties.isCacheDisabled()) {
305+
final Locale finalLocale = locale == null ? Locale.getDefault() : locale;
306+
if (openAPIService.getCachedOpenAPI(finalLocale) == null || springDocConfigProperties.isCacheDisabled()) {
300307
Instant start = Instant.now();
301-
openAPIService.build(locale);
308+
openAPIService.build(finalLocale);
302309
Map<String, Object> mappingsMap = openAPIService.getMappingsMap().entrySet().stream()
303310
.filter(controller -> (AnnotationUtils.findAnnotation(controller.getValue().getClass(),
304311
Hidden.class) == null))
@@ -311,9 +318,9 @@ protected synchronized OpenAPI getOpenApi(Locale locale) {
311318
if (springDocConfigProperties.isOverrideWithGenericResponse() && !CollectionUtils.isEmpty(findControllerAdvice)) {
312319
if (!CollectionUtils.isEmpty(mappingsMap))
313320
findControllerAdvice.putAll(mappingsMap);
314-
responseBuilder.buildGenericResponse(openApi.getComponents(), findControllerAdvice, locale);
321+
responseBuilder.buildGenericResponse(openApi.getComponents(), findControllerAdvice, finalLocale);
315322
}
316-
getPaths(mappingsMap, locale);
323+
getPaths(mappingsMap, finalLocale);
317324
if (!CollectionUtils.isEmpty(openApi.getServers()))
318325
openAPIService.setServersPresent(true);
319326
openAPIService.updateServers(openApi);
@@ -332,19 +339,20 @@ protected synchronized OpenAPI getOpenApi(Locale locale) {
332339
LOGGER.warn("Json Processing Exception occurred: {}", e.getMessage());
333340
}
334341

342+
openApiLocaleCustomizers.values().forEach(openApiLocaleCustomizer -> openApiLocaleCustomizer.customise(openApi, finalLocale));
335343
openApiCustomisers.ifPresent(apiCustomisers -> apiCustomisers.forEach(openApiCustomiser -> openApiCustomiser.customise(openApi)));
336344
if (!CollectionUtils.isEmpty(openApi.getServers()) && !openApi.getServers().equals(serversCopy))
337345
openAPIService.setServersPresent(true);
338346

339-
openAPIService.setCachedOpenAPI(openApi, locale);
347+
openAPIService.setCachedOpenAPI(openApi, finalLocale);
340348
openAPIService.resetCalculatedOpenAPI();
341349

342350
LOGGER.info("Init duration for springdoc-openapi is: {} ms",
343351
Duration.between(start, Instant.now()).toMillis());
344352
}
345353
else {
346354
LOGGER.debug("Fetching openApi document from cache");
347-
openApi = openAPIService.updateServers(openAPIService.getCachedOpenAPI(locale));
355+
openApi = openAPIService.updateServers(openAPIService.getCachedOpenAPI(finalLocale));
348356
}
349357
return openApi;
350358
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
*
3+
* *
4+
* * * Copyright 2019-2020 the original author or authors.
5+
* * *
6+
* * * Licensed under the Apache License, Version 2.0 (the "License");
7+
* * * you may not use this file except in compliance with the License.
8+
* * * You may obtain a copy of the License at
9+
* * *
10+
* * * https://www.apache.org/licenses/LICENSE-2.0
11+
* * *
12+
* * * Unless required by applicable law or agreed to in writing, software
13+
* * * distributed under the License is distributed on an "AS IS" BASIS,
14+
* * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* * * See the License for the specific language governing permissions and
16+
* * * limitations under the License.
17+
* *
18+
*
19+
*/
20+
21+
package org.springdoc.core.customizers;
22+
23+
import java.util.Locale;
24+
25+
import io.swagger.v3.oas.models.OpenAPI;
26+
27+
/**
28+
* The interface Open api locale customizer.
29+
* @author bnasslahsen
30+
*/
31+
@FunctionalInterface
32+
public interface OpenApiLocaleCustomizer {
33+
34+
/**
35+
* Customise.
36+
*
37+
* @param openApi the open api
38+
* @param locale the locale
39+
*/
40+
void customise(OpenAPI openApi, Locale locale);
41+
42+
}

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,13 @@
2222

2323
import org.junit.jupiter.api.Test;
2424
import org.springdoc.core.Constants;
25+
import org.springdoc.core.customizers.OpenApiLocaleCustomizer;
2526
import test.org.springdoc.api.AbstractSpringDocTest;
2627

28+
import org.springframework.beans.factory.annotation.Autowired;
2729
import org.springframework.boot.autoconfigure.SpringBootApplication;
30+
import org.springframework.context.annotation.Bean;
31+
import org.springframework.context.support.ResourceBundleMessageSource;
2832
import org.springframework.http.HttpHeaders;
2933
import org.springframework.test.context.TestPropertySource;
3034
import org.springframework.test.web.servlet.MvcResult;
@@ -40,6 +44,16 @@ public class SpringDocApp171Test extends AbstractSpringDocTest {
4044

4145
@SpringBootApplication
4246
static class SpringDocTestApp {
47+
48+
@Autowired
49+
ResourceBundleMessageSource resourceBundleMessageSource;
50+
51+
@Bean
52+
public OpenApiLocaleCustomizer openApiLocaleCustomizer() {
53+
return (openAPI, locale)
54+
-> openAPI.getInfo().title(resourceBundleMessageSource.getMessage("test", null, locale));
55+
}
56+
4357
}
4458

4559
@Test

springdoc-openapi-webmvc-core/src/test/resources/results/app171-en-GB.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"openapi": "3.0.1",
33
"info": {
4-
"title": "OpenAPI definition",
4+
"title": "This is a test message[EN_GB]",
55
"version": "v0"
66
},
77
"servers": [

springdoc-openapi-webmvc-core/src/test/resources/results/app171-en-US.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"openapi": "3.0.1",
33
"info": {
4-
"title": "OpenAPI definition",
4+
"title": "This is a test message",
55
"version": "v0"
66
},
77
"servers": [

springdoc-openapi-webmvc-core/src/test/resources/results/app171-fr-FR.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"openapi": "3.0.1",
33
"info": {
4-
"title": "OpenAPI definition",
4+
"title": "This is a test message[FR]",
55
"version": "v0"
66
},
77
"servers": [

0 commit comments

Comments
 (0)