Search in sources :

Example 1 with Content

use of io.swagger.v3.oas.annotations.media.Content 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 2 with Content

use of io.swagger.v3.oas.annotations.media.Content in project swagger-parser by swagger-api.

the class OpenAPIDeserializer method getRequestBody.

public RequestBody getRequestBody(ObjectNode node, String location, ParseResult result) {
    if (node == null) {
        return null;
    }
    final RequestBody body = new RequestBody();
    JsonNode ref = node.get("$ref");
    if (ref != null) {
        if (ref.getNodeType().equals(JsonNodeType.STRING)) {
            String mungedRef = mungedRef(ref.textValue());
            if (mungedRef != null) {
                body.set$ref(mungedRef);
            } else {
                body.set$ref(ref.textValue());
            }
            return body;
        } else {
            result.invalidType(location, "$ref", "string", node);
            return null;
        }
    }
    final String description = getString("description", node, false, location, result);
    if (result.isAllowEmptyStrings() && description != null) {
        body.setDescription(description);
    }
    final Boolean required = getBoolean("required", node, false, location, result);
    if (required != null) {
        body.setRequired(required);
    }
    final ObjectNode contentNode = getObject("content", node, true, location, result);
    Content content = getContent(contentNode, location + ".content", result);
    if (content != null && content.isEmpty()) {
        result.unsupported(location, "content with no media type", contentNode);
        result.invalid();
    } else {
        body.setContent(content);
    }
    Map<String, Object> extensions = getExtensions(node);
    if (extensions != null && extensions.size() > 0) {
        body.setExtensions(extensions);
    }
    Set<String> keys = getKeys(node);
    for (String key : keys) {
        if (!REQUEST_BODY_KEYS.contains(key) && !key.startsWith("x-")) {
            result.extra(location, key, node.get(key));
        }
    }
    return body;
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Content(io.swagger.v3.oas.models.media.Content) JsonNode(com.fasterxml.jackson.databind.JsonNode) RequestBody(io.swagger.v3.oas.models.parameters.RequestBody)

Example 3 with Content

use of io.swagger.v3.oas.annotations.media.Content in project swagger-parser by swagger-api.

the class OpenAPIDeserializer method getParameter.

public Parameter getParameter(ObjectNode obj, String location, ParseResult result) {
    if (obj == null) {
        return null;
    }
    Parameter parameter = null;
    JsonNode ref = obj.get("$ref");
    if (ref != null) {
        if (ref.getNodeType().equals(JsonNodeType.STRING)) {
            parameter = new Parameter();
            String mungedRef = mungedRef(ref.textValue());
            if (mungedRef != null) {
                parameter.set$ref(mungedRef);
            } else {
                parameter.set$ref(ref.textValue());
            }
            return parameter;
        } else {
            result.invalidType(location, "$ref", "string", obj);
            return null;
        }
    }
    String l = null;
    JsonNode ln = obj.get("name");
    if (ln != null) {
        l = ln.asText();
    } else {
        l = "['unknown']";
    }
    location += ".[" + l + "]";
    String value = getString("in", obj, true, location, result);
    if (!result.isAllowEmptyStrings() && StringUtils.isBlank(value) || result.isAllowEmptyStrings() && value == null) {
        return null;
    }
    if (QUERY_PARAMETER.equals(value)) {
        parameter = new QueryParameter();
    } else if (HEADER_PARAMETER.equals(value)) {
        parameter = new HeaderParameter();
    } else if (PATH_PARAMETER.equals(value)) {
        parameter = new PathParameter();
    } else if (COOKIE_PARAMETER.equals(value)) {
        parameter = new CookieParameter();
    }
    if (parameter == null) {
        result.invalidType(location, "in", "[query|header|path|cookie]", obj);
        return null;
    }
    parameter.setIn(value);
    value = getString("name", obj, true, location, result);
    if ((result.isAllowEmptyStrings() && value != null) || (!result.isAllowEmptyStrings() && !StringUtils.isBlank(value))) {
        parameter.setName(value);
    }
    value = getString("description", obj, false, location, result);
    if ((result.isAllowEmptyStrings() && value != null) || (!result.isAllowEmptyStrings() && !StringUtils.isBlank(value))) {
        parameter.setDescription(value);
    }
    Boolean required = getBoolean("required", obj, false, location, result);
    if (required != null) {
        parameter.setRequired(required);
    } else {
        parameter.setRequired(false);
    }
    Boolean deprecated = getBoolean("deprecated", obj, false, location, result);
    if (deprecated != null) {
        parameter.setDeprecated(deprecated);
    }
    if (parameter instanceof QueryParameter) {
        Boolean allowEmptyValue = getBoolean("allowEmptyValue", obj, false, location, result);
        if (allowEmptyValue != null) {
            parameter.setAllowEmptyValue(allowEmptyValue);
        }
    }
    value = getString("style", obj, false, location, result);
    setStyle(value, parameter, location, obj, result);
    Boolean explode = getBoolean("explode", obj, false, location, result);
    if (explode != null) {
        parameter.setExplode(explode);
    } else if (StyleEnum.FORM.equals(parameter.getStyle())) {
        parameter.setExplode(Boolean.TRUE);
    } else {
        parameter.setExplode(Boolean.FALSE);
    }
    ObjectNode parameterObject = getObject("schema", obj, false, location, result);
    if (parameterObject != null) {
        parameter.setSchema(getSchema(parameterObject, String.format("%s.%s", location, "schemas"), result));
    }
    ObjectNode examplesObject = getObject("examples", obj, false, location, result);
    if (examplesObject != null) {
        parameter.setExamples(getExamples(examplesObject, String.format("%s.%s", location, "examples"), result, false));
    }
    Object example = getAnyExample("example", obj, location, result);
    if (example != null) {
        if (examplesObject != null) {
            result.warning(location, "examples already defined -- ignoring \"example\" field");
        } else {
            parameter.setExample(example instanceof NullNode ? null : example);
        }
    }
    Boolean allowReserved = getBoolean("allowReserved", obj, false, location, result);
    if (allowReserved != null) {
        parameter.setAllowReserved(allowReserved);
    }
    ObjectNode contentNode = getObject("content", obj, false, location, result);
    if (contentNode != null) {
        Content content = getContent(contentNode, String.format("%s.%s", location, "content"), result);
        if (content.size() == 0) {
            result.unsupported(location, "content with no media type", contentNode);
            result.invalid();
        } else if (content.size() > 1) {
            result.unsupported(location, "content with multiple media types", contentNode);
            result.invalid();
        } else if (parameter.getSchema() != null) {
            result.unsupported(location, "content when schema defined", contentNode);
            result.invalid();
        } else {
            parameter.setContent(content);
        }
    } else if (parameter.getSchema() == null) {
        result.missing(location, "content");
    }
    Map<String, Object> extensions = getExtensions(obj);
    if (extensions != null && extensions.size() > 0) {
        parameter.setExtensions(extensions);
    }
    Set<String> keys = getKeys(obj);
    for (String key : keys) {
        if (!PARAMETER_KEYS.contains(key) && !key.startsWith("x-")) {
            result.extra(location, key, obj.get(key));
        }
    }
    return parameter;
}
Also used : QueryParameter(io.swagger.v3.oas.models.parameters.QueryParameter) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) JsonNode(com.fasterxml.jackson.databind.JsonNode) PathParameter(io.swagger.v3.oas.models.parameters.PathParameter) Content(io.swagger.v3.oas.models.media.Content) Parameter(io.swagger.v3.oas.models.parameters.Parameter) CookieParameter(io.swagger.v3.oas.models.parameters.CookieParameter) QueryParameter(io.swagger.v3.oas.models.parameters.QueryParameter) HeaderParameter(io.swagger.v3.oas.models.parameters.HeaderParameter) PathParameter(io.swagger.v3.oas.models.parameters.PathParameter) CookieParameter(io.swagger.v3.oas.models.parameters.CookieParameter) HeaderParameter(io.swagger.v3.oas.models.parameters.HeaderParameter) NullNode(com.fasterxml.jackson.databind.node.NullNode)

Example 4 with Content

use of io.swagger.v3.oas.annotations.media.Content in project swagger-parser by swagger-api.

the class ResolverFully method resolvePath.

public void resolvePath(PathItem pathItem) {
    for (Operation op : pathItem.readOperations()) {
        // inputs
        if (op.getParameters() != null) {
            for (Parameter parameter : op.getParameters()) {
                parameter = parameter.get$ref() != null ? resolveParameter(parameter) : parameter;
                if (parameter.getSchema() != null) {
                    Schema resolved = resolveSchema(parameter.getSchema());
                    if (resolved != null) {
                        parameter.setSchema(resolved);
                    }
                }
                if (parameter.getContent() != null) {
                    Map<String, MediaType> content = parameter.getContent();
                    for (String key : content.keySet()) {
                        if (content.get(key) != null && content.get(key).getSchema() != null) {
                            Schema resolvedSchema = resolveSchema(content.get(key).getSchema());
                            if (resolvedSchema != null) {
                                content.get(key).setSchema(resolvedSchema);
                            }
                        }
                    }
                }
            }
        }
        if (op.getCallbacks() != null) {
            Map<String, Callback> callbacks = op.getCallbacks();
            for (String name : callbacks.keySet()) {
                Callback callback = callbacks.get(name);
                callback = callback.get$ref() != null ? resolveCallback(callback) : callback;
                if (callback != null) {
                    for (String callbackName : callback.keySet()) {
                        PathItem path = callback.get(callbackName);
                        if (path != null) {
                            resolvePath(path);
                        }
                    }
                }
                op.getCallbacks().put(name, callback);
            }
        }
        RequestBody refRequestBody = op.getRequestBody();
        if (refRequestBody != null) {
            RequestBody requestBody = refRequestBody.get$ref() != null ? resolveRequestBody(refRequestBody) : refRequestBody;
            op.setRequestBody(requestBody);
            if (requestBody.getContent() != null) {
                Map<String, MediaType> content = requestBody.getContent();
                for (String key : content.keySet()) {
                    if (content.get(key) != null && content.get(key).getSchema() != null) {
                        Schema resolved = resolveSchema(content.get(key).getSchema());
                        if (resolved != null) {
                            content.get(key).setSchema(resolved);
                        }
                    }
                }
            }
        }
        // responses
        ApiResponses responses = op.getResponses();
        if (responses != null) {
            for (String code : responses.keySet()) {
                ApiResponse response = responses.get(code);
                if (response.getContent() != null) {
                    Map<String, MediaType> content = response.getContent();
                    for (String mediaType : content.keySet()) {
                        if (content.get(mediaType).getSchema() != null) {
                            Schema resolved = resolveSchema(content.get(mediaType).getSchema());
                            response.getContent().get(mediaType).setSchema(resolved);
                        }
                        if (content.get(mediaType).getExamples() != null) {
                            Map<String, Example> resolved = resolveExample(content.get(mediaType).getExamples());
                            response.getContent().get(mediaType).setExamples(resolved);
                        }
                    }
                }
                resolveHeaders(response.getHeaders());
                Map<String, Link> links = response.getLinks();
                if (links != null) {
                    for (Map.Entry<String, Link> link : links.entrySet()) {
                        Link value = link.getValue();
                        Link resolvedValue = value.get$ref() != null ? resolveLink(value) : value;
                        link.setValue(resolvedValue);
                    }
                }
            }
        }
    }
}
Also used : Operation(io.swagger.v3.oas.models.Operation) ApiResponse(io.swagger.v3.oas.models.responses.ApiResponse) PathItem(io.swagger.v3.oas.models.PathItem) Callback(io.swagger.v3.oas.models.callbacks.Callback) Example(io.swagger.v3.oas.models.examples.Example) Parameter(io.swagger.v3.oas.models.parameters.Parameter) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) IdentityHashMap(java.util.IdentityHashMap) ApiResponses(io.swagger.v3.oas.models.responses.ApiResponses) Link(io.swagger.v3.oas.models.links.Link) RequestBody(io.swagger.v3.oas.models.parameters.RequestBody)

Example 5 with Content

use of io.swagger.v3.oas.annotations.media.Content in project swagger-parser by swagger-api.

the class PathsProcessor method updateRefs.

protected void updateRefs(RequestBody body, String pathRef) {
    if (body.get$ref() != null) {
        body.set$ref(computeRef(body.get$ref(), pathRef));
    }
    if (body.getContent() != null) {
        Map<String, MediaType> content = body.getContent();
        for (String key : content.keySet()) {
            MediaType mediaType = content.get(key);
            if (mediaType.getSchema() != null) {
                updateRefs(mediaType.getSchema(), pathRef);
            }
            Map<String, Example> examples = content.get(key).getExamples();
            if (examples != null) {
                for (Example example : examples.values()) {
                    updateRefs(example, pathRef);
                }
            }
        }
    } else if (body.get$ref() != null) {
    }
}
Also used : Example(io.swagger.v3.oas.models.examples.Example) MediaType(io.swagger.v3.oas.models.media.MediaType)

Aggregations

OpenAPI (io.swagger.v3.oas.models.OpenAPI)121 Test (org.testng.annotations.Test)120 MediaType (io.swagger.v3.oas.models.media.MediaType)70 Schema (io.swagger.v3.oas.models.media.Schema)70 Operation (io.swagger.v3.oas.annotations.Operation)65 Content (io.swagger.v3.oas.models.media.Content)64 ApiResponses (io.swagger.v3.oas.annotations.responses.ApiResponses)62 OpenAPIV3Parser (io.swagger.v3.parser.OpenAPIV3Parser)59 ArraySchema (io.swagger.v3.oas.models.media.ArraySchema)57 IntegerSchema (io.swagger.v3.oas.models.media.IntegerSchema)53 Operation (io.swagger.v3.oas.models.Operation)52 ObjectSchema (io.swagger.v3.oas.models.media.ObjectSchema)51 StringSchema (io.swagger.v3.oas.models.media.StringSchema)50 ProtectedApi (org.gluu.oxtrust.service.filter.ProtectedApi)42 PathItem (io.swagger.v3.oas.models.PathItem)41 ArrayList (java.util.ArrayList)41 RequestBody (io.swagger.v3.oas.models.parameters.RequestBody)40 ApiResponse (io.swagger.v3.oas.models.responses.ApiResponse)37 SwaggerParseResult (io.swagger.v3.parser.core.models.SwaggerParseResult)37 ParseOptions (io.swagger.v3.parser.core.models.ParseOptions)33