Search in sources :

Example 1 with SpringDocConfigProperties

use of org.springdoc.core.SpringDocConfigProperties in project springdoc-openapi by springdoc.

the class AbstractOpenApiResourceTest method preLoadingModeShouldNotOverwriteServers.

@Test
void preLoadingModeShouldNotOverwriteServers() throws InterruptedException {
    when(openAPIService.updateServers(any())).thenCallRealMethod();
    when(openAPIService.getCachedOpenAPI(any())).thenCallRealMethod();
    doAnswer(new CallsRealMethods()).when(openAPIService).setServersPresent(true);
    doAnswer(new CallsRealMethods()).when(openAPIService).setServerBaseUrl(any());
    doAnswer(new CallsRealMethods()).when(openAPIService).setCachedOpenAPI(any(), 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)), Optional.empty(), properties, springDocProviders);
    // wait for executor to be done
    Thread.sleep(1_000);
    // emulate generating base url
    openAPIService.setServerBaseUrl(generatedUrl);
    openAPIService.updateServers(openAPI);
    Locale locale = Locale.US;
    OpenAPI after = resource.getOpenApi(locale);
    assertThat(after.getServers().get(0).getUrl(), is(customUrl));
}
Also used : OperationCustomizer(org.springdoc.core.customizers.OperationCustomizer) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BeforeEach(org.junit.jupiter.api.BeforeEach) GenericResponseService(org.springdoc.core.GenericResponseService) Mock(org.mockito.Mock) Parameter(io.swagger.v3.oas.models.parameters.Parameter) HashMap(java.util.HashMap) Operation(io.swagger.v3.oas.models.Operation) GET(org.springframework.web.bind.annotation.RequestMethod.GET) ParameterIn(io.swagger.v3.oas.annotations.enums.ParameterIn) Collections.singletonList(java.util.Collections.singletonList) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) NumberSchema(io.swagger.v3.oas.models.media.NumberSchema) OpenApiMethodFilter(org.springdoc.core.filters.OpenApiMethodFilter) Locale(java.util.Locale) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Arrays.asList(java.util.Arrays.asList) Map(java.util.Map) OperationService(org.springdoc.core.OperationService) Matchers.nullValue(org.hamcrest.Matchers.nullValue) Mockito.doAnswer(org.mockito.Mockito.doAnswer) OpenAPIService(org.springdoc.core.OpenAPIService) AbstractRequestService(org.springdoc.core.AbstractRequestService) SpringDocProviders(org.springdoc.core.SpringDocProviders) OpenApiCustomiser(org.springdoc.core.customizers.OpenApiCustomiser) RouterOperation(org.springdoc.core.fn.RouterOperation) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) SpringDocConfigProperties(org.springdoc.core.SpringDocConfigProperties) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) CallsRealMethods(org.mockito.internal.stubbing.answers.CallsRealMethods) PathItem(io.swagger.v3.oas.models.PathItem) ReflectionTestUtils(org.springframework.test.util.ReflectionTestUtils) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) Paths(io.swagger.v3.oas.models.Paths) Mockito.when(org.mockito.Mockito.when) ApplicationContext(org.springframework.context.ApplicationContext) Test(org.junit.jupiter.api.Test) ObjectFactory(org.springframework.beans.factory.ObjectFactory) List(java.util.List) Server(io.swagger.v3.oas.models.servers.Server) StringSchema(io.swagger.v3.oas.models.media.StringSchema) Matchers.containsInAnyOrder(org.hamcrest.Matchers.containsInAnyOrder) Optional(java.util.Optional) Matchers.is(org.hamcrest.Matchers.is) Locale(java.util.Locale) OpenApiCustomiser(org.springdoc.core.customizers.OpenApiCustomiser) SpringDocConfigProperties(org.springdoc.core.SpringDocConfigProperties) Server(io.swagger.v3.oas.models.servers.Server) CallsRealMethods(org.mockito.internal.stubbing.answers.CallsRealMethods) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.junit.jupiter.api.Test)

Example 2 with SpringDocConfigProperties

use of org.springdoc.core.SpringDocConfigProperties in project springdoc-openapi by springdoc.

the class AbstractOpenApiResourceTest method calculatePathFromRouterOperation.

@Test
void calculatePathFromRouterOperation() {
    resource = new EmptyPathsOpenApiResource(GROUP_NAME, openAPIBuilderObjectFactory, requestBuilder, responseBuilder, operationParser, Optional.empty(), Optional.empty(), Optional.empty(), new SpringDocConfigProperties(), springDocProviders);
    final Parameter refParameter = new Parameter().$ref(PARAMETER_REFERENCE);
    final Parameter numberParameterInPath = new Parameter().name(PARAMETER_WITH_NUMBER_SCHEMA_NAME).in(ParameterIn.PATH.toString()).schema(new NumberSchema());
    final Parameter parameterWithoutSchema = new Parameter().name(PARAMETER_WITHOUT_SCHEMA_NAME);
    final Operation operation = new Operation();
    operation.setParameters(asList(refParameter, numberParameterInPath, parameterWithoutSchema));
    final RouterOperation routerOperation = new RouterOperation();
    routerOperation.setMethods(new RequestMethod[] { GET });
    routerOperation.setOperationModel(operation);
    routerOperation.setPath(PATH);
    resource.calculatePath(routerOperation, Locale.getDefault());
    final List<Parameter> parameters = resource.getOpenApi(Locale.getDefault()).getPaths().get(PATH).getGet().getParameters();
    assertThat(parameters.size(), is(3));
    assertThat(parameters, containsInAnyOrder(refParameter, numberParameterInPath, parameterWithoutSchema));
    assertThat(refParameter.getName(), nullValue());
    assertThat(refParameter.get$ref(), is(PARAMETER_REFERENCE));
    assertThat(refParameter.getSchema(), nullValue());
    assertThat(refParameter.getIn(), nullValue());
    assertThat(numberParameterInPath.getName(), is(PARAMETER_WITH_NUMBER_SCHEMA_NAME));
    assertThat(numberParameterInPath.get$ref(), nullValue());
    assertThat(numberParameterInPath.getSchema(), is(new NumberSchema()));
    assertThat(numberParameterInPath.getIn(), is(ParameterIn.PATH.toString()));
    assertThat(parameterWithoutSchema.getName(), is(PARAMETER_WITHOUT_SCHEMA_NAME));
    assertThat(parameterWithoutSchema.get$ref(), nullValue());
    assertThat(parameterWithoutSchema.getSchema(), is(new StringSchema()));
    assertThat(parameterWithoutSchema.getIn(), is(ParameterIn.QUERY.toString()));
}
Also used : RouterOperation(org.springdoc.core.fn.RouterOperation) SpringDocConfigProperties(org.springdoc.core.SpringDocConfigProperties) Parameter(io.swagger.v3.oas.models.parameters.Parameter) NumberSchema(io.swagger.v3.oas.models.media.NumberSchema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) Operation(io.swagger.v3.oas.models.Operation) RouterOperation(org.springdoc.core.fn.RouterOperation) Test(org.junit.jupiter.api.Test)

Example 3 with SpringDocConfigProperties

use of org.springdoc.core.SpringDocConfigProperties in project jhipster-bom by jhipster.

the class JHipsterSpringDocGroupsConfiguration method openAPIDefaultGroupedOpenAPI.

/**
 * OpenApi default group configuration.
 *
 * @return the GroupedOpenApi configuration
 */
@Bean
@ConditionalOnMissingBean(name = "openAPIDefaultGroupedOpenAPI")
public GroupedOpenApi openAPIDefaultGroupedOpenAPI(List<OpenApiCustomiser> openApiCustomisers, List<OperationCustomizer> operationCustomizers, @Qualifier("apiFirstGroupedOpenAPI") Optional<GroupedOpenApi> apiFirstGroupedOpenAPI, SpringDocConfigProperties springDocConfigProperties) {
    log.debug("Initializing JHipster OpenApi default group");
    Builder builder = GroupedOpenApi.builder().group(DEFAULT_GROUP_NAME).pathsToMatch(properties.getDefaultIncludePattern());
    openApiCustomisers.stream().filter(customiser -> !(customiser instanceof ActuatorOpenApiCustomizer)).forEach(builder::addOpenApiCustomiser);
    operationCustomizers.stream().filter(customiser -> !(customiser instanceof ActuatorOperationCustomizer)).forEach(builder::addOperationCustomizer);
    apiFirstGroupedOpenAPI.ifPresent(apiFirst -> {
        if (apiFirst.getPackagesToScan() != null) {
            apiFirst.getPackagesToScan().stream().forEach(builder::packagesToExclude);
        }
    });
    return builder.build();
}
Also used : ActuatorOpenApiCustomizer(org.springdoc.core.customizers.ActuatorOpenApiCustomizer) OperationCustomizer(org.springdoc.core.customizers.OperationCustomizer) Builder(org.springdoc.core.GroupedOpenApi.Builder) ConditionalOnClass(org.springframework.boot.autoconfigure.condition.ConditionalOnClass) JHipsterOpenApiCustomizer(tech.jhipster.config.apidoc.customizer.JHipsterOpenApiCustomizer) LoggerFactory(org.slf4j.LoggerFactory) StringUtils(org.apache.commons.lang3.StringUtils) ByteBuffer(java.nio.ByteBuffer) Value(org.springframework.beans.factory.annotation.Value) JHipsterProperties(tech.jhipster.config.JHipsterProperties) DEFAULT_GROUP_NAME(org.springdoc.core.Constants.DEFAULT_GROUP_NAME) Qualifier(org.springframework.beans.factory.annotation.Qualifier) SpringDocUtils.getConfig(org.springdoc.core.SpringDocUtils.getConfig) OpenApiCustomiser(org.springdoc.core.customizers.OpenApiCustomiser) ConditionalOnProperty(org.springframework.boot.autoconfigure.condition.ConditionalOnProperty) SpringDocConfigProperties(org.springdoc.core.SpringDocConfigProperties) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) Logger(org.slf4j.Logger) GroupedOpenApi(org.springdoc.core.GroupedOpenApi) Info(io.swagger.v3.oas.models.info.Info) Configuration(org.springframework.context.annotation.Configuration) SPRINGDOC_SHOW_ACTUATOR(org.springdoc.core.Constants.SPRINGDOC_SHOW_ACTUATOR) List(java.util.List) Optional(java.util.Optional) Bean(org.springframework.context.annotation.Bean) ActuatorOperationCustomizer(org.springdoc.core.customizers.ActuatorOperationCustomizer) ActuatorOpenApiCustomizer(org.springdoc.core.customizers.ActuatorOpenApiCustomizer) Builder(org.springdoc.core.GroupedOpenApi.Builder) ActuatorOperationCustomizer(org.springdoc.core.customizers.ActuatorOperationCustomizer) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) Bean(org.springframework.context.annotation.Bean)

Aggregations

SpringDocConfigProperties (org.springdoc.core.SpringDocConfigProperties)3 Operation (io.swagger.v3.oas.models.Operation)2 NumberSchema (io.swagger.v3.oas.models.media.NumberSchema)2 StringSchema (io.swagger.v3.oas.models.media.StringSchema)2 Parameter (io.swagger.v3.oas.models.parameters.Parameter)2 List (java.util.List)2 Optional (java.util.Optional)2 Test (org.junit.jupiter.api.Test)2 OpenApiCustomiser (org.springdoc.core.customizers.OpenApiCustomiser)2 OperationCustomizer (org.springdoc.core.customizers.OperationCustomizer)2 RouterOperation (org.springdoc.core.fn.RouterOperation)2 ParameterIn (io.swagger.v3.oas.annotations.enums.ParameterIn)1 OpenAPI (io.swagger.v3.oas.models.OpenAPI)1 PathItem (io.swagger.v3.oas.models.PathItem)1 Paths (io.swagger.v3.oas.models.Paths)1 Info (io.swagger.v3.oas.models.info.Info)1 Server (io.swagger.v3.oas.models.servers.Server)1 ByteBuffer (java.nio.ByteBuffer)1 Arrays.asList (java.util.Arrays.asList)1 Collections.singletonList (java.util.Collections.singletonList)1