Search in sources :

Example 1 with CookieParameter

use of io.swagger.v3.oas.models.parameters.CookieParameter 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)

Aggregations

JsonNode (com.fasterxml.jackson.databind.JsonNode)1 NullNode (com.fasterxml.jackson.databind.node.NullNode)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 Content (io.swagger.v3.oas.models.media.Content)1 CookieParameter (io.swagger.v3.oas.models.parameters.CookieParameter)1 HeaderParameter (io.swagger.v3.oas.models.parameters.HeaderParameter)1 Parameter (io.swagger.v3.oas.models.parameters.Parameter)1 PathParameter (io.swagger.v3.oas.models.parameters.PathParameter)1 QueryParameter (io.swagger.v3.oas.models.parameters.QueryParameter)1