Skip to content

Commit 9320d74

Browse files
author
Ben Einaudi
committed
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
1 parent 5bd85e1 commit 9320d74

File tree

4 files changed

+12
-28
lines changed

4 files changed

+12
-28
lines changed

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

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ public abstract class AbstractOpenApiResource extends SpecFilter {
169169
private final OperationService operationParser;
170170

171171
/**
172-
* The Open api customisers.
172+
* The Open api customizers.
173173
*/
174174
private final Optional<List<OpenApiCustomiser>> openApiCustomisers;
175175

@@ -299,7 +299,7 @@ public static void addHiddenRestControllers(String... classes) {
299299
* @return the open api
300300
*/
301301
protected synchronized OpenAPI getOpenApi(Locale locale) {
302-
OpenAPI openAPI;
302+
final OpenAPI openAPI;
303303
final Locale finalLocale = locale == null ? Locale.getDefault() : locale;
304304
if (openAPIService.getCachedOpenAPI(finalLocale) == null || springDocConfigProperties.isCacheDisabled()) {
305305
Instant start = Instant.now();
@@ -327,29 +327,12 @@ protected synchronized OpenAPI getOpenApi(Locale locale) {
327327
this.calculatePath(routerOperationList, locale, openAPI);
328328
}
329329
);
330-
331330
if (!CollectionUtils.isEmpty(openAPI.getServers()))
332331
openAPIService.setServersPresent(true);
333-
openAPIService.updateServers(openAPI);
334332

335333
if (springDocConfigProperties.isRemoveBrokenReferenceDefinitions())
336334
this.removeBrokenReferenceDefinitions(openAPI);
337335

338-
// run the optional customisers
339-
List<Server> servers = openAPI.getServers();
340-
List<Server> serversCopy = null;
341-
try {
342-
serversCopy = springDocProviders.jsonMapper()
343-
.readValue(springDocProviders.jsonMapper().writeValueAsString(servers), new TypeReference<List<Server>>() {});
344-
}
345-
catch (JsonProcessingException e) {
346-
LOGGER.warn("Json Processing Exception occurred: {}", e.getMessage());
347-
}
348-
349-
openApiLocaleCustomizers.values().forEach(openApiLocaleCustomizer -> openApiLocaleCustomizer.customise(openAPI, finalLocale));
350-
openApiCustomisers.ifPresent(apiCustomisers -> apiCustomisers.forEach(openApiCustomiser -> openApiCustomiser.customise(openAPI)));
351-
if (!CollectionUtils.isEmpty(openAPI.getServers()) && !openAPI.getServers().equals(serversCopy))
352-
openAPIService.setServersPresent(true);
353336

354337
openAPIService.setCachedOpenAPI(openAPI, finalLocale);
355338

@@ -358,8 +341,11 @@ protected synchronized OpenAPI getOpenApi(Locale locale) {
358341
}
359342
else {
360343
LOGGER.debug("Fetching openApi document from cache");
361-
openAPI = openAPIService.updateServers(openAPIService.getCachedOpenAPI(finalLocale));
344+
openAPI = openAPIService.getCachedOpenAPI(finalLocale);
362345
}
346+
openAPIService.updateServers(openAPI);
347+
openApiLocaleCustomizers.values().forEach(openApiLocaleCustomizer -> openApiLocaleCustomizer.customise(openAPI, finalLocale));
348+
openApiCustomisers.ifPresent(apiCustomizers -> apiCustomizers.forEach(openApiCustomizer -> openApiCustomizer.customise(openAPI)));
363349
return openAPI;
364350
}
365351

@@ -1327,4 +1313,4 @@ enum ConditionType {
13271313
public static void setModelAndViewClass(Class<?> modelAndViewClass) {
13281314
AbstractOpenApiResource.modelAndViewClass = modelAndViewClass;
13291315
}
1330-
}
1316+
}

springdoc-openapi-common/src/main/java/org/springdoc/core/OpenAPIService.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -296,17 +296,15 @@ private void initializeHiddenRestController() {
296296
* Update servers open api.
297297
*
298298
* @param openAPI the open api
299-
* @return the open api
300299
*/
301-
public OpenAPI updateServers(OpenAPI openAPI) {
300+
public void updateServers(OpenAPI openAPI) {
302301
if (!isServersPresent && serverBaseUrl != null) // default server value
303302
{
304303
Server server = new Server().url(serverBaseUrl).description(DEFAULT_SERVER_DESCRIPTION);
305304
List<Server> servers = new ArrayList<>();
306305
servers.add(server);
307306
openAPI.setServers(servers);
308307
}
309-
return openAPI;
310308
}
311309

312310
/**

springdoc-openapi-common/src/test/java/org/springdoc/api/AbstractOpenApiResourceTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
import static org.hamcrest.Matchers.nullValue;
7171
import static org.mockito.ArgumentMatchers.any;
7272
import static org.mockito.Mockito.doAnswer;
73+
import static org.mockito.Mockito.doCallRealMethod;
7374
import static org.mockito.Mockito.when;
7475
import static org.springframework.web.bind.annotation.RequestMethod.GET;
7576

@@ -122,7 +123,6 @@ public void setUp() {
122123
when(openAPIService.build(any())).thenReturn(openAPI);
123124

124125
when(openAPIBuilderObjectFactory.getObject()).thenReturn(openAPIService);
125-
when(springDocProviders.jsonMapper()).thenReturn(Json.mapper());
126126
}
127127

128128
@Test
@@ -186,7 +186,7 @@ void calculatePathFromRouterOperation() {
186186

187187
@Test
188188
void preLoadingModeShouldNotOverwriteServers() throws InterruptedException {
189-
when(openAPIService.updateServers(any())).thenCallRealMethod();
189+
doCallRealMethod().when(openAPIService).updateServers(any());
190190
when(openAPIService.getCachedOpenAPI(any())).thenCallRealMethod();
191191
doAnswer(new CallsRealMethods()).when(openAPIService).setServersPresent(true);
192192
doAnswer(new CallsRealMethods()).when(openAPIService).setServerBaseUrl(any());
@@ -224,7 +224,7 @@ void preLoadingModeShouldNotOverwriteServers() throws InterruptedException {
224224

225225
@Test
226226
void serverBaseUrlCustomisersTest() throws InterruptedException {
227-
when(openAPIService.updateServers(any())).thenCallRealMethod();
227+
doCallRealMethod().when(openAPIService).updateServers(any());
228228
when(openAPIService.getCachedOpenAPI(any())).thenCallRealMethod();
229229
doAnswer(new CallsRealMethods()).when(openAPIService).setServerBaseUrl(any());
230230
doAnswer(new CallsRealMethods()).when(openAPIService).setCachedOpenAPI(any(), any());
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +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}}
1+
{"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 commit comments

Comments
 (0)