Skip to content

Update server out of cache #1749

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
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 @@ -1327,4 +1313,4 @@ enum ConditionType {
public static void setModelAndViewClass(Class<?> modelAndViewClass) {
AbstractOpenApiResource.modelAndViewClass = modelAndViewClass;
}
}
}
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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}}