Search in sources :

Example 1 with Link

use of io.swagger.v3.oas.models.links.Link in project swagger-parser by swagger-api.

the class OpenAPIDeserializer method getLink.

public Link getLink(ObjectNode linkNode, String location, ParseResult result) {
    if (linkNode == null) {
        return null;
    }
    Link link = new Link();
    JsonNode ref = linkNode.get("$ref");
    if (ref != null) {
        if (ref.getNodeType().equals(JsonNodeType.STRING)) {
            String mungedRef = mungedRef(ref.textValue());
            if (mungedRef != null) {
                link.set$ref(mungedRef);
            } else {
                link.set$ref(ref.textValue());
            }
            return link;
        } else {
            result.invalidType(location, "$ref", "string", linkNode);
            return null;
        }
    }
    String value = getString("operationRef", linkNode, false, location, result);
    if ((result.isAllowEmptyStrings() && value != null) || (!result.isAllowEmptyStrings() && !StringUtils.isBlank(value))) {
        link.setOperationRef(value);
    }
    value = getString("operationId", linkNode, false, location, result);
    if ((result.isAllowEmptyStrings() && value != null) || (!result.isAllowEmptyStrings() && !StringUtils.isBlank(value))) {
        link.setOperationId(value);
    }
    ObjectNode parametersObject = getObject("parameters", linkNode, false, location, result);
    if (parametersObject != null) {
        link.setParameters(getLinkParameters(parametersObject, location, result));
    }
    String requestBody = getString("requestBody", linkNode, false, location, result);
    if (result.isAllowEmptyStrings() && requestBody != null) {
        link.setRequestBody(requestBody);
    }
    ObjectNode headerObject = getObject("headers", linkNode, false, location, result);
    if (headerObject != null) {
        link.setHeaders(getHeaders(headerObject, location, result, false));
    }
    ObjectNode serverObject = getObject("server", linkNode, false, location, result);
    if (serverObject != null) {
        link.setServer(getServer(serverObject, location, result));
    }
    value = getString("description", linkNode, false, location, result);
    if ((result.isAllowEmptyStrings() && value != null) || (!result.isAllowEmptyStrings() && !StringUtils.isBlank(value))) {
        link.setDescription(value);
    }
    Map<String, Object> extensions = getExtensions(linkNode);
    if (extensions != null && extensions.size() > 0) {
        link.setExtensions(extensions);
    }
    Set<String> keys = getKeys(linkNode);
    for (String key : keys) {
        if (!LINK_KEYS.contains(key) && !key.startsWith("x-")) {
            result.extra(location, key, linkNode.get(key));
        }
    }
    return link;
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) JsonNode(com.fasterxml.jackson.databind.JsonNode) Link(io.swagger.v3.oas.models.links.Link)

Example 2 with Link

use of io.swagger.v3.oas.models.links.Link in project swagger-parser by swagger-api.

the class OpenAPIV3ParserTest method testLinkIssue.

@Test
public void testLinkIssue() {
    ParseOptions parseOptions = new ParseOptions();
    parseOptions.setResolveFully(true);
    OpenAPI openAPI = new OpenAPIV3Parser().read("src/test/resources/linkIssue.yaml", null, parseOptions);
    Map<String, Link> links = openAPI.getPaths().get("/2.0/repositories/{username}").getGet().getResponses().get("200").getLinks();
    Object requestBody = links.get("userRepository").getRequestBody();
    assertEquals(requestBody, "$response.body#/slug");
}
Also used : ParseOptions(io.swagger.v3.parser.core.models.ParseOptions) OpenAPIV3Parser(io.swagger.v3.parser.OpenAPIV3Parser) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Link(io.swagger.v3.oas.models.links.Link) Test(org.testng.annotations.Test)

Example 3 with Link

use of io.swagger.v3.oas.models.links.Link in project swagger-parser by swagger-api.

the class OpenAPIV3ParserTest method testRefAdditionalProperties.

@Test
public void testRefAdditionalProperties() throws Exception {
    OpenAPI openAPI = new OpenAPIV3Parser().read("src/test/resources/relative/additionalProperties.yaml");
    Assert.assertNotNull(openAPI);
    Assert.assertTrue(openAPI.getComponents().getSchemas().size() == 3);
    Assert.assertNotNull(openAPI.getComponents().getSchemas().get("link-object"));
    Assert.assertNotNull(openAPI.getComponents().getSchemas().get("rel-data"));
    Assert.assertNotNull(openAPI.getComponents().getSchemas().get("result"));
}
Also used : OpenAPIV3Parser(io.swagger.v3.parser.OpenAPIV3Parser) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.testng.annotations.Test)

Example 4 with Link

use of io.swagger.v3.oas.models.links.Link in project swagger-parser by swagger-api.

the class OpenAPIDeserializer method getResponse.

public ApiResponse getResponse(ObjectNode node, String location, ParseResult result) {
    if (node == null) {
        return null;
    }
    ApiResponse apiResponse = new ApiResponse();
    JsonNode ref = node.get("$ref");
    if (ref != null) {
        if (ref.getNodeType().equals(JsonNodeType.STRING)) {
            String mungedRef = mungedRef(ref.textValue());
            if (mungedRef != null) {
                apiResponse.set$ref(mungedRef);
            } else {
                apiResponse.set$ref(ref.textValue());
            }
            return apiResponse;
        } else {
            result.invalidType(location, "$ref", "string", node);
            return null;
        }
    }
    String value = getString("description", node, true, location, result);
    if ((result.isAllowEmptyStrings() && value != null) || (!result.isAllowEmptyStrings() && !StringUtils.isBlank(value))) {
        apiResponse.description(value);
    }
    ObjectNode headerObject = getObject("headers", node, false, location, result);
    if (headerObject != null) {
        Map<String, Header> headers = getHeaders(headerObject, location, result, false);
        if (headers != null && headers.size() > 0) {
            apiResponse.setHeaders(headers);
        }
    }
    ObjectNode linksObj = getObject("links", node, false, location, result);
    if (linksObj != null) {
        Map<String, Link> links = getLinks(linksObj, location, result, false);
        if (links != null && links.size() > 0) {
            apiResponse.setLinks(links);
        }
    }
    ObjectNode contentObject = getObject("content", node, false, location, result);
    if (contentObject != null) {
        apiResponse.setContent(getContent(contentObject, String.format("%s.%s", location, "content"), result));
    }
    Map<String, Object> extensions = getExtensions(node);
    if (extensions != null && extensions.size() > 0) {
        apiResponse.setExtensions(extensions);
    }
    Set<String> keys = getKeys(node);
    for (String key : keys) {
        if (!RESPONSE_KEYS.contains(key) && !key.startsWith("x-")) {
            result.extra(location, key, node.get(key));
        }
    }
    return apiResponse;
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Header(io.swagger.v3.oas.models.headers.Header) JsonNode(com.fasterxml.jackson.databind.JsonNode) ApiResponse(io.swagger.v3.oas.models.responses.ApiResponse) Link(io.swagger.v3.oas.models.links.Link)

Example 5 with Link

use of io.swagger.v3.oas.models.links.Link in project swagger-parser by swagger-api.

the class OpenAPIDeserializer method getLinks.

public Map<String, Link> getLinks(ObjectNode obj, String location, ParseResult result, boolean underComponents) {
    if (obj == null) {
        return null;
    }
    Map<String, Link> links = new LinkedHashMap<>();
    Set<String> linkKeys = getKeys(obj);
    for (String linkName : linkKeys) {
        if (underComponents) {
            if (!Pattern.matches("^[a-zA-Z0-9\\.\\-_]+$", linkName)) {
                result.warning(location, "Link name " + linkName + " doesn't adhere to regular expression " + "^[a-zA-Z0-9\\.\\-_]+$");
            }
        }
        JsonNode linkValue = obj.get(linkName);
        if (!linkValue.getNodeType().equals(JsonNodeType.OBJECT)) {
            result.invalidType(location, linkName, "object", linkValue);
        } else {
            ObjectNode link = (ObjectNode) linkValue;
            Link linkObj = getLink(link, String.format("%s.%s", location, linkName), result);
            if (linkObj != null) {
                links.put(linkName, linkObj);
            }
        }
    }
    return links;
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) JsonNode(com.fasterxml.jackson.databind.JsonNode) Link(io.swagger.v3.oas.models.links.Link)

Aggregations

Link (io.swagger.v3.oas.models.links.Link)18 OpenAPI (io.swagger.v3.oas.models.OpenAPI)14 Test (org.testng.annotations.Test)14 Operation (io.swagger.v3.oas.annotations.Operation)12 ApiResponses (io.swagger.v3.oas.annotations.responses.ApiResponses)12 ApiResponse (io.swagger.v3.oas.models.responses.ApiResponse)8 Schema (io.swagger.v3.oas.models.media.Schema)7 Path (javax.ws.rs.Path)7 Produces (javax.ws.rs.Produces)7 Components (io.swagger.v3.oas.models.Components)6 MediaType (io.swagger.v3.oas.models.media.MediaType)6 ApiResponses (io.swagger.v3.oas.models.responses.ApiResponses)6 RefFormat (io.swagger.v3.parser.models.RefFormat)6 JsonNode (com.fasterxml.jackson.databind.JsonNode)5 RequestBody (io.swagger.v3.oas.annotations.parameters.RequestBody)5 Operation (io.swagger.v3.oas.models.Operation)5 PathItem (io.swagger.v3.oas.models.PathItem)5 Example (io.swagger.v3.oas.models.examples.Example)5 StringSchema (io.swagger.v3.oas.models.media.StringSchema)5 Parameter (io.swagger.v3.oas.models.parameters.Parameter)5