use of io.swagger.v3.oas.models.links.Link in project swagger-core by swagger-api.
the class OpenAPI3_1SerializationTest method testComponentPathItemsSerialization.
@Test
public void testComponentPathItemsSerialization() {
Schema schema = new StringSchema();
schema.addType(schema.getType());
OpenAPI openAPI = new OpenAPI().openapi("3.1.0").components(new Components().addSchemas("stringTest", schema).addPathItem("/pathTest", new PathItem().description("test path item").get(new Operation().operationId("testPathItem").responses(new ApiResponses().addApiResponse("200", new ApiResponse().description("response description"))))).addResponses("201", new ApiResponse().description("api response description")).addParameters("param", new Parameter().in("query").description("parameter description").schema(schema)).addExamples("example", new Example().summary("example summary").value("This is an example/").description("example description")).addRequestBodies("body", new RequestBody().content(new Content().addMediaType("application/json", new MediaType().schema(new ObjectSchema())))).addHeaders("test-head", new Header().description("test header description")).addSecuritySchemes("basic", new SecurityScheme().in(SecurityScheme.In.HEADER).scheme("http").description("ref security description")).addLinks("Link", new Link().operationRef("#/paths/~12.0~1repositories~1{username}/get")).addCallbacks("TestCallback", new Callback().addPathItem("{$request.query.queryUrl}", new PathItem().description("test path item").post(new Operation().operationId("testPathItem")))));
SerializationMatchers.assertEqualsToYaml31(openAPI, "openapi: 3.1.0\n" + "components:\n" + " schemas:\n" + " stringTest:\n" + " type: string\n" + " responses:\n" + " \"201\":\n" + " description: api response description\n" + " parameters:\n" + " param:\n" + " in: query\n" + " description: parameter description\n" + " schema:\n" + " type: string\n" + " examples:\n" + " example:\n" + " summary: example summary\n" + " description: example description\n" + " value: This is an example/\n" + " requestBodies:\n" + " body:\n" + " content:\n" + " application/json:\n" + " schema: {}\n" + " headers:\n" + " test-head:\n" + " description: test header description\n" + " securitySchemes:\n" + " basic:\n" + " description: ref security description\n" + " in: header\n" + " scheme: http\n" + " links:\n" + " Link:\n" + " operationRef: \"#/paths/~12.0~1repositories~1{username}/get\"\n" + " callbacks:\n" + " TestCallback:\n" + " '{$request.query.queryUrl}':\n" + " description: test path item\n" + " post:\n" + " operationId: testPathItem\n" + " pathItems:\n" + " /pathTest:\n" + " description: test path item\n" + " get:\n" + " operationId: testPathItem\n" + " responses:\n" + " \"200\":\n" + " description: response description");
SerializationMatchers.assertEqualsToJson31(openAPI, "{\n" + " \"openapi\" : \"3.1.0\",\n" + " \"components\" : {\n" + " \"schemas\" : {\n" + " \"stringTest\" : {\n" + " \"type\" : \"string\"\n" + " }\n" + " },\n" + " \"responses\" : {\n" + " \"201\" : {\n" + " \"description\" : \"api response description\"\n" + " }\n" + " },\n" + " \"parameters\" : {\n" + " \"param\" : {\n" + " \"in\" : \"query\",\n" + " \"description\" : \"parameter description\",\n" + " \"schema\" : {\n" + " \"type\" : \"string\"\n" + " }\n" + " }\n" + " },\n" + " \"examples\" : {\n" + " \"example\" : {\n" + " \"summary\" : \"example summary\",\n" + " \"description\" : \"example description\",\n" + " \"value\" : \"This is an example/\"\n" + " }\n" + " },\n" + " \"requestBodies\" : {\n" + " \"body\" : {\n" + " \"content\" : {\n" + " \"application/json\" : {\n" + " \"schema\" : { }\n" + " }\n" + " }\n" + " }\n" + " },\n" + " \"headers\" : {\n" + " \"test-head\" : {\n" + " \"description\" : \"test header description\"\n" + " }\n" + " },\n" + " \"securitySchemes\" : {\n" + " \"basic\" : {\n" + " \"description\" : \"ref security description\",\n" + " \"in\" : \"header\",\n" + " \"scheme\" : \"http\"\n" + " }\n" + " },\n" + " \"links\" : {\n" + " \"Link\" : {\n" + " \"operationRef\" : \"#/paths/~12.0~1repositories~1{username}/get\"\n" + " }\n" + " },\n" + " \"callbacks\" : {\n" + " \"TestCallback\" : {\n" + " \"{$request.query.queryUrl}\" : {\n" + " \"description\" : \"test path item\",\n" + " \"post\" : {\n" + " \"operationId\" : \"testPathItem\"\n" + " }\n" + " }\n" + " }\n" + " },\n" + " \"pathItems\" : {\n" + " \"/pathTest\" : {\n" + " \"description\" : \"test path item\",\n" + " \"get\" : {\n" + " \"operationId\" : \"testPathItem\",\n" + " \"responses\" : {\n" + " \"200\" : {\n" + " \"description\" : \"response description\"\n" + " }\n" + " }\n" + " }\n" + " }\n" + " }\n" + " }\n" + "}");
openAPI.openapi("3.0.3");
SerializationMatchers.assertEqualsToYaml(openAPI, "openapi: 3.0.3\n" + "components:\n" + " schemas:\n" + " stringTest:\n" + " type: string\n" + " responses:\n" + " \"201\":\n" + " description: api response description\n" + " parameters:\n" + " param:\n" + " in: query\n" + " description: parameter description\n" + " schema:\n" + " type: string\n" + " examples:\n" + " example:\n" + " summary: example summary\n" + " description: example description\n" + " value: This is an example/\n" + " requestBodies:\n" + " body:\n" + " content:\n" + " application/json:\n" + " schema:\n" + " type: object\n" + " headers:\n" + " test-head:\n" + " description: test header description\n" + " securitySchemes:\n" + " basic:\n" + " description: ref security description\n" + " in: header\n" + " scheme: http\n" + " links:\n" + " Link:\n" + " operationRef: \"#/paths/~12.0~1repositories~1{username}/get\"\n" + " callbacks:\n" + " TestCallback:\n" + " '{$request.query.queryUrl}':\n" + " description: test path item\n" + " post:\n" + " operationId: testPathItem");
SerializationMatchers.assertEqualsToJson(openAPI, "{\n" + " \"openapi\" : \"3.0.3\",\n" + " \"components\" : {\n" + " \"schemas\" : {\n" + " \"stringTest\" : {\n" + " \"type\" : \"string\"\n" + " }\n" + " },\n" + " \"responses\" : {\n" + " \"201\" : {\n" + " \"description\" : \"api response description\"\n" + " }\n" + " },\n" + " \"parameters\" : {\n" + " \"param\" : {\n" + " \"in\" : \"query\",\n" + " \"description\" : \"parameter description\",\n" + " \"schema\" : {\n" + " \"type\" : \"string\"\n" + " }\n" + " }\n" + " },\n" + " \"examples\" : {\n" + " \"example\" : {\n" + " \"summary\" : \"example summary\",\n" + " \"description\" : \"example description\",\n" + " \"value\" : \"This is an example/\"\n" + " }\n" + " },\n" + " \"requestBodies\" : {\n" + " \"body\" : {\n" + " \"content\" : {\n" + " \"application/json\" : {\n" + " \"schema\" : {\n" + " \"type\" : \"object\"\n" + " }\n" + " }\n" + " }\n" + " }\n" + " },\n" + " \"headers\" : {\n" + " \"test-head\" : {\n" + " \"description\" : \"test header description\"\n" + " }\n" + " },\n" + " \"securitySchemes\" : {\n" + " \"basic\" : {\n" + " \"description\" : \"ref security description\",\n" + " \"in\" : \"header\",\n" + " \"scheme\" : \"http\"\n" + " }\n" + " },\n" + " \"links\" : {\n" + " \"Link\" : {\n" + " \"operationRef\" : \"#/paths/~12.0~1repositories~1{username}/get\"\n" + " }\n" + " },\n" + " \"callbacks\" : {\n" + " \"TestCallback\" : {\n" + " \"{$request.query.queryUrl}\" : {\n" + " \"description\" : \"test path item\",\n" + " \"post\" : {\n" + " \"operationId\" : \"testPathItem\"\n" + " }\n" + " }\n" + " }\n" + " }\n" + " }\n" + "}");
}
use of io.swagger.v3.oas.models.links.Link in project swagger-core by swagger-api.
the class ReaderTest method testLinkWithRef.
@Test(description = "Link with Ref")
public void testLinkWithRef() {
Components components = new Components();
components.addLinks("Link", new Link().description("Link Description").operationId("id"));
OpenAPI oas = new OpenAPI().info(new Info().description("info")).components(components);
Reader reader = new Reader(oas);
OpenAPI openAPI = reader.read(RefLinksResource.class);
String yaml = "openapi: 3.0.1\n" + "info:\n" + " description: info\n" + "paths:\n" + " /links:\n" + " get:\n" + " operationId: getUserWithAddress\n" + " parameters:\n" + " - name: userId\n" + " in: query\n" + " schema:\n" + " type: string\n" + " responses:\n" + " default:\n" + " description: test description\n" + " content:\n" + " '*/*':\n" + " schema:\n" + " $ref: '#/components/schemas/User'\n" + " links:\n" + " address:\n" + " operationId: getAddress\n" + " parameters:\n" + " userId: $request.query.userId\n" + " $ref: '#/components/links/Link'\n" + "components:\n" + " links:\n" + " Link:\n" + " operationId: id\n" + " description: Link Description\n";
SerializationMatchers.assertEqualsToYaml(openAPI, yaml);
}
use of io.swagger.v3.oas.models.links.Link in project swagger-core by swagger-api.
the class OperationParser method getApiResponses.
public static Optional<ApiResponses> getApiResponses(final io.swagger.v3.oas.annotations.responses.ApiResponse[] responses, Produces classProduces, Produces methodProduces, Components components, JsonView jsonViewAnnotation) {
if (responses == null) {
return Optional.empty();
}
ApiResponses apiResponsesObject = new ApiResponses();
for (io.swagger.v3.oas.annotations.responses.ApiResponse response : responses) {
ApiResponse apiResponseObject = new ApiResponse();
if (StringUtils.isNotBlank(response.ref())) {
apiResponseObject.set$ref(response.ref());
if (StringUtils.isNotBlank(response.responseCode())) {
apiResponsesObject.addApiResponse(response.responseCode(), apiResponseObject);
} else {
apiResponsesObject._default(apiResponseObject);
}
continue;
}
if (StringUtils.isNotBlank(response.description())) {
apiResponseObject.setDescription(response.description());
}
if (response.extensions().length > 0) {
Map<String, Object> extensions = AnnotationsUtils.getExtensions(response.extensions());
if (extensions != null) {
extensions.forEach(apiResponseObject::addExtension);
}
}
AnnotationsUtils.getContent(response.content(), classProduces == null ? new String[0] : classProduces.value(), methodProduces == null ? new String[0] : methodProduces.value(), null, components, jsonViewAnnotation).ifPresent(apiResponseObject::content);
AnnotationsUtils.getHeaders(response.headers(), jsonViewAnnotation).ifPresent(apiResponseObject::headers);
if (StringUtils.isNotBlank(apiResponseObject.getDescription()) || apiResponseObject.getContent() != null || apiResponseObject.getHeaders() != null) {
Map<String, Link> links = AnnotationsUtils.getLinks(response.links());
if (links.size() > 0) {
apiResponseObject.setLinks(links);
}
if (StringUtils.isNotBlank(response.responseCode())) {
apiResponsesObject.addApiResponse(response.responseCode(), apiResponseObject);
} else {
apiResponsesObject._default(apiResponseObject);
}
}
}
if (apiResponsesObject.isEmpty()) {
return Optional.empty();
}
return Optional.of(apiResponsesObject);
}
use of io.swagger.v3.oas.models.links.Link in project swagger-core by swagger-api.
the class AnnotationsUtils method getLink.
public static Optional<Link> getLink(io.swagger.v3.oas.annotations.links.Link link) {
if (link == null) {
return Optional.empty();
}
boolean isEmpty = true;
Link linkObject = new Link();
if (StringUtils.isNotBlank(link.description())) {
linkObject.setDescription(link.description());
isEmpty = false;
}
if (StringUtils.isNotBlank(link.operationId())) {
linkObject.setOperationId(link.operationId());
isEmpty = false;
if (StringUtils.isNotBlank(link.operationRef())) {
LOGGER.debug("OperationId and OperatonRef are mutually exclusive, there must be only one setted");
}
} else {
if (StringUtils.isNotBlank(link.operationRef())) {
linkObject.setOperationRef(link.operationRef());
isEmpty = false;
}
}
if (StringUtils.isNotBlank(link.ref())) {
linkObject.set$ref(link.ref());
isEmpty = false;
}
if (link.extensions() != null && link.extensions().length > 0) {
Map<String, Object> extensions = AnnotationsUtils.getExtensions(link.extensions());
if (extensions != null) {
extensions.forEach(linkObject::addExtension);
isEmpty = false;
}
}
if (isEmpty) {
return Optional.empty();
}
Map<String, String> linkParameters = getLinkParameters(link.parameters());
if (linkParameters.size() > 0) {
linkObject.setParameters(linkParameters);
}
if (StringUtils.isNotBlank(link.requestBody())) {
JsonNode processedValue = null;
try {
processedValue = Json.mapper().readTree(link.requestBody());
} catch (Exception e) {
// not a json string
}
if (processedValue == null) {
linkObject.requestBody(link.requestBody());
} else {
linkObject.requestBody(processedValue);
}
}
return Optional.of(linkObject);
}
Aggregations