use of io.swagger.v3.core.filter.OpenAPISpecFilter in project swagger-core by swagger-api.
the class SpecFilter method filterResponse.
protected ApiResponse filterResponse(OpenAPISpecFilter filter, Operation operation, ApiResponse response, String resourcePath, String key, Map<String, List<String>> params, Map<String, String> cookies, Map<String, List<String>> headers) {
if (response != null) {
ApiDescription description = new ApiDescription(resourcePath, key);
Optional<ApiResponse> filteredResponse = filter.filterResponse(response, operation, description, params, cookies, headers);
if (filteredResponse.isPresent()) {
return filteredResponse.get();
}
}
return null;
}
use of io.swagger.v3.core.filter.OpenAPISpecFilter in project swagger-core by swagger-api.
the class SpecFilter method filterRequestBody.
protected RequestBody filterRequestBody(OpenAPISpecFilter filter, Operation operation, RequestBody requestBody, String resourcePath, String key, Map<String, List<String>> params, Map<String, String> cookies, Map<String, List<String>> headers) {
if (requestBody != null) {
ApiDescription description = new ApiDescription(resourcePath, key);
Optional<RequestBody> filteredRequestBody = filter.filterRequestBody(requestBody, operation, description, params, cookies, headers);
if (filteredRequestBody.isPresent()) {
return filteredRequestBody.get();
}
}
return null;
}
use of io.swagger.v3.core.filter.OpenAPISpecFilter in project swagger-core by swagger-api.
the class SpecFilter method filterParameter.
protected Parameter filterParameter(OpenAPISpecFilter filter, Operation operation, Parameter parameter, String resourcePath, String key, Map<String, List<String>> params, Map<String, String> cookies, Map<String, List<String>> headers) {
if (parameter != null) {
ApiDescription description = new ApiDescription(resourcePath, key);
Optional<Parameter> filteredParameter = filter.filterParameter(parameter, operation, description, params, cookies, headers);
if (filteredParameter.isPresent()) {
return filteredParameter.get();
}
}
return null;
}
use of io.swagger.v3.core.filter.OpenAPISpecFilter in project swagger-core by swagger-api.
the class ReaderTest method testResponseWithFilter.
@Test(description = "Responses with filter")
public void testResponseWithFilter() {
Components components = new Components();
components.addResponses("invalidJWT", new ApiResponse().description("when JWT token invalid/expired"));
OpenAPI oas = new OpenAPI().info(new Info().description("info")).components(components);
Reader reader = new Reader(oas);
OpenAPI openAPI = reader.read(SimpleResponsesResource.class);
OpenAPISpecFilter filterImpl = new RefResponseFilter();
SpecFilter f = new SpecFilter();
openAPI = f.filter(openAPI, filterImpl, null, null, null);
String yaml = "openapi: 3.0.1\n" + "info:\n" + " description: info\n" + "paths:\n" + " /:\n" + " get:\n" + " summary: Simple get operation\n" + " description: Defines a simple get operation with no inputs and a complex output\n" + " object\n" + " operationId: getWithPayloadResponse\n" + " responses:\n" + " \"200\":\n" + " description: voila!\n" + " content:\n" + " application/json:\n" + " schema:\n" + " $ref: '#/components/schemas/SampleResponseSchema'\n" + " default:\n" + " description: boo\n" + " content:\n" + " '*/*':\n" + " schema:\n" + " $ref: '#/components/schemas/GenericError'\n" + " \"401\":\n" + " $ref: '#/components/responses/invalidJWT'\n" + " deprecated: true\n" + "components:\n" + " schemas:\n" + " GenericError:\n" + " type: object\n" + " SampleResponseSchema:\n" + " type: object\n" + " responses:\n" + " invalidJWT:\n" + " description: when JWT token invalid/expired";
SerializationMatchers.assertEqualsToYaml(openAPI, yaml);
}
use of io.swagger.v3.core.filter.OpenAPISpecFilter in project swagger-core by swagger-api.
the class OpenApiServlet method doGet.
// TODO move to own servlet non jaxrs project and reference from there
// TODO cleanup and errors
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String ctxId = getContextIdFromServletConfig(getServletConfig());
OpenApiContext ctx = OpenApiContextLocator.getInstance().getOpenApiContext(ctxId);
OpenAPI oas = ctx.read();
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, ServletUtils.getQueryParams(req.getParameterMap()), ServletUtils.getCookies(req.getCookies()), ServletUtils.getHeaders(req));
} catch (Exception e) {
LOGGER.error("failed to load filter", e);
}
}
}
String type = "json";
String acceptHeader = req.getHeader(ACCEPT_HEADER);
if (!StringUtils.isBlank(acceptHeader) && acceptHeader.toLowerCase().contains(APPLICATION_YAML)) {
type = "yaml";
} else {
// check URL:
if (req.getRequestURL().toString().toLowerCase().endsWith("yaml")) {
type = "yaml";
}
}
boolean pretty = false;
if (ctx.getOpenApiConfiguration() != null && Boolean.TRUE.equals(ctx.getOpenApiConfiguration().isPrettyPrint())) {
pretty = true;
}
resp.setStatus(200);
if (type.equalsIgnoreCase("yaml")) {
resp.setContentType(APPLICATION_YAML);
try (PrintWriter pw = resp.getWriter()) {
pw.write(pretty ? ctx.getOutputYamlMapper().writer(new DefaultPrettyPrinter()).writeValueAsString(oas) : ctx.getOutputYamlMapper().writeValueAsString(oas));
}
} else {
resp.setContentType(APPLICATION_JSON);
try (PrintWriter pw = resp.getWriter()) {
pw.write(pretty ? ctx.getOutputJsonMapper().writer(new DefaultPrettyPrinter()).writeValueAsString(oas) : ctx.getOutputJsonMapper().writeValueAsString(oas));
}
}
}
Aggregations