Search in sources :

Example 1 with OpenApiCustomiser

use of org.springdoc.core.customizers.OpenApiCustomiser in project Projeto-Integrador by ProjetoAmbar.

the class SwaggerConfig method customerGlobalHeaderOpenApiCustomiser.

@Bean
public OpenApiCustomiser customerGlobalHeaderOpenApiCustomiser() {
    return openApi -> {
        openApi.getPaths().values().forEach(pathItem -> pathItem.readOperations().forEach(operation -> {
            ApiResponses apiResponses = operation.getResponses();
            apiResponses.addApiResponse("200", createApiResponse("Sucesso!"));
            apiResponses.addApiResponse("201", createApiResponse("Objeto Persistido!"));
            apiResponses.addApiResponse("204", createApiResponse("Objeto Excluído!"));
            apiResponses.addApiResponse("400", createApiResponse("Erro na Requisição!"));
            apiResponses.addApiResponse("401", createApiResponse("Acesso Não Autorizado!"));
            apiResponses.addApiResponse("404", createApiResponse("Objeto Não Encontrado!"));
            apiResponses.addApiResponse("500", createApiResponse("Erro na Aplicação!"));
        }));
    };
}
Also used : Configuration(org.springframework.context.annotation.Configuration) ApiResponses(io.swagger.v3.oas.models.responses.ApiResponses) ApiResponse(io.swagger.v3.oas.models.responses.ApiResponse) License(io.swagger.v3.oas.models.info.License) Contact(io.swagger.v3.oas.models.info.Contact) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Info(io.swagger.v3.oas.models.info.Info) OpenApiCustomiser(org.springdoc.core.customizers.OpenApiCustomiser) Bean(org.springframework.context.annotation.Bean) ExternalDocumentation(io.swagger.v3.oas.models.ExternalDocumentation) ApiResponses(io.swagger.v3.oas.models.responses.ApiResponses) Bean(org.springframework.context.annotation.Bean)

Example 2 with OpenApiCustomiser

use of org.springdoc.core.customizers.OpenApiCustomiser in project springdoc-openapi by springdoc.

the class SpringDocTestApp method serverOpenApiCustomiser1.

public OpenApiCustomiser serverOpenApiCustomiser1() {
    Server server = new Server().url("http://toto.v1.com").description("myserver1");
    List<Server> servers = new ArrayList<>();
    servers.add(server);
    return openApi -> openApi.setServers(servers);
}
Also used : OperationCustomizer(org.springdoc.core.customizers.OperationCustomizer) SecurityScheme(io.swagger.v3.oas.models.security.SecurityScheme) SpringBootApplication(org.springframework.boot.autoconfigure.SpringBootApplication) GroupedOpenApi(org.springdoc.core.GroupedOpenApi) Info(io.swagger.v3.oas.models.info.Info) Operation(io.swagger.v3.oas.models.Operation) StringUtils(org.apache.commons.lang3.StringUtils) ArrayList(java.util.ArrayList) SpringApplication(org.springframework.boot.SpringApplication) HandlerMethod(org.springframework.web.method.HandlerMethod) List(java.util.List) License(io.swagger.v3.oas.models.info.License) Server(io.swagger.v3.oas.models.servers.Server) Constants(org.springdoc.core.Constants) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Components(io.swagger.v3.oas.models.Components) OpenApiCustomiser(org.springdoc.core.customizers.OpenApiCustomiser) Bean(org.springframework.context.annotation.Bean) Server(io.swagger.v3.oas.models.servers.Server) ArrayList(java.util.ArrayList)

Example 3 with OpenApiCustomiser

use of org.springdoc.core.customizers.OpenApiCustomiser 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 4 with OpenApiCustomiser

use of org.springdoc.core.customizers.OpenApiCustomiser 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)

Example 5 with OpenApiCustomiser

use of org.springdoc.core.customizers.OpenApiCustomiser in project DiversiFica by teey-tech.

the class SwaggerConfig method customerGlobalHeaderOpenApiCustomiser.

@Bean
public OpenApiCustomiser customerGlobalHeaderOpenApiCustomiser() {
    return openApi -> {
        openApi.getPaths().values().forEach(pathItem -> pathItem.readOperations().forEach(operation -> {
            ApiResponses apiResponses = operation.getResponses();
            apiResponses.addApiResponse("200", createApiResponse("Sucesso!"));
            apiResponses.addApiResponse("201", createApiResponse("Objeto Persistido!"));
            apiResponses.addApiResponse("204", createApiResponse("Objeto Excluído!"));
            apiResponses.addApiResponse("400", createApiResponse("Erro na Requisição!"));
            apiResponses.addApiResponse("401", createApiResponse("Acesso Não Autorizado!"));
            apiResponses.addApiResponse("404", createApiResponse("Objeto Não Encontrado!"));
            apiResponses.addApiResponse("500", createApiResponse("Erro na Aplicação!"));
        }));
    };
}
Also used : Configuration(org.springframework.context.annotation.Configuration) ApiResponses(io.swagger.v3.oas.models.responses.ApiResponses) ApiResponse(io.swagger.v3.oas.models.responses.ApiResponse) License(io.swagger.v3.oas.models.info.License) Contact(io.swagger.v3.oas.models.info.Contact) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Info(io.swagger.v3.oas.models.info.Info) OpenApiCustomiser(org.springdoc.core.customizers.OpenApiCustomiser) Bean(org.springframework.context.annotation.Bean) ExternalDocumentation(io.swagger.v3.oas.models.ExternalDocumentation) ApiResponses(io.swagger.v3.oas.models.responses.ApiResponses) Bean(org.springframework.context.annotation.Bean)

Aggregations

OpenApiCustomiser (org.springdoc.core.customizers.OpenApiCustomiser)7 OpenAPI (io.swagger.v3.oas.models.OpenAPI)6 Info (io.swagger.v3.oas.models.info.Info)5 List (java.util.List)5 OperationCustomizer (org.springdoc.core.customizers.OperationCustomizer)5 Operation (io.swagger.v3.oas.models.Operation)4 License (io.swagger.v3.oas.models.info.License)4 Server (io.swagger.v3.oas.models.servers.Server)4 StringUtils (org.apache.commons.lang3.StringUtils)4 Bean (org.springframework.context.annotation.Bean)4 Components (io.swagger.v3.oas.models.Components)3 ApiResponses (io.swagger.v3.oas.models.responses.ApiResponses)3 ArrayList (java.util.ArrayList)3 Configuration (org.springframework.context.annotation.Configuration)3 ParameterIn (io.swagger.v3.oas.annotations.enums.ParameterIn)2 ExternalDocumentation (io.swagger.v3.oas.models.ExternalDocumentation)2 PathItem (io.swagger.v3.oas.models.PathItem)2 Paths (io.swagger.v3.oas.models.Paths)2 Contact (io.swagger.v3.oas.models.info.Contact)2 StringSchema (io.swagger.v3.oas.models.media.StringSchema)2