Skip to content

Commit 7a90e0e

Browse files
committed
fix server address customizing in case of enabled pre-loading mode
1 parent be925d4 commit 7a90e0e

File tree

2 files changed

+65
-19
lines changed

2 files changed

+65
-19
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ protected synchronized OpenAPI getOpenApi(Locale locale) {
306306
// run the optional customisers
307307
List<Server> servers = openApi.getServers();
308308
openApiCustomisers.ifPresent(apiCustomisers -> apiCustomisers.forEach(openApiCustomiser -> openApiCustomiser.customise(openApi)));
309-
if (servers != null && !servers.equals(openApi.getServers()))
309+
if ((!CollectionUtils.isEmpty(openApi.getServers()) && !Objects.equals(servers, openApi.getServers())))
310310
openAPIService.setServersPresent(true);
311311

312312
openAPIService.setCachedOpenAPI(openApi);

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

+64-18
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,7 @@
2020

2121
package org.springdoc.api;
2222

23-
import java.util.List;
24-
import java.util.Locale;
25-
import java.util.Map;
26-
import java.util.Optional;
23+
import java.util.*;
2724

2825
import io.swagger.v3.oas.annotations.enums.ParameterIn;
2926
import io.swagger.v3.oas.models.OpenAPI;
@@ -33,27 +30,30 @@
3330
import io.swagger.v3.oas.models.media.NumberSchema;
3431
import io.swagger.v3.oas.models.media.StringSchema;
3532
import io.swagger.v3.oas.models.parameters.Parameter;
33+
import io.swagger.v3.oas.models.servers.Server;
3634
import org.junit.jupiter.api.BeforeEach;
3735
import org.junit.jupiter.api.Test;
3836
import org.junit.jupiter.api.extension.ExtendWith;
3937
import org.mockito.Mock;
38+
import org.mockito.internal.stubbing.answers.CallsRealMethods;
4039
import org.mockito.junit.jupiter.MockitoExtension;
41-
import org.springdoc.core.AbstractRequestService;
42-
import org.springdoc.core.GenericResponseService;
43-
import org.springdoc.core.OpenAPIService;
44-
import org.springdoc.core.OperationService;
45-
import org.springdoc.core.SpringDocConfigProperties;
40+
import org.springdoc.core.*;
41+
import org.springdoc.core.customizers.OpenApiCustomiser;
42+
import org.springdoc.core.customizers.OperationCustomizer;
4643
import org.springdoc.core.fn.RouterOperation;
4744

4845
import org.springframework.beans.factory.ObjectFactory;
4946
import org.springframework.context.ApplicationContext;
5047
import org.springframework.web.bind.annotation.RequestMethod;
5148

5249
import static java.util.Arrays.asList;
50+
import static java.util.Collections.singletonList;
5351
import static org.hamcrest.MatcherAssert.assertThat;
5452
import static org.hamcrest.Matchers.containsInAnyOrder;
5553
import static org.hamcrest.Matchers.is;
5654
import static org.hamcrest.Matchers.nullValue;
55+
import static org.mockito.ArgumentMatchers.any;
56+
import static org.mockito.Mockito.doAnswer;
5757
import static org.mockito.Mockito.when;
5858
import static org.springframework.web.bind.annotation.RequestMethod.GET;
5959

@@ -97,8 +97,11 @@ public void setUp() {
9797
when(openAPIService.getContext()).thenReturn(context);
9898

9999
when(openAPIBuilderObjectFactory.getObject()).thenReturn(openAPIService);
100+
}
100101

101-
resource = new AbstractOpenApiResource(
102+
@Test
103+
void calculatePathFromRouterOperation() {
104+
resource = new EmptyPathsOpenApiResource(
102105
GROUP_NAME,
103106
openAPIBuilderObjectFactory,
104107
requestBuilder,
@@ -108,15 +111,8 @@ public void setUp() {
108111
Optional.empty(),
109112
new SpringDocConfigProperties(),
110113
Optional.empty()
111-
) {
114+
);
112115

113-
@Override
114-
protected void getPaths(final Map<String, Object> findRestControllers, Locale locale) { }
115-
};
116-
}
117-
118-
@Test
119-
void calculatePathFromRouterOperation() {
120116
final Parameter refParameter = new Parameter().$ref(PARAMETER_REFERENCE);
121117

122118
final Parameter numberParameterInPath = new Parameter()
@@ -160,4 +156,54 @@ void calculatePathFromRouterOperation() {
160156
assertThat(parameterWithoutSchema.getSchema(), is(new StringSchema()));
161157
assertThat(parameterWithoutSchema.getIn(), is(ParameterIn.QUERY.toString()));
162158
}
159+
160+
@Test
161+
void preLoadingModeShouldNotOverwriteServers() throws InterruptedException {
162+
when(openAPIService.updateServers(any())).thenCallRealMethod();
163+
when(openAPIService.getCachedOpenAPI()).thenCallRealMethod();
164+
doAnswer(new CallsRealMethods()).when(openAPIService).setServersPresent(true);
165+
doAnswer(new CallsRealMethods()).when(openAPIService).setServerBaseUrl(any());
166+
doAnswer(new CallsRealMethods()).when(openAPIService).setCachedOpenAPI(any());
167+
168+
String customUrl = "https://custom.com";
169+
String generatedUrl = "https://generated.com";
170+
OpenApiCustomiser openApiCustomiser = openApi -> openApi.setServers(singletonList(new Server().url(customUrl)));
171+
SpringDocConfigProperties properties = new SpringDocConfigProperties();
172+
properties.setPreLoadingEnabled(true);
173+
174+
resource = new EmptyPathsOpenApiResource(
175+
GROUP_NAME,
176+
openAPIBuilderObjectFactory,
177+
requestBuilder,
178+
responseBuilder,
179+
operationParser,
180+
Optional.empty(),
181+
Optional.of(singletonList(openApiCustomiser)),
182+
properties,
183+
Optional.empty()
184+
);
185+
186+
// wait for executor to be done
187+
Thread.sleep(1_000);
188+
189+
// emulate generating base url
190+
openAPIService.setServerBaseUrl(generatedUrl);
191+
openAPIService.updateServers(openAPI);
192+
193+
OpenAPI after = resource.getOpenApi(Locale.getDefault());
194+
195+
assertThat(after.getServers().get(0).getUrl(), is(customUrl));
196+
}
197+
198+
199+
private static class EmptyPathsOpenApiResource extends AbstractOpenApiResource {
200+
201+
EmptyPathsOpenApiResource(String groupName, ObjectFactory<OpenAPIService> openAPIBuilderObjectFactory, AbstractRequestService requestBuilder, GenericResponseService responseBuilder, OperationService operationParser, Optional<List<OperationCustomizer>> operationCustomizers, Optional<List<OpenApiCustomiser>> openApiCustomisers, SpringDocConfigProperties springDocConfigProperties, Optional<ActuatorProvider> actuatorProvider) {
202+
super(groupName, openAPIBuilderObjectFactory, requestBuilder, responseBuilder, operationParser, operationCustomizers, openApiCustomisers, springDocConfigProperties, actuatorProvider);
203+
}
204+
205+
@Override
206+
public void getPaths(Map<String, Object> findRestControllers, Locale locale) {
207+
}
208+
}
163209
}

0 commit comments

Comments
 (0)