Search in sources :

Example 11 with OpenAPISpecFilter

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

Example 12 with OpenAPISpecFilter

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

the class SpecFilter method filterOperation.

protected Operation filterOperation(OpenAPISpecFilter filter, Operation operation, String resourcePath, String key, Map<String, List<String>> params, Map<String, String> cookies, Map<String, List<String>> headers) {
    if (operation != null) {
        ApiDescription description = new ApiDescription(resourcePath, key);
        Optional<Operation> filteredOperation = filter.filterOperation(operation, description, params, cookies, headers);
        if (filteredOperation.isPresent()) {
            List<Parameter> filteredParameters = new ArrayList<>();
            Operation filteredOperationGet = filteredOperation.get();
            Operation clone = new Operation();
            clone.setCallbacks(filteredOperationGet.getCallbacks());
            clone.setDeprecated(filteredOperationGet.getDeprecated());
            clone.setDescription(filteredOperationGet.getDescription());
            clone.setExtensions(filteredOperationGet.getExtensions());
            clone.setExternalDocs(filteredOperationGet.getExternalDocs());
            clone.setOperationId(filteredOperationGet.getOperationId());
            clone.setSecurity(filteredOperationGet.getSecurity());
            clone.setServers(filteredOperationGet.getServers());
            clone.setSummary(filteredOperationGet.getSummary());
            clone.setTags(filteredOperationGet.getTags());
            List<Parameter> parameters = filteredOperationGet.getParameters();
            if (parameters != null) {
                for (Parameter parameter : parameters) {
                    Parameter filteredParameter = filterParameter(filter, operation, parameter, resourcePath, key, params, cookies, headers);
                    if (filteredParameter != null) {
                        filteredParameters.add(filteredParameter);
                    }
                }
                clone.setParameters(filteredParameters);
            }
            RequestBody requestBody = filteredOperation.get().getRequestBody();
            if (requestBody != null) {
                RequestBody filteredRequestBody = filterRequestBody(filter, operation, requestBody, resourcePath, key, params, cookies, headers);
                clone.setRequestBody(filteredRequestBody);
            }
            ApiResponses responses = filteredOperation.get().getResponses();
            ApiResponses clonedResponses = responses;
            if (responses != null) {
                responses.forEach((responseKey, response) -> {
                    ApiResponse filteredResponse = filterResponse(filter, operation, response, resourcePath, key, params, cookies, headers);
                    if (filteredResponse != null) {
                        clonedResponses.addApiResponse(responseKey, filteredResponse);
                    }
                });
                clone.setResponses(clonedResponses);
            }
            return clone;
        }
    }
    return null;
}
Also used : ArrayList(java.util.ArrayList) Parameter(io.swagger.v3.oas.models.parameters.Parameter) Operation(io.swagger.v3.oas.models.Operation) ApiDescription(io.swagger.v3.core.model.ApiDescription) ApiResponses(io.swagger.v3.oas.models.responses.ApiResponses) ApiResponse(io.swagger.v3.oas.models.responses.ApiResponse) RequestBody(io.swagger.v3.oas.models.parameters.RequestBody)

Example 13 with OpenAPISpecFilter

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

the class SpecFilter method filterPathItem.

protected PathItem filterPathItem(OpenAPISpecFilter filter, PathItem pathItem, String resourcePath, Map<String, List<String>> params, Map<String, String> cookies, Map<String, List<String>> headers) {
    ApiDescription description = new ApiDescription(resourcePath, null);
    Optional<PathItem> filteredPathItem = filter.filterPathItem(pathItem, description, params, cookies, headers);
    if (filteredPathItem.isPresent()) {
        return filteredPathItem.get();
    }
    return null;
}
Also used : PathItem(io.swagger.v3.oas.models.PathItem) ApiDescription(io.swagger.v3.core.model.ApiDescription)

Example 14 with OpenAPISpecFilter

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

the class SpecFilter method filterComponentsSchema.

protected Map<String, Schema> filterComponentsSchema(OpenAPISpecFilter filter, Map<String, Schema> schemasMap, Map<String, List<String>> params, Map<String, String> cookies, Map<String, List<String>> headers) {
    if (schemasMap == null) {
        return null;
    }
    Map<String, Schema> clonedComponentsSchema = new LinkedHashMap<>();
    for (Map.Entry<String, Schema> entry : schemasMap.entrySet()) {
        String key = entry.getKey();
        Schema definition = entry.getValue();
        Optional<Schema> filteredDefinition = filter.filterSchema(definition, params, cookies, headers);
        if (filteredDefinition.isPresent()) {
            Map<String, Schema> clonedProperties = new LinkedHashMap<>();
            if (filteredDefinition.get().getProperties() != null) {
                for (Object propName : filteredDefinition.get().getProperties().keySet()) {
                    Schema property = (Schema) filteredDefinition.get().getProperties().get(propName);
                    if (property != null) {
                        Optional<Schema> filteredProperty = filter.filterSchemaProperty(property, definition, (String) propName, params, cookies, headers);
                        if (filteredProperty.isPresent()) {
                            clonedProperties.put((String) propName, filteredProperty.get());
                        }
                    }
                }
            }
            try {
                // TODO solve this, and generally handle clone and passing references
                Schema clonedModel = Json.mapper().readValue(Json.pretty(definition), Schema.class);
                if (clonedModel.getProperties() != null) {
                    clonedModel.getProperties().clear();
                }
                if (!clonedProperties.isEmpty()) {
                    clonedModel.setProperties(clonedProperties);
                }
                clonedComponentsSchema.put(key, clonedModel);
            } catch (IOException e) {
            }
        }
    }
    return clonedComponentsSchema;
}
Also used : ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) Schema(io.swagger.v3.oas.models.media.Schema) IOException(java.io.IOException) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap)

Example 15 with OpenAPISpecFilter

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

the class SpecFilter method filter.

public OpenAPI filter(OpenAPI openAPI, OpenAPISpecFilter filter, Map<String, List<String>> params, Map<String, String> cookies, Map<String, List<String>> headers) {
    OpenAPI filteredOpenAPI = filterOpenAPI(filter, openAPI, params, cookies, headers);
    if (filteredOpenAPI == null) {
        return filteredOpenAPI;
    }
    OpenAPI clone = new OpenAPI();
    clone.info(filteredOpenAPI.getInfo());
    clone.openapi(filteredOpenAPI.getOpenapi());
    clone.setExtensions(filteredOpenAPI.getExtensions());
    clone.setExternalDocs(filteredOpenAPI.getExternalDocs());
    clone.setSecurity(filteredOpenAPI.getSecurity());
    clone.setServers(filteredOpenAPI.getServers());
    clone.tags(filteredOpenAPI.getTags() == null ? null : new ArrayList<>(openAPI.getTags()));
    final Set<String> allowedTags = new HashSet<>();
    final Set<String> filteredTags = new HashSet<>();
    Paths clonedPaths = new Paths();
    if (filteredOpenAPI.getPaths() != null) {
        for (String resourcePath : filteredOpenAPI.getPaths().keySet()) {
            PathItem pathItem = filteredOpenAPI.getPaths().get(resourcePath);
            PathItem filteredPathItem = filterPathItem(filter, pathItem, resourcePath, params, cookies, headers);
            if (filteredPathItem != null) {
                PathItem clonedPathItem = new PathItem();
                clonedPathItem.set$ref(filteredPathItem.get$ref());
                clonedPathItem.setDescription(filteredPathItem.getDescription());
                clonedPathItem.setSummary(filteredPathItem.getSummary());
                clonedPathItem.setExtensions(filteredPathItem.getExtensions());
                clonedPathItem.setParameters(filteredPathItem.getParameters());
                clonedPathItem.setServers(filteredPathItem.getServers());
                Map<PathItem.HttpMethod, Operation> ops = filteredPathItem.readOperationsMap();
                for (Map.Entry<PathItem.HttpMethod, Operation> entry : ops.entrySet()) {
                    PathItem.HttpMethod key = entry.getKey();
                    Operation op = entry.getValue();
                    List<String> opTagsBeforeFilter = null;
                    if (op.getTags() != null) {
                        opTagsBeforeFilter = new ArrayList<>(op.getTags());
                    } else {
                        opTagsBeforeFilter = new ArrayList<>();
                    }
                    op = filterOperation(filter, op, resourcePath, key.toString(), params, cookies, headers);
                    clonedPathItem.operation(key, op);
                    if (op == null) {
                        filteredTags.addAll(opTagsBeforeFilter);
                    } else {
                        if (op.getTags() != null) {
                            opTagsBeforeFilter.removeAll(op.getTags());
                            allowedTags.addAll(op.getTags());
                        }
                        filteredTags.addAll(opTagsBeforeFilter);
                    }
                }
                if (!clonedPathItem.readOperations().isEmpty()) {
                    clonedPaths.addPathItem(resourcePath, clonedPathItem);
                }
            }
        }
        clone.paths(clonedPaths);
    }
    filteredTags.removeAll(allowedTags);
    final List<Tag> tags = clone.getTags();
    if (tags != null && !filteredTags.isEmpty()) {
        for (Iterator<Tag> it = tags.iterator(); it.hasNext(); ) {
            if (filteredTags.contains(it.next().getName())) {
                it.remove();
            }
        }
        if (clone.getTags().isEmpty()) {
            clone.setTags(null);
        }
    }
    if (filteredOpenAPI.getComponents() != null) {
        clone.components(new Components());
        clone.getComponents().setSchemas(filterComponentsSchema(filter, filteredOpenAPI.getComponents().getSchemas(), params, cookies, headers));
        clone.getComponents().setSecuritySchemes(filteredOpenAPI.getComponents().getSecuritySchemes());
        clone.getComponents().setCallbacks(filteredOpenAPI.getComponents().getCallbacks());
        clone.getComponents().setExamples(filteredOpenAPI.getComponents().getExamples());
        clone.getComponents().setExtensions(filteredOpenAPI.getComponents().getExtensions());
        clone.getComponents().setHeaders(filteredOpenAPI.getComponents().getHeaders());
        clone.getComponents().setLinks(filteredOpenAPI.getComponents().getLinks());
        clone.getComponents().setParameters(filteredOpenAPI.getComponents().getParameters());
        clone.getComponents().setRequestBodies(filteredOpenAPI.getComponents().getRequestBodies());
        clone.getComponents().setResponses(filteredOpenAPI.getComponents().getResponses());
    }
    if (filter.isRemovingUnreferencedDefinitions()) {
        clone = removeBrokenReferenceDefinitions(clone);
    }
    return clone;
}
Also used : ArrayList(java.util.ArrayList) Operation(io.swagger.v3.oas.models.Operation) Components(io.swagger.v3.oas.models.Components) PathItem(io.swagger.v3.oas.models.PathItem) Paths(io.swagger.v3.oas.models.Paths) Tag(io.swagger.v3.oas.models.tags.Tag) OpenAPI(io.swagger.v3.oas.models.OpenAPI) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Aggregations

OpenAPI (io.swagger.v3.oas.models.OpenAPI)9 OpenAPISpecFilter (io.swagger.v3.core.filter.OpenAPISpecFilter)8 SpecFilter (io.swagger.v3.core.filter.SpecFilter)8 ApiDescription (io.swagger.v3.core.model.ApiDescription)5 Components (io.swagger.v3.oas.models.Components)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 Info (io.swagger.v3.oas.models.info.Info)4 Test (org.testng.annotations.Test)4 OpenApiConfigurationException (io.swagger.v3.oas.integration.OpenApiConfigurationException)3 Parameter (io.swagger.v3.oas.models.parameters.Parameter)3 RequestBody (io.swagger.v3.oas.models.parameters.RequestBody)3 ApiResponse (io.swagger.v3.oas.models.responses.ApiResponse)3 IOException (java.io.IOException)3 JaxrsOpenApiContextBuilder (io.swagger.v3.jaxrs2.integration.JaxrsOpenApiContextBuilder)2 GenericOpenApiContextBuilder (io.swagger.v3.oas.integration.GenericOpenApiContextBuilder)2 SwaggerConfiguration (io.swagger.v3.oas.integration.SwaggerConfiguration)2 Operation (io.swagger.v3.oas.models.Operation)2 PathItem (io.swagger.v3.oas.models.PathItem)2