Search in sources :

Example 31 with SpecFilter

use of io.swagger.v3.core.filter.SpecFilter in project swagger-core by swagger-api.

the class SpecFilterTest method filterAwayPetRefInSchemas.

@Test(description = "it should filter any Pet Ref in Schemas")
public void filterAwayPetRefInSchemas() throws IOException {
    final OpenAPI openAPI = getOpenAPI(RESOURCE_PATH);
    final OpenAPI filtered = new SpecFilter().filter(openAPI, new NoPetRefSchemaFilter(), null, null, null);
    validateSchemasInComponents(filtered.getComponents(), PET_MODEL);
}
Also used : NoPetRefSchemaFilter(io.swagger.v3.core.filter.resources.NoPetRefSchemaFilter) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.testng.annotations.Test)

Example 32 with SpecFilter

use of io.swagger.v3.core.filter.SpecFilter 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

OpenAPI (io.swagger.v3.oas.models.OpenAPI)32 Test (org.testng.annotations.Test)28 OpenAPISpecFilter (io.swagger.v3.core.filter.OpenAPISpecFilter)8 SpecFilter (io.swagger.v3.core.filter.SpecFilter)8 RemoveUnreferencedDefinitionsFilter (io.swagger.v3.core.filter.resources.RemoveUnreferencedDefinitionsFilter)8 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)6 NoOpOperationsFilter (io.swagger.v3.core.filter.resources.NoOpOperationsFilter)5 Map (java.util.Map)5 DefaultPrettyPrinter (com.fasterxml.jackson.core.util.DefaultPrettyPrinter)4 AbstractSpecFilter (io.swagger.v3.core.filter.AbstractSpecFilter)4 OpenApiContext (io.swagger.v3.oas.integration.api.OpenApiContext)4 Components (io.swagger.v3.oas.models.Components)4 PathItem (io.swagger.v3.oas.models.PathItem)4 Info (io.swagger.v3.oas.models.info.Info)4 OpenApiConfigurationException (io.swagger.v3.oas.integration.OpenApiConfigurationException)3 IOException (java.io.IOException)3 InternalModelPropertiesRemoverFilter (io.swagger.v3.core.filter.resources.InternalModelPropertiesRemoverFilter)2 NoPetOperationsFilter (io.swagger.v3.core.filter.resources.NoPetOperationsFilter)2 JaxrsOpenApiContextBuilder (io.swagger.v3.jaxrs2.integration.JaxrsOpenApiContextBuilder)2 GenericOpenApiContextBuilder (io.swagger.v3.oas.integration.GenericOpenApiContextBuilder)2