@Override public JsonNode digest(final JsonNode schema) { final ObjectNode ret = FACTORY.objectNode(); final ArrayNode allowedTypes = FACTORY.arrayNode(); ret.put(keyword, allowedTypes); final JsonNode node = schema.get(keyword); final EnumSet<NodeType> typeSet = EnumSet.noneOf(NodeType.class); if (node.isTextual()) // Single type typeSet.add(NodeType.fromName(node.textValue())); else // More than one type for (final JsonNode element: node) typeSet.add(NodeType.fromName(element.textValue())); if (typeSet.contains(NodeType.NUMBER)) typeSet.add(NodeType.INTEGER); /* * Note that as this is an enumset, order is guaranteed */ for (final NodeType type: typeSet) allowedTypes.add(type.toString()); return ret; } }
simpleTypes.add(type.toString());
private static boolean alreadySupportsNullType(final JsonNode schemaObject) { final String nullType = NodeType.NULL.toString(); final JsonNode typeNode = schemaObject.get(TYPE_KEY); if (typeNode.isTextual()) { return nullType.equals(typeNode.asText()); } else if (typeNode.isArray()) { final ArrayNode typeNodeArr = (ArrayNode) typeNode; for (final JsonNode typeElem : typeNodeArr) { if (nullType.equals(typeElem.asText())) { return true; } } } return false; }
private static void setupNullableTypes(final JsonNode schemaObject) { // If the node is marked as nullable, and the type for this node is not // already "null", then we need to turn the "type" field into a list of // the currently specified type and "null", so that it will be properly // handled by the JSON-schema validation routine. final String nullType = NodeType.NULL.toString(); schemaObject .findParents(TYPE_KEY) .stream() .filter(jsonNode -> jsonNode.path(NULLABLE_KEY).asBoolean(false)) .filter(jsonNode -> !alreadySupportsNullType(jsonNode)) .forEach(jsonNode -> { final JsonNode type = jsonNode.get(TYPE_KEY); if (type.isTextual()) { // If we are here, it means the type is a value // like "string". So we need to transform it into // an array type like [ "null", "string" ]. ((ObjectNode) jsonNode).putArray(TYPE_KEY).add(nullType).add(type); } else if (type.isArray()) { // If we are here, it means the type is already an // array of types, like [ "integer", "string" ]. We // just need to append the null type on the end. ((ArrayNode) type).add(nullType); } }); }