From 9320d747fecda9caf5dc467d41cd08e9c7e2116d Mon Sep 17 00:00:00 2001 From: Ben Einaudi Date: Thu, 21 Jul 2022 16:16:07 +0200 Subject: [PATCH] Update server out of cache Update servers out of cache and do not set server as provided when they are modified by customizer. This way we can still have a generic customized server address Close #1748 --- .../api/AbstractOpenApiResource.java | 28 +++++-------------- .../org/springdoc/core/OpenAPIService.java | 4 +-- .../api/AbstractOpenApiResourceTest.java | 6 ++-- .../test/resources/results/3.0.1/app138.json | 2 +- 4 files changed, 12 insertions(+), 28 deletions(-) diff --git a/springdoc-openapi-common/src/main/java/org/springdoc/api/AbstractOpenApiResource.java b/springdoc-openapi-common/src/main/java/org/springdoc/api/AbstractOpenApiResource.java index b73fcb602..cd2186277 100644 --- a/springdoc-openapi-common/src/main/java/org/springdoc/api/AbstractOpenApiResource.java +++ b/springdoc-openapi-common/src/main/java/org/springdoc/api/AbstractOpenApiResource.java @@ -169,7 +169,7 @@ public abstract class AbstractOpenApiResource extends SpecFilter { private final OperationService operationParser; /** - * The Open api customisers. + * The Open api customizers. */ private final Optional> openApiCustomisers; @@ -299,7 +299,7 @@ public static void addHiddenRestControllers(String... classes) { * @return the open api */ protected synchronized OpenAPI getOpenApi(Locale locale) { - OpenAPI openAPI; + final OpenAPI openAPI; final Locale finalLocale = locale == null ? Locale.getDefault() : locale; if (openAPIService.getCachedOpenAPI(finalLocale) == null || springDocConfigProperties.isCacheDisabled()) { Instant start = Instant.now(); @@ -327,29 +327,12 @@ protected synchronized OpenAPI getOpenApi(Locale locale) { this.calculatePath(routerOperationList, locale, openAPI); } ); - if (!CollectionUtils.isEmpty(openAPI.getServers())) openAPIService.setServersPresent(true); - openAPIService.updateServers(openAPI); if (springDocConfigProperties.isRemoveBrokenReferenceDefinitions()) this.removeBrokenReferenceDefinitions(openAPI); - // run the optional customisers - List servers = openAPI.getServers(); - List serversCopy = null; - try { - serversCopy = springDocProviders.jsonMapper() - .readValue(springDocProviders.jsonMapper().writeValueAsString(servers), new TypeReference>() {}); - } - catch (JsonProcessingException e) { - LOGGER.warn("Json Processing Exception occurred: {}", e.getMessage()); - } - - openApiLocaleCustomizers.values().forEach(openApiLocaleCustomizer -> openApiLocaleCustomizer.customise(openAPI, finalLocale)); - openApiCustomisers.ifPresent(apiCustomisers -> apiCustomisers.forEach(openApiCustomiser -> openApiCustomiser.customise(openAPI))); - if (!CollectionUtils.isEmpty(openAPI.getServers()) && !openAPI.getServers().equals(serversCopy)) - openAPIService.setServersPresent(true); openAPIService.setCachedOpenAPI(openAPI, finalLocale); @@ -358,8 +341,11 @@ protected synchronized OpenAPI getOpenApi(Locale locale) { } else { LOGGER.debug("Fetching openApi document from cache"); - openAPI = openAPIService.updateServers(openAPIService.getCachedOpenAPI(finalLocale)); + openAPI = openAPIService.getCachedOpenAPI(finalLocale); } + openAPIService.updateServers(openAPI); + openApiLocaleCustomizers.values().forEach(openApiLocaleCustomizer -> openApiLocaleCustomizer.customise(openAPI, finalLocale)); + openApiCustomisers.ifPresent(apiCustomizers -> apiCustomizers.forEach(openApiCustomizer -> openApiCustomizer.customise(openAPI))); return openAPI; } @@ -1327,4 +1313,4 @@ enum ConditionType { public static void setModelAndViewClass(Class modelAndViewClass) { AbstractOpenApiResource.modelAndViewClass = modelAndViewClass; } -} +} \ No newline at end of file diff --git a/springdoc-openapi-common/src/main/java/org/springdoc/core/OpenAPIService.java b/springdoc-openapi-common/src/main/java/org/springdoc/core/OpenAPIService.java index 30c86ee22..d1a9a9c57 100644 --- a/springdoc-openapi-common/src/main/java/org/springdoc/core/OpenAPIService.java +++ b/springdoc-openapi-common/src/main/java/org/springdoc/core/OpenAPIService.java @@ -296,9 +296,8 @@ private void initializeHiddenRestController() { * Update servers open api. * * @param openAPI the open api - * @return the open api */ - public OpenAPI updateServers(OpenAPI openAPI) { + public void updateServers(OpenAPI openAPI) { if (!isServersPresent && serverBaseUrl != null) // default server value { Server server = new Server().url(serverBaseUrl).description(DEFAULT_SERVER_DESCRIPTION); @@ -306,7 +305,6 @@ public OpenAPI updateServers(OpenAPI openAPI) { servers.add(server); openAPI.setServers(servers); } - return openAPI; } /** diff --git a/springdoc-openapi-common/src/test/java/org/springdoc/api/AbstractOpenApiResourceTest.java b/springdoc-openapi-common/src/test/java/org/springdoc/api/AbstractOpenApiResourceTest.java index f2059ab07..5c231e93e 100644 --- a/springdoc-openapi-common/src/test/java/org/springdoc/api/AbstractOpenApiResourceTest.java +++ b/springdoc-openapi-common/src/test/java/org/springdoc/api/AbstractOpenApiResourceTest.java @@ -70,6 +70,7 @@ import static org.hamcrest.Matchers.nullValue; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.doAnswer; +import static org.mockito.Mockito.doCallRealMethod; import static org.mockito.Mockito.when; import static org.springframework.web.bind.annotation.RequestMethod.GET; @@ -122,7 +123,6 @@ public void setUp() { when(openAPIService.build(any())).thenReturn(openAPI); when(openAPIBuilderObjectFactory.getObject()).thenReturn(openAPIService); - when(springDocProviders.jsonMapper()).thenReturn(Json.mapper()); } @Test @@ -186,7 +186,7 @@ void calculatePathFromRouterOperation() { @Test void preLoadingModeShouldNotOverwriteServers() throws InterruptedException { - when(openAPIService.updateServers(any())).thenCallRealMethod(); + doCallRealMethod().when(openAPIService).updateServers(any()); when(openAPIService.getCachedOpenAPI(any())).thenCallRealMethod(); doAnswer(new CallsRealMethods()).when(openAPIService).setServersPresent(true); doAnswer(new CallsRealMethods()).when(openAPIService).setServerBaseUrl(any()); @@ -224,7 +224,7 @@ void preLoadingModeShouldNotOverwriteServers() throws InterruptedException { @Test void serverBaseUrlCustomisersTest() throws InterruptedException { - when(openAPIService.updateServers(any())).thenCallRealMethod(); + doCallRealMethod().when(openAPIService).updateServers(any()); when(openAPIService.getCachedOpenAPI(any())).thenCallRealMethod(); doAnswer(new CallsRealMethods()).when(openAPIService).setServerBaseUrl(any()); doAnswer(new CallsRealMethods()).when(openAPIService).setCachedOpenAPI(any(), any()); diff --git a/springdoc-openapi-webmvc-core/src/test/resources/results/3.0.1/app138.json b/springdoc-openapi-webmvc-core/src/test/resources/results/3.0.1/app138.json index b5c657b22..7b757b5eb 100644 --- a/springdoc-openapi-webmvc-core/src/test/resources/results/3.0.1/app138.json +++ b/springdoc-openapi-webmvc-core/src/test/resources/results/3.0.1/app138.json @@ -1 +1 @@ -{"openapi":"3.0.1","info":{"title":"OpenAPI definition","version":"v0"},"servers":[{"url":"http://localhost","description":"Generated server url"}],"paths":{"/testA":{"get":{"operationId":"testA","parameters":[{"in":"query","name":"hello","required":true,"schema":{"type":"string"}}],"responses":{"200":{"description":"OK"}},"tags":["hello-controller"]}},"/testB":{"get":{"operationId":"testB","parameters":[{"in":"query","name":"hello","required":true,"schema":{"type":"string"}}],"responses":{"200":{"description":"OK"}},"tags":["hello-controller"]}}},"components":{},"x-my-vendor-extensions":{"property1":"value1","property2":null}} \ No newline at end of file +{"openapi":"3.0.1","info":{"title":"OpenAPI definition","version":"v0"},"servers":[{"description":"Generated server url","url":"http://localhost"}],"paths":{"/testA":{"get":{"operationId":"testA","parameters":[{"in":"query","name":"hello","required":true,"schema":{"type":"string"}}],"responses":{"200":{"description":"OK"}},"tags":["hello-controller"]}},"/testB":{"get":{"operationId":"testB","parameters":[{"in":"query","name":"hello","required":true,"schema":{"type":"string"}}],"responses":{"200":{"description":"OK"}},"tags":["hello-controller"]}}},"components":{},"x-my-vendor-extensions":{"property1":"value1","property2":null}} \ No newline at end of file