use of io.swagger.v3.core.filter.SpecFilter 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.SpecFilter 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));
}
}
}
use of io.swagger.v3.core.filter.SpecFilter in project swagger-core by swagger-api.
the class ReaderTest method testExampleWithFilter.
@Test(description = "Example with Ref Filter")
public void testExampleWithFilter() {
Components components = new Components();
components.addExamples("Id", new Example().description("Id Example").summary("Id Example").value("1"));
OpenAPI oas = new OpenAPI().info(new Info().description("info")).components(components);
Reader reader = new Reader(oas);
OpenAPI openAPI = reader.read(SimpleExamplesResource.class);
OpenAPISpecFilter filterImpl = new RefExampleFilter();
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" + " /example:\n" + " post:\n" + " description: subscribes a client to updates relevant to the requestor's account\n" + " operationId: subscribe\n" + " parameters:\n" + " - example:\n" + " $ref: '#/components/examples/Id'\n" + " requestBody:\n" + " content:\n" + " '*/*':\n" + " schema:\n" + " type: integer\n" + " format: int32\n" + " responses:\n" + " default:\n" + " description: default response\n" + " content:\n" + " '*/*':\n" + " schema:\n" + " $ref: '#/components/schemas/SubscriptionResponse'\n" + "components:\n" + " schemas:\n" + " SubscriptionResponse:\n" + " type: object\n" + " properties:\n" + " subscriptionId:\n" + " type: string\n" + " examples:\n" + " Id:\n" + " summary: Id Example\n" + " description: Id Example\n" + " value: \"1\"\n";
SerializationMatchers.assertEqualsToYaml(openAPI, yaml);
}
use of io.swagger.v3.core.filter.SpecFilter in project swagger-core by swagger-api.
the class ReaderTest method testParameterWithFilter.
@Test(description = "Responses with filter")
public void testParameterWithFilter() {
Components components = new Components();
components.addParameters("id", new Parameter().description("Id Description").schema(new IntegerSchema()).in(ParameterIn.QUERY.toString()).example(1).required(true));
OpenAPI oas = new OpenAPI().info(new Info().description("info")).components(components);
Reader reader = new Reader(oas);
OpenAPI openAPI = reader.read(SimpleParameterResource.class);
OpenAPISpecFilter filterImpl = new RefParameterFilter();
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 a payload complex input object\n" + " operationId: sendPayload\n" + " parameters:\n" + " - $ref: '#/components/parameters/id'\n" + " responses:\n" + " default:\n" + " description: default response\n" + " content:\n" + " '*/*': {}\n" + " deprecated: true\n" + "components:\n" + " parameters: \n" + " id:\n" + " in: query\n" + " description: Id Description\n" + " required: true\n" + " schema:\n" + " type: integer\n" + " format: int32\n" + " example: 1\n";
SerializationMatchers.assertEqualsToYaml(openAPI, yaml);
}
use of io.swagger.v3.core.filter.SpecFilter in project swagger-core by swagger-api.
the class ReaderTest method testRequestBodyWithFilter.
@Test(description = "RequestBody with filter")
public void testRequestBodyWithFilter() {
Components components = new Components();
components.addRequestBodies("User", new RequestBody());
OpenAPI oas = new OpenAPI().info(new Info().description("info")).components(components);
Reader reader = new Reader(oas);
OpenAPI openAPI = reader.read(SimpleRequestBodyResource.class);
OpenAPISpecFilter filterImpl = new RefRequestBodyFilter();
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 a payload complex input object\n" + " operationId: sendPayload\n" + " requestBody:\n" + " $ref: '#/components/requestBodies/User'\n" + " responses:\n" + " default:\n" + " description: default response\n" + " content:\n" + " '*/*': {}\n" + " deprecated: true\n" + "components:\n" + " schemas:\n" + " User:\n" + " type: object\n" + " properties:\n" + " id:\n" + " type: integer\n" + " format: int64\n" + " username:\n" + " type: string\n" + " firstName:\n" + " type: string\n" + " lastName:\n" + " type: string\n" + " email:\n" + " type: string\n" + " password:\n" + " type: string\n" + " phone:\n" + " type: string\n" + " userStatus:\n" + " type: integer\n" + " description: User Status\n" + " format: int32\n" + " xml:\n" + " name: User\n" + " requestBodies:\n" + " User: {}\n";
SerializationMatchers.assertEqualsToYaml(openAPI, yaml);
}
Aggregations