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();
}
}
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;
}
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;
}
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;
}
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;
}
Aggregations