private void markPositionsInSection(JsonNode section) { int position = 0; for (Entry<String, JsonNode> field : Util.iterable(section.fields())) { if (!isVendorExtension(field.getKey())) { OpenApiMarkers.markPosition(field.getValue(), position++); } } }
private static JsonNode deepCopy(JsonNode node, IdentityHashMap<JsonNode, JsonNode> copies) { if (copies.containsKey(node)) { return copies.get(node); } if (node.isObject()) { return deepCopyObject((ObjectNode) node, copies); } else if (node.isArray()) { return deepCopyArray((ArrayNode) node, copies); } else { return node.deepCopy(); // all other node types are already safe } }
private void addComponentExtensions(ObjectNode tree, JsonNode spec) { for (Entry<String, JsonNode> field : Util.iterable(spec.get(COMPONENTS_FIELD).fields())) { String fieldName = field.getKey(); if (fieldName.startsWith("x-")) { if (!tree.has(COMPONENTS_FIELD)) { tree.set(COMPONENTS_FIELD, JsonNodeFactory.instance.objectNode()); } ObjectNode components = (ObjectNode) tree.get(COMPONENTS_FIELD); components.set(fieldName, Util.safeDeepCopy(field.getValue())); } } }
if (Util.isRef(node)) { Reference ref = new Reference(Util.getRefString(node).get(), base, modelVersion); if (ref.isModelObjectRef()) { ObjectType section = ref.getSection(); JsonNode result = maybeInline(Util.safeDeepCopy(replacement.get()), ref); addRefFileUrls(result, ref); if (ref.isDefinitionRef()) {
public static boolean isRef(JsonNode node) { return getRefString(node).isPresent(); }
public static JsonNode safeDeepCopy(JsonNode node) { IdentityHashMap<JsonNode, JsonNode> copies = new IdentityHashMap<>(); return deepCopy(node, copies); }
/** * Recursive method to search out and handle all references in this loaded spec */ private JsonNode assembleNode(JsonNode node, Reference base, Set<Reference> visited) { if (Util.isRef(node)) { Reference ref = Reference.fromNode(node, base, contentManager.getModelVersion()); return assembleRef(ref, base, visited); } else { assembleObjectProperties(node, base, visited); assembleArrayElements(node, base, visited); return node; } }
private ObjectNode buildObjectTree() { ObjectNode tree = JsonNodeFactory.instance.objectNode(); while (true) { Optional<LocalContent> optItem = contentManager.getAndRemoveRetainedModelObject(); if (optItem.isPresent()) { LocalContent item = optItem.get(); JsonNode object = Util.safeDeepCopy(item.getContent()); object = inline(object, item.getRef()); OpenApiMarkers.markPosition(object, item.getPosition()); addToTree(tree, object, item.getSectionType(), item.getName()); addJsonPointers(object, item.getRef()); } else { break; } } return tree; }
private void addNonObjects(ObjectNode objectTree, JsonNode spec) { for (Entry<String, JsonNode> field : Util.iterable(spec.fields())) { String fieldName = field.getKey(); // Note: the following test assumes that the top-level property on the path to // any container of model // objects is itself an object container or is a JSON object that only contains // object containers. This // avoids what would be a much trickier test before copying a value from the // top-level source model to the // result model. if (!ObjectType.isObjectContainerPrefix(fieldName, modelVersion)) { objectTree.set(fieldName, Util.safeDeepCopy(field.getValue())); } // // That assumption is NOT valid for v3 models, because the `components` property // can contain vendor // extensions. We fudge it by separately copying vendor extensions appearing in // that property. if (modelVersion == OPENAPI3_MODEL_VERSION && fieldName.equals("components")) { addComponentExtensions(objectTree, spec); } } // TODO V2? if (ObjectType.PATH.getFromNode(objectTree, modelVersion).isMissingNode()) { ObjectType.PATH.setInNode(objectTree, JsonNodeFactory.instance.objectNode(), modelVersion); } }
private static boolean isSimpleRef(JsonNode node) { Optional<String> ref = getRefString(node); return ref.isPresent() ? isSimpleRef(ref.get()) : false; }
/** * If this is an object node, handle any refs among its property values */ private void assembleObjectProperties(JsonNode node, Reference base, Set<Reference> visited) { if (node instanceof ObjectNode) { ObjectNode objectNode = (ObjectNode) node; for (Entry<String, JsonNode> field : Util.iterable(objectNode.fields())) { field.setValue(assembleNode(field.getValue(), base, visited)); } } }
private static JsonNode deepCopyObject(ObjectNode node, IdentityHashMap<JsonNode, JsonNode> copies) { ObjectNode copy = node.objectNode(); copies.put(node, copy); for (Entry<String, JsonNode> field : iterable(node.fields())) { copy.set(field.getKey(), deepCopy(field.getValue(), copies)); } return copy; }
public static Reference fromNode(JsonNode refNode, Reference base, Integer modelVersion) { Optional<String> refString = Util.getRefString(refNode); return refString.isPresent() ? new Reference(refString.get(), base, modelVersion) : null; }
private void maybeInlineObjectFields(JsonNode node, Reference base) { if (node instanceof ObjectNode) { ObjectNode objNode = (ObjectNode) node; for (Entry<String, JsonNode> field : Util.iterable(objNode.fields())) { field.setValue(maybeInline(field.getValue(), base)); } } }
private static boolean isFauxSimpleRef(JsonNode node) { Optional<String> ref = getRefString(node); if (ref.isPresent()) { if (NOT_FAUX_SIMPLE_REF_PAT.matcher(ref.get()).find()) { return false; } // OK, Swagger20Parser will get this wrong, but our fix will be to prepend"./" // to it, which is wrong // if this is a URL with a scheme. So is what Swagger20Parser will do to it, but // at least it's their crap // and not ours if (SCHEME_PAT.matcher(ref.get()).find()) { return false; } else { return true; } } else { return false; } }
private void markPositionsInPath(JsonNode path) { int position = 0; for (Entry<String, JsonNode> method : Util.iterable(path.fields())) { if (!isVendorExtension(method.getKey())) { OpenApiMarkers.markPosition(method.getValue(), position++); JsonNode responses = method.getValue().get("responses"); if (responses != null) { int respPosition = 0; for (Entry<String, JsonNode> response : Util.iterable(responses.fields())) { if (!isVendorExtension(response.getKey())) { OpenApiMarkers.markPosition(response.getValue(), respPosition++); } } } } } }
public static void fixSimpleRefs(JsonNode root) { for (JsonNode param : iterable(root.path("parameters").elements())) { fixParameterSimpleRefs(param); } for (JsonNode path : iterable(root.path("paths").elements())) { fixSimpleParameterRefs(path.path("parameters")); for (String method : Util.swaggerMethodOrder) { JsonNode operation = path.path(method); fixSimpleParameterRefs(operation.path("parameters")); for (JsonNode parameter : iterable(operation.path("parameters").elements())) { if (parameter.path("in").asText().equals("body")) { fixSchemaSimpleRefs(parameter.path("schema")); } } for (JsonNode response : iterable(operation.path("responses").elements())) { fixResponseSimpleRefs(response); } } } for (JsonNode response : iterable(root.path("responses").elements())) { fixResponseSimpleRefs(response); } for (JsonNode definition : iterable(root.path("definitions").elements())) { fixSchemaSimpleRefs(definition); } }
private static void fixSchemaSimpleRefs(JsonNode schema) { if (schema.isMissingNode()) { return; } fixSimpleRef(schema, "definitions"); fixSchemaSimpleRefs(schema.path("items")); fixSchemaSimpleRefs(schema.path("additionalProperties")); for (JsonNode property : iterable(schema.path("properties").elements())) { fixSchemaSimpleRefs(property); } for (JsonNode allOf : iterable(schema.path("allOf").elements())) { fixSchemaSimpleRefs(allOf); } }
public Iterable<String> getObjectNames(ObjectType section) { if (isValid()) { JsonNode sectionNode = section.getFromNode(tree, version); if (sectionNode instanceof ObjectNode) { return Util.iterable(((ObjectNode) sectionNode).fieldNames()); } } return Collections.emptyList(); } }