Search in sources :

Example 6 with JaxrsOpenApiContextBuilder

use of io.swagger.v3.jaxrs2.integration.JaxrsOpenApiContextBuilder in project swagger-core by swagger-api.

the class BaseOpenApiResource method getOpenApi.

protected Response getOpenApi(HttpHeaders headers, ServletConfig config, Application app, UriInfo uriInfo, String type) throws Exception {
    String ctxId = getContextId(config);
    OpenApiContext ctx = new JaxrsOpenApiContextBuilder().servletConfig(config).application(app).resourcePackages(resourcePackages).configLocation(configLocation).openApiConfiguration(openApiConfiguration).ctxId(ctxId).buildContext(true);
    OpenAPI oas = ctx.read();
    boolean pretty = false;
    if (ctx.getOpenApiConfiguration() != null && Boolean.TRUE.equals(ctx.getOpenApiConfiguration().isPrettyPrint())) {
        pretty = true;
    }
    if (oas != null) {
        if (ctx.getOpenApiConfiguration() != null && ctx.getOpenApiConfiguration().getFilterClass() != null) {
            try {
                OpenAPISpecFilter filterImpl = (OpenAPISpecFilter) Class.forName(ctx.getOpenApiConfiguration().getFilterClass()).newInstance();
                SpecFilter f = new SpecFilter();
                oas = f.filter(oas, filterImpl, getQueryParams(uriInfo.getQueryParameters()), getCookies(headers), getHeaders(headers));
            } catch (Exception e) {
                LOGGER.error("failed to load filter", e);
            }
        }
    }
    if (oas == null) {
        return Response.status(404).build();
    }
    if (StringUtils.isNotBlank(type) && type.trim().equalsIgnoreCase("yaml")) {
        return Response.status(Response.Status.OK).entity(pretty ? ctx.getOutputYamlMapper().writer(new DefaultPrettyPrinter()).writeValueAsString(oas) : ctx.getOutputYamlMapper().writeValueAsString(oas)).type("application/yaml").build();
    } else {
        return Response.status(Response.Status.OK).entity(pretty ? ctx.getOutputJsonMapper().writer(new DefaultPrettyPrinter()).writeValueAsString(oas) : ctx.getOutputJsonMapper().writeValueAsString(oas)).type(MediaType.APPLICATION_JSON_TYPE).build();
    }
}
Also used : DefaultPrettyPrinter(com.fasterxml.jackson.core.util.DefaultPrettyPrinter) OpenAPISpecFilter(io.swagger.v3.core.filter.OpenAPISpecFilter) JaxrsOpenApiContextBuilder(io.swagger.v3.jaxrs2.integration.JaxrsOpenApiContextBuilder) OpenAPI(io.swagger.v3.oas.models.OpenAPI) SpecFilter(io.swagger.v3.core.filter.SpecFilter) OpenAPISpecFilter(io.swagger.v3.core.filter.OpenAPISpecFilter) OpenApiContext(io.swagger.v3.oas.integration.api.OpenApiContext)

Aggregations

OpenApiConfigurationException (io.swagger.v3.oas.integration.OpenApiConfigurationException)5 SwaggerConfiguration (io.swagger.v3.oas.integration.SwaggerConfiguration)5 OpenApiContext (io.swagger.v3.oas.integration.api.OpenApiContext)4 OpenAPI (io.swagger.v3.oas.models.OpenAPI)4 DefaultPrettyPrinter (com.fasterxml.jackson.core.util.DefaultPrettyPrinter)3 OpenAPISpecFilter (io.swagger.v3.core.filter.OpenAPISpecFilter)3 SpecFilter (io.swagger.v3.core.filter.SpecFilter)3 JaxrsOpenApiContextBuilder (io.swagger.v3.jaxrs2.integration.JaxrsOpenApiContextBuilder)3 HashSet (java.util.HashSet)3 OpenAPIDefinition (io.swagger.v3.oas.annotations.OpenAPIDefinition)2 GenericOpenApiContextBuilder (io.swagger.v3.oas.integration.GenericOpenApiContextBuilder)2 IgnoredPackages (io.swagger.v3.oas.integration.IgnoredPackages)2 LinkedHashSet (java.util.LinkedHashSet)2 Set (java.util.Set)2 Collectors (java.util.stream.Collectors)2 ServletContainerInitializer (javax.servlet.ServletContainerInitializer)2 ServletContext (javax.servlet.ServletContext)2 ServletException (javax.servlet.ServletException)2 HandlesTypes (javax.servlet.annotation.HandlesTypes)2 ApplicationPath (javax.ws.rs.ApplicationPath)2