Example usage for com.fasterxml.jackson.databind.node JsonNodeType OBJECT

List of usage examples for com.fasterxml.jackson.databind.node JsonNodeType OBJECT

Introduction

In this page you can find the example usage for com.fasterxml.jackson.databind.node JsonNodeType OBJECT.

Prototype

JsonNodeType OBJECT

To view the source code for com.fasterxml.jackson.databind.node JsonNodeType OBJECT.

Click Source Link

Usage

From source file:io.swagger.v3.parser.util.OpenAPIDeserializer.java

public Map<String, SecurityScheme> getSecuritySchemes(ObjectNode obj, String location, ParseResult result) {
    if (obj == null) {
        return null;
    }//from  w ww.  j ava  2  s .com
    Map<String, SecurityScheme> securitySchemes = new LinkedHashMap<>();

    Set<String> securitySchemeKeys = getKeys(obj);
    for (String securitySchemeName : securitySchemeKeys) {
        JsonNode securitySchemeValue = obj.get(securitySchemeName);
        if (!securitySchemeValue.getNodeType().equals(JsonNodeType.OBJECT)) {
            result.invalidType(location, securitySchemeName, "object", securitySchemeValue);
        } else {
            ObjectNode securityScheme = (ObjectNode) securitySchemeValue;
            SecurityScheme securitySchemeObj = getSecurityScheme(securityScheme,
                    String.format("%s.%s", location, securitySchemeName), result);
            if (securityScheme != null) {
                securitySchemes.put(securitySchemeName, securitySchemeObj);
            }
        }
    }
    return securitySchemes;
}

From source file:io.swagger.v3.parser.util.OpenAPIDeserializer.java

public Map<String, Schema> getSchemas(ObjectNode obj, String location, ParseResult result) {
    if (obj == null) {
        return null;
    }//  www. j  a va  2s  .co m
    Map<String, Schema> schemas = new LinkedHashMap<>();

    Set<String> schemaKeys = getKeys(obj);
    for (String schemaName : schemaKeys) {
        JsonNode schemaValue = obj.get(schemaName);
        if (!schemaValue.getNodeType().equals(JsonNodeType.OBJECT)) {
            result.invalidType(location, schemaName, "object", schemaValue);
        } else {
            ObjectNode schema = (ObjectNode) schemaValue;
            Schema schemaObj = getSchema(schema, String.format("%s.%s", location, schemaName), result);
            if (schemaObj != null) {
                schemas.put(schemaName, schemaObj);
            }
        }
    }

    return schemas;
}

From source file:io.swagger.v3.parser.util.OpenAPIDeserializer.java

public Schema getSchema(ObjectNode node, String location, ParseResult result) {
    if (node == null) {
        return null;
    }/*from w ww  .  j a v a 2s  . c o m*/

    Schema schema = null;
    ArrayNode oneOfArray = getArray("oneOf", node, false, location, result);
    ArrayNode allOfArray = getArray("allOf", node, false, location, result);
    ArrayNode anyOfArray = getArray("anyOf", node, false, location, result);
    ObjectNode itemsNode = getObject("items", node, false, location, result);

    if ((allOfArray != null) || (anyOfArray != null) || (oneOfArray != null)) {
        ComposedSchema composedSchema = new ComposedSchema();

        if (allOfArray != null) {

            for (JsonNode n : allOfArray) {
                if (n.isObject()) {
                    schema = getSchema((ObjectNode) n, location, result);
                    composedSchema.addAllOfItem(schema);
                }
            }
            schema = composedSchema;
        }
        if (anyOfArray != null) {

            for (JsonNode n : anyOfArray) {
                if (n.isObject()) {
                    schema = getSchema((ObjectNode) n, location, result);
                    composedSchema.addAnyOfItem(schema);
                }
            }
            schema = composedSchema;
        }
        if (oneOfArray != null) {

            for (JsonNode n : oneOfArray) {
                if (n.isObject()) {
                    schema = getSchema((ObjectNode) n, location, result);
                    composedSchema.addOneOfItem(schema);
                }
            }
            schema = composedSchema;
        }
    }

    if (itemsNode != null) {
        ArraySchema items = new ArraySchema();
        if (itemsNode.getNodeType().equals(JsonNodeType.OBJECT)) {
            items.setItems(getSchema(itemsNode, location, result));
        } else if (itemsNode.getNodeType().equals(JsonNodeType.ARRAY)) {
            for (JsonNode n : itemsNode) {
                if (n.isValueNode()) {
                    items.setItems(getSchema(itemsNode, location, result));
                }
            }
        }
        schema = items;
    }

    if (schema == null) {
        schema = SchemaTypeUtil.createSchemaByType(node);
    }

    JsonNode ref = node.get("$ref");
    if (ref != null) {
        if (ref.getNodeType().equals(JsonNodeType.STRING)) {
            String mungedRef = mungedRef(ref.textValue());
            if (mungedRef != null) {
                schema.set$ref(mungedRef);
            } else {
                schema.set$ref(ref.asText());
            }
            return schema;
        } else {
            result.invalidType(location, "$ref", "string", node);
            return null;
        }
    }

    String value = getString("title", node, false, location, result);
    if (StringUtils.isNotBlank(value)) {
        schema.setTitle(value);
    }

    ObjectNode discriminatorNode = getObject("discriminator", node, false, location, result);
    if (discriminatorNode != null) {
        schema.setDiscriminator(getDiscriminator(discriminatorNode, location, result));
    }

    BigDecimal bigDecimal = getBigDecimal("multipleOf", node, false, location, result);
    if (bigDecimal != null) {
        schema.setMultipleOf(bigDecimal);
    }

    bigDecimal = getBigDecimal("maximum", node, false, location, result);
    if (bigDecimal != null) {
        schema.setMaximum(bigDecimal);
    }

    Boolean bool = getBoolean("exclusiveMaximum", node, false, location, result);
    if (bool != null) {
        schema.setExclusiveMaximum(bool);
    }

    bigDecimal = getBigDecimal("minimum", node, false, location, result);
    if (bigDecimal != null) {
        schema.setMinimum(bigDecimal);
    }

    bool = getBoolean("exclusiveMinimum", node, false, location, result);
    if (bool != null) {
        schema.setExclusiveMinimum(bool);
    }

    Integer integer = getInteger("minLength", node, false, location, result);
    if (integer != null) {
        schema.setMinLength(integer);
    }

    integer = getInteger("maxLength", node, false, location, result);
    if (integer != null) {
        schema.setMaxLength(integer);
    }

    String pattern = getString("pattern", node, false, location, result);
    if (StringUtils.isNotBlank(pattern)) {
        schema.setPattern(pattern);
    }

    integer = getInteger("maxItems", node, false, location, result);
    if (integer != null) {
        schema.setMaxItems(integer);
    }
    integer = getInteger("minItems", node, false, location, result);
    if (integer != null) {
        schema.setMinItems(integer);
    }

    bool = getBoolean("uniqueItems", node, false, location, result);
    if (bool != null) {
        schema.setUniqueItems(bool);
    }

    integer = getInteger("maxProperties", node, false, location, result);
    if (integer != null) {
        schema.setMaxProperties(integer);
    }

    integer = getInteger("minProperties", node, false, location, result);
    if (integer != null) {
        schema.setMinProperties(integer);
    }

    ArrayNode required = getArray("required", node, false, location, result);
    if (required != null) {
        List<String> requiredList = new ArrayList<>();
        for (JsonNode n : required) {
            if (n.getNodeType().equals(JsonNodeType.STRING)) {
                requiredList.add(((TextNode) n).textValue());
            } else {
                result.invalidType(location, "required", "string", n);
            }
        }
        if (requiredList.size() > 0) {
            schema.setRequired(requiredList);
        }
    }

    ArrayNode enumArray = getArray("enum", node, false, location, result);
    if (enumArray != null) {
        for (JsonNode n : enumArray) {
            if (n.isNumber()) {
                schema.addEnumItemObject(n.numberValue());
            } else if (n.isValueNode()) {
                schema.addEnumItemObject(n.asText());
            } else {
                result.invalidType(location, "enum", "value", n);
            }
        }
    }

    value = getString("type", node, false, location, result);
    if (StringUtils.isNotBlank(value)) {
        schema.setType(value);
    } else {
        // may have an enum where type can be inferred
        JsonNode enumNode = node.get("enum");
        if (enumNode != null && enumNode.isArray()) {
            String type = inferTypeFromArray((ArrayNode) enumNode);
            schema.setType(type);
        }
    }

    ObjectNode notObj = getObject("not", node, false, location, result);
    if (notObj != null) {
        Schema not = getSchema(notObj, location, result);
        if (not != null) {
            schema.setNot(not);
        }
    }

    Map<String, Schema> properties = new LinkedHashMap<>();
    ObjectNode propertiesObj = getObject("properties", node, false, location, result);
    Schema property = null;

    Set<String> keys = getKeys(propertiesObj);
    for (String name : keys) {
        JsonNode propertyValue = propertiesObj.get(name);
        if (!propertyValue.getNodeType().equals(JsonNodeType.OBJECT)) {
            result.invalidType(location, "properties", "object", propertyValue);
        } else {
            if (propertiesObj != null) {
                property = getSchema((ObjectNode) propertyValue, location, result);
                if (property != null) {
                    properties.put(name, property);
                }
            }
        }
    }
    if (propertiesObj != null) {
        schema.setProperties(properties);
    }

    if (node.get("additionalProperties") != null) {
        if (node.get("additionalProperties").getNodeType().equals(JsonNodeType.OBJECT)) {
            ObjectNode additionalPropertiesObj = getObject("additionalProperties", node, false, location,
                    result);
            if (additionalPropertiesObj != null) {
                Schema additionalProperties = getSchema(additionalPropertiesObj, location, result);
                if (additionalProperties != null) {
                    schema.setAdditionalProperties(additionalProperties);
                }
            }
        } else if (node.get("additionalProperties").getNodeType().equals(JsonNodeType.BOOLEAN)) {
            Boolean additionalProperties = getBoolean("additionalProperties", node, false, location, result);
            if (additionalProperties != null) {
                schema.setAdditionalProperties(additionalProperties);
            }

        }
    }
    value = getString("description", node, false, location, result);
    if (StringUtils.isNotBlank(value)) {
        schema.setDescription(value);
    }

    value = getString("format", node, false, location, result);
    if (StringUtils.isNotBlank(value)) {
        schema.setFormat(value);
    }

    value = getString("default", node, false, location, result);
    if (StringUtils.isNotBlank(value)) {
        schema.setDefault(value);
    }

    //discriminator

    bool = getBoolean("nullable", node, false, location, result);
    if (bool != null) {
        schema.setNullable(bool);
    }

    bool = getBoolean("readOnly", node, false, location, result);
    if (bool != null) {
        schema.setReadOnly(bool);
    }

    bool = getBoolean("writeOnly", node, false, location, result);
    if (bool != null) {
        schema.setWriteOnly(bool);
    }

    ObjectNode xmlNode = getObject("xml", node, false, location, result);
    if (xmlNode != null) {
        XML xml = getXml(xmlNode, location, result);
        if (xml != null) {
            schema.setXml(xml);
        }
    }

    ObjectNode externalDocs = getObject("externalDocs", node, false, location, result);
    if (externalDocs != null) {
        ExternalDocumentation docs = getExternalDocs(externalDocs, location, result);
        if (docs != null) {
            schema.setExternalDocs(docs);
        }
    }

    Object example = getAnyExample("example", node, location, result);
    if (example != null) {
        schema.setExample(example);
    }

    bool = getBoolean("deprecated", node, false, location, result);
    if (bool != null) {
        schema.setDeprecated(bool);
    }

    Map<String, Object> extensions = getExtensions(node);
    if (extensions != null && extensions.size() > 0) {
        schema.setExtensions(extensions);
    }

    Set<String> schemaKeys = getKeys(node);
    for (String key : schemaKeys) {
        if (!SCHEMA_KEYS.contains(key) && !key.startsWith("x-")) {
            result.extra(location, key, node.get(key));
        }
    }

    return schema;

}

From source file:io.swagger.v3.parser.util.OpenAPIDeserializer.java

public Map<String, Example> getExamples(ObjectNode obj, String location, ParseResult result) {
    if (obj == null) {
        return null;
    }/*from  ww w.  j  a v  a  2 s .com*/
    Map<String, Example> examples = new LinkedHashMap<>();

    Set<String> exampleKeys = getKeys(obj);
    for (String exampleName : exampleKeys) {
        JsonNode exampleValue = obj.get(exampleName);
        if (!exampleValue.getNodeType().equals(JsonNodeType.OBJECT)) {
            result.invalidType(location, exampleName, "object", exampleValue);
        } else {
            ObjectNode example = (ObjectNode) exampleValue;
            if (example != null) {
                Example exampleObj = getExample(example, String.format("%s.%s", location, exampleName), result);
                if (exampleObj != null) {
                    examples.put(exampleName, exampleObj);
                }
            }
        }
    }
    return examples;
}

From source file:io.swagger.v3.parser.util.OpenAPIDeserializer.java

public List<Example> getExampleList(ArrayNode obj, String location, ParseResult result) {
    List<Example> examples = new ArrayList<>();
    if (obj == null) {
        return examples;
    }//from  w w  w .  j  a  va 2s . c o m
    for (JsonNode item : obj) {
        if (item.getNodeType().equals(JsonNodeType.OBJECT)) {
            Example example = getExample((ObjectNode) item, location, result);
            if (example != null) {
                examples.add(example);
            }
        }
    }
    return examples;
}

From source file:io.swagger.v3.parser.util.OpenAPIDeserializer.java

public List<SecurityRequirement> getSecurityRequirementsList(ArrayNode nodes, String location,
        ParseResult result) {/* w w w.ja v a  2s  . c  om*/
    if (nodes == null)
        return null;

    List<SecurityRequirement> securityRequirements = new ArrayList<>();

    for (JsonNode node : nodes) {
        if (node.getNodeType().equals(JsonNodeType.OBJECT)) {
            SecurityRequirement securityRequirement = new SecurityRequirement();
            Set<String> keys = getKeys((ObjectNode) node);
            if (keys.size() == 0) {
                securityRequirements.add(securityRequirement);
            } else {
                for (String key : keys) {
                    if (key != null) {
                        JsonNode value = node.get(key);
                        if (key != null && JsonNodeType.ARRAY.equals(value.getNodeType())) {
                            ArrayNode arrayNode = (ArrayNode) value;
                            List<String> scopes = Stream.generate(arrayNode.elements()::next)
                                    .map((n) -> n.asText()).limit(arrayNode.size())
                                    .collect(Collectors.toList());
                            securityRequirement.addList(key, scopes);
                        }
                    }
                }
                if (securityRequirement.size() > 0) {
                    securityRequirements.add(securityRequirement);
                }
            }
        }
    }

    return securityRequirements;

}

From source file:io.swagger.v3.parser.util.OpenAPIDeserializer.java

public Map<String, RequestBody> getRequestBodies(ObjectNode obj, String location, ParseResult result) {
    if (obj == null) {
        return null;
    }// w w w. j  a  v  a  2s  . c o  m
    Map<String, RequestBody> bodies = new LinkedHashMap<>();

    Set<String> bodyKeys = getKeys(obj);
    for (String bodyName : bodyKeys) {
        JsonNode bodyValue = obj.get(bodyName);
        if (!bodyValue.getNodeType().equals(JsonNodeType.OBJECT)) {
            result.invalidType(location, bodyName, "object", bodyValue);
        } else {
            ObjectNode bodyObj = (ObjectNode) bodyValue;
            RequestBody body = getRequestBody(bodyObj, String.format("%s.%s", location, bodyName), result);
            if (body != null) {
                bodies.put(bodyName, body);
            }
        }
    }
    return bodies;
}