Skip to content

Fix server address customizing in case of enabled pre-loading mode #1289

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
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 @@ -306,7 +306,7 @@ protected synchronized OpenAPI getOpenApi(Locale locale) {
// run the optional customisers
List<Server> servers = openApi.getServers();
openApiCustomisers.ifPresent(apiCustomisers -> apiCustomisers.forEach(openApiCustomiser -> openApiCustomiser.customise(openApi)));
if (servers != null && !servers.equals(openApi.getServers()))
if ((!CollectionUtils.isEmpty(openApi.getServers()) && !Objects.equals(servers, openApi.getServers())))
openAPIService.setServersPresent(true);

openAPIService.setCachedOpenAPI(openApi);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@

package org.springdoc.api;

import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.*;

import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.models.OpenAPI;
Expand All @@ -33,27 +30,30 @@
import io.swagger.v3.oas.models.media.NumberSchema;
import io.swagger.v3.oas.models.media.StringSchema;
import io.swagger.v3.oas.models.parameters.Parameter;
import io.swagger.v3.oas.models.servers.Server;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.internal.stubbing.answers.CallsRealMethods;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springdoc.core.AbstractRequestService;
import org.springdoc.core.GenericResponseService;
import org.springdoc.core.OpenAPIService;
import org.springdoc.core.OperationService;
import org.springdoc.core.SpringDocConfigProperties;
import org.springdoc.core.*;
import org.springdoc.core.customizers.OpenApiCustomiser;
import org.springdoc.core.customizers.OperationCustomizer;
import org.springdoc.core.fn.RouterOperation;

import org.springframework.beans.factory.ObjectFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.RequestMethod;

import static java.util.Arrays.asList;
import static java.util.Collections.singletonList;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.when;
import static org.springframework.web.bind.annotation.RequestMethod.GET;

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

when(openAPIBuilderObjectFactory.getObject()).thenReturn(openAPIService);
}

resource = new AbstractOpenApiResource(
@Test
void calculatePathFromRouterOperation() {
resource = new EmptyPathsOpenApiResource(
GROUP_NAME,
openAPIBuilderObjectFactory,
requestBuilder,
Expand All @@ -108,15 +111,8 @@ public void setUp() {
Optional.empty(),
new SpringDocConfigProperties(),
Optional.empty()
) {
);

@Override
protected void getPaths(final Map<String, Object> findRestControllers, Locale locale) { }
};
}

@Test
void calculatePathFromRouterOperation() {
final Parameter refParameter = new Parameter().$ref(PARAMETER_REFERENCE);

final Parameter numberParameterInPath = new Parameter()
Expand Down Expand Up @@ -160,4 +156,54 @@ void calculatePathFromRouterOperation() {
assertThat(parameterWithoutSchema.getSchema(), is(new StringSchema()));
assertThat(parameterWithoutSchema.getIn(), is(ParameterIn.QUERY.toString()));
}

@Test
void preLoadingModeShouldNotOverwriteServers() throws InterruptedException {
when(openAPIService.updateServers(any())).thenCallRealMethod();
when(openAPIService.getCachedOpenAPI()).thenCallRealMethod();
doAnswer(new CallsRealMethods()).when(openAPIService).setServersPresent(true);
doAnswer(new CallsRealMethods()).when(openAPIService).setServerBaseUrl(any());
doAnswer(new CallsRealMethods()).when(openAPIService).setCachedOpenAPI(any());

String customUrl = "https://custom.com";
String generatedUrl = "https://generated.com";
OpenApiCustomiser openApiCustomiser = openApi -> openApi.setServers(singletonList(new Server().url(customUrl)));
SpringDocConfigProperties properties = new SpringDocConfigProperties();
properties.setPreLoadingEnabled(true);

resource = new EmptyPathsOpenApiResource(
GROUP_NAME,
openAPIBuilderObjectFactory,
requestBuilder,
responseBuilder,
operationParser,
Optional.empty(),
Optional.of(singletonList(openApiCustomiser)),
properties,
Optional.empty()
);

// wait for executor to be done
Thread.sleep(1_000);

// emulate generating base url
openAPIService.setServerBaseUrl(generatedUrl);
openAPIService.updateServers(openAPI);

OpenAPI after = resource.getOpenApi(Locale.getDefault());

assertThat(after.getServers().get(0).getUrl(), is(customUrl));
}


private static class EmptyPathsOpenApiResource extends AbstractOpenApiResource {

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) {
super(groupName, openAPIBuilderObjectFactory, requestBuilder, responseBuilder, operationParser, operationCustomizers, openApiCustomisers, springDocConfigProperties, actuatorProvider);
}

@Override
public void getPaths(Map<String, Object> findRestControllers, Locale locale) {
}
}
}