From 4debf734b4f192b247a3103fc756c9840038956d 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 | 26 +++++-------------- .../org/springdoc/core/OpenAPIService.java | 6 ++--- .../api/AbstractOpenApiResourceTest.java | 8 +++--- .../src/test/resources/logback-test.xml | 4 +-- 4 files changed, 14 insertions(+), 30 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..1e1f9233b 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; } 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..856c57fa1 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; } /** @@ -753,4 +751,4 @@ public String getServerBaseUrl() { public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { this.context = applicationContext; } -} \ No newline at end of file +} 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..16f78df93 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()); @@ -298,4 +298,4 @@ private static class EmptyPathsOpenApiResource extends AbstractOpenApiResource { public void getPaths(Map findRestControllers, Locale locale, OpenAPI openAPI) { } } -} \ No newline at end of file +} diff --git a/springdoc-openapi-webflux-core/src/test/resources/logback-test.xml b/springdoc-openapi-webflux-core/src/test/resources/logback-test.xml index 753193aa4..a410b69ea 100644 --- a/springdoc-openapi-webflux-core/src/test/resources/logback-test.xml +++ b/springdoc-openapi-webflux-core/src/test/resources/logback-test.xml @@ -1,6 +1,6 @@ - + - \ No newline at end of file +