Skip to content

Commit

Permalink
Update server out of cache
Browse files Browse the repository at this point in the history
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 springdoc#1748
  • Loading branch information
Ben Einaudi committed Jul 24, 2022
1 parent 5bd85e1 commit 8dce7ff
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 29 deletions.
Expand Up @@ -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<List<OpenApiCustomiser>> openApiCustomisers;

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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<Server> servers = openAPI.getServers();
List<Server> serversCopy = null;
try {
serversCopy = springDocProviders.jsonMapper()
.readValue(springDocProviders.jsonMapper().writeValueAsString(servers), new TypeReference<List<Server>>() {});
}
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);

Expand All @@ -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;
}

Expand Down
Expand Up @@ -296,17 +296,15 @@ 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);
List<Server> servers = new ArrayList<>();
servers.add(server);
openAPI.setServers(servers);
}
return openAPI;
}

/**
Expand Down Expand Up @@ -753,4 +751,4 @@ public String getServerBaseUrl() {
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.context = applicationContext;
}
}
}
Expand Up @@ -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;

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -298,4 +298,4 @@ private static class EmptyPathsOpenApiResource extends AbstractOpenApiResource {
public void getPaths(Map<String, Object> findRestControllers, Locale locale, OpenAPI openAPI) {
}
}
}
}
@@ -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}}
{"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}}

0 comments on commit 8dce7ff

Please sign in to comment.