public Set<String> getKeys(ObjectNode node) { Set<String> keys = new LinkedHashSet<>(); if (node == null) { return keys; } Iterator<String> it = node.fieldNames(); while (it.hasNext()) { keys.add(it.next()); } return keys; }
public Operation operation(ObjectNode obj, String location, ParseResult result) { if (obj == null) { return null; } Operation output = new Operation(); ArrayNode array = getArray("tags", obj, false, location, result); List<String> tags = tagStrings(array, location, result); if (tags != null) { output.tags(tags); } String value = getString("summary", obj, false, location, result); output.summary(value); value = getString("description", obj, false, location, result); output.description(value); ObjectNode externalDocs = getObject("externalDocs", obj, false, location, result); ExternalDocs docs = externalDocs(externalDocs, location, result); output.setExternalDocs(docs); value = getString("operationId", obj, false, location, result); output.operationId(value); array = getArray("consumes", obj, false, location, result); if (array != null) { if (array.size() == 0) { output.consumes(Collections.<String>emptyList()); } else { Iterator<JsonNode> it = array.iterator(); while (it.hasNext()) { JsonNode n = it.next(); String s = getString(n, location + ".consumes", result); if (s != null) { output.consumes(s); } } } } array = getArray("produces", obj, false, location, result); if (array != null) { if (array.size() == 0) { output.produces(Collections.<String>emptyList()); } else { Iterator<JsonNode> it = array.iterator(); while (it.hasNext()) { JsonNode n = it.next(); String s = getString(n, location + ".produces", result); if (s != null) { output.produces(s); } } } } ArrayNode parameters = getArray("parameters", obj, false, location, result); output.setParameters(parameters(parameters, location, result)); ObjectNode responses = getObject("responses", obj, true, location, result); output.setResponses(responses(responses, location, result)); array = getArray("schemes", obj, false, location, result); if (array != null) { Iterator<JsonNode> it = array.iterator(); while (it.hasNext()) { JsonNode n = it.next(); String s = getString(n, location + ".schemes", result); if (s != null) { Scheme scheme = Scheme.forValue(s); if (scheme != null) { output.scheme(scheme); } } } } Boolean deprecated = getBoolean("deprecated", obj, false, location, result); if (deprecated != null) { output.setDeprecated(deprecated); } array = getArray("security", obj, false, location, result); List<SecurityRequirement> security = securityRequirements(array, location, result); if (security != null) { List<Map<String, List<String>>> ss = new ArrayList<>(); for (SecurityRequirement s : security) { if (s.getRequirements() != null && s.getRequirements().size() > 0) { ss.add(s.getRequirements()); } } output.setSecurity(ss); } // extra keys Set<String> keys = getKeys(obj); for (String key : keys) { if (key.startsWith("x-")) { output.setVendorExtension(key, extension(obj.get(key))); } else if (!OPERATION_KEYS.contains(key)) { result.extra(location, key, obj.get(key)); } } return output; }
public Swagger parseRoot(JsonNode node, ParseResult result) { String location = ""; Swagger swagger = new Swagger(); if (node.getNodeType().equals(JsonNodeType.OBJECT)) { ObjectNode on = (ObjectNode) node; Iterator<JsonNode> it = null; // required String value = getString("swagger", on, true, location, result); swagger.setSwagger(value); ObjectNode obj = getObject("info", on, true, "", result); if (obj != null) { Info info = info(obj, "info", result); swagger.info(info); } // optional value = getString("host", on, false, location, result); swagger.setHost(value); value = getString("basePath", on, false, location, result); swagger.setBasePath(value); ArrayNode array = getArray("schemes", on, false, location, result); if (array != null) { it = array.iterator(); while (it.hasNext()) { JsonNode n = it.next(); String s = getString(n, location + ".schemes", result); if (s != null) { Scheme scheme = Scheme.forValue(s); if (scheme != null) { swagger.scheme(scheme); } } } } array = getArray("consumes", on, false, location, result); if (array != null) { it = array.iterator(); while (it.hasNext()) { JsonNode n = it.next(); String s = getString(n, location + ".consumes", result); if (s != null) { swagger.consumes(s); } } } array = getArray("produces", on, false, location, result); if (array != null) { it = array.iterator(); while (it.hasNext()) { JsonNode n = it.next(); String s = getString(n, location + ".produces", result); if (s != null) { swagger.produces(s); } } } obj = getObject("paths", on, true, location, result); Map<String, Path> paths = paths(obj, "paths", result); swagger.paths(paths); obj = getObject("definitions", on, false, location, result); Map<String, Model> definitions = definitions(obj, "definitions", result); swagger.setDefinitions(definitions); obj = getObject("parameters", on, false, location, result); // TODO: parse Map<String, Parameter> parameters = Json.mapper() .convertValue( obj, Json.mapper() .getTypeFactory() .constructMapType(Map.class, String.class, Parameter.class)); swagger.setParameters(parameters); obj = getObject("responses", on, false, location, result); Map<String, Response> responses = responses(obj, "responses", result); swagger.responses(responses); obj = getObject("securityDefinitions", on, false, location, result); Map<String, SecuritySchemeDefinition> securityDefinitions = securityDefinitions(obj, location, result); swagger.setSecurityDefinitions(securityDefinitions); array = getArray("security", on, false, location, result); List<SecurityRequirement> security = securityRequirements(array, location, result); swagger.setSecurity(security); array = getArray("tags", on, false, location, result); List<Tag> tags = tags(array, location, result); swagger.tags(tags); obj = getObject("externalDocs", on, false, location, result); ExternalDocs docs = externalDocs(obj, location, result); swagger.externalDocs(docs); // extra keys Set<String> keys = getKeys(on); for (String key : keys) { if (key.startsWith("x-")) { swagger.vendorExtension(key, extension(on.get(key))); } else if (!ROOT_KEYS.contains(key)) { result.extra(location, key, node.get(key)); } } } else { result.invalidType("", "", "object", node); result.invalid(); return null; } return swagger; }