public String getString( String key, ObjectNode node, boolean required, String location, ParseResult result) { String value = null; JsonNode v = node.get(key); if (node == null || v == null) { if (required) { result.missing(location, key); result.invalid(); } } else { value = v.asText(); } return value; }
public Integer getInteger( String key, ObjectNode node, boolean required, String location, ParseResult result) { Integer value = null; JsonNode v = node.get(key); if (node == null || v == null) { if (required) { result.missing(location, key); result.invalid(); } } else if (v.getNodeType().equals(JsonNodeType.NUMBER)) { value = v.intValue(); } return value; }
public ArrayNode getArray( String key, ObjectNode node, boolean required, String location, ParseResult result) { JsonNode value = node.get(key); ArrayNode an = null; if (value == null) { if (required) { result.missing(location, key); result.invalid(); } } else if (!value.getNodeType().equals(JsonNodeType.ARRAY)) { result.invalidType(location, key, "array", value); } else { an = (ArrayNode) value; } return an; }
public Boolean getBoolean( String key, ObjectNode node, boolean required, String location, ParseResult result) { Boolean value = null; JsonNode v = node.get(key); if (node == null || v == null) { if (required) { result.missing(location, key); result.invalid(); } } else { if (v.getNodeType().equals(JsonNodeType.BOOLEAN)) { value = v.asBoolean(); } else if (v.getNodeType().equals(JsonNodeType.STRING)) { String stringValue = v.textValue(); return Boolean.parseBoolean(stringValue); } } return value; }
public ObjectNode getObject( String key, ObjectNode node, boolean required, String location, ParseResult result) { JsonNode value = node.get(key); ObjectNode on = null; if (value == null) { if (required) { result.missing(location, key); result.invalid(); } } else if (!value.getNodeType().equals(JsonNodeType.OBJECT)) { result.invalidType(location, key, "object", value); if (required) { result.invalid(); } } else { on = (ObjectNode) value; } return on; }
public Model definition(ObjectNode node, String location, ParseResult result) { if (node == null) { result.missing(location, "empty schema"); } if (node.get("$ref") != null) { return refModel(node, location, result); } if (node.get("allOf") != null) { return allOfModel(node, location, result); } Model model = null; String value = null; String type = getString("type", node, false, location, result); Model m = new ModelImpl(); if ("array".equals(type)) { ArrayModel am = new ArrayModel(); ObjectNode propertyNode = getObject("properties", node, false, location, result); Map<String, Property> properties = properties(propertyNode, location, result); am.setProperties(properties); ObjectNode itemsNode = getObject("items", node, false, location, result); Property items = property(itemsNode, location, result); if (items != null) { am.items(items); } model = am; } else { ModelImpl impl = new ModelImpl(); impl.setType(value); JsonNode ap = node.get("additionalProperties"); if (ap != null && ap.getNodeType().equals(JsonNodeType.OBJECT)) { impl.setAdditionalProperties(Json.mapper().convertValue(ap, Property.class)); } value = getString("default", node, false, location, result); impl.setDefaultValue(value); value = getString("format", node, false, location, result); impl.setFormat(value); value = getString("discriminator", node, false, location, result); impl.setDiscriminator(value); JsonNode xml = node.get("xml"); if (xml != null) { impl.setXml(Json.mapper().convertValue(xml, Xml.class)); } ObjectNode externalDocs = getObject("externalDocs", node, false, location, result); ExternalDocs docs = externalDocs(externalDocs, location, result); impl.setExternalDocs(docs); ObjectNode properties = getObject("properties", node, true, location, result); if (properties != null) { Set<String> propertyNames = getKeys(properties); for (String propertyName : propertyNames) { JsonNode propertyNode = properties.get(propertyName); if (propertyNode.getNodeType().equals(JsonNodeType.OBJECT)) { ObjectNode on = (ObjectNode) propertyNode; Property property = property(on, location, result); impl.property(propertyName, property); } else { result.invalidType(location, "properties", "object", propertyNode); } } } // need to set properties first ArrayNode required = getArray("required", node, false, location, result); if (required != null) { List<String> requiredProperties = new ArrayList<String>(); for (JsonNode n : required) { if (n.getNodeType().equals(JsonNodeType.STRING)) { requiredProperties.add(((TextNode) n).textValue()); } else { result.invalidType(location, "required", "string", n); } } if (requiredProperties.size() > 0) { impl.setRequired(requiredProperties); } } // extra keys Set<String> keys = getKeys(node); for (String key : keys) { if (key.startsWith("x-")) { impl.setVendorExtension(key, extension(node.get(key))); } else if (!SCHEMA_KEYS.contains(key)) { result.extra(location, key, node.get(key)); } } if ("{ }".equals(Json.pretty(impl))) return null; model = impl; } JsonNode exampleNode = node.get("example"); if (exampleNode != null) { // we support text or object nodes if (exampleNode.getNodeType().equals(JsonNodeType.OBJECT)) { ObjectNode on = getObject("example", node, false, location, result); if (on != null) { model.setExample(on); } } else { model.setExample(exampleNode.asText()); } } if (model != null) { value = getString("description", node, false, location, result); model.setDescription(value); value = getString("title", node, false, location, result); model.setTitle(value); } return model; }