private void markPositions(ObjectNode tree) { for (ObjectType sectionType : ObjectType.getTypesForVersion(modelVersion)) { JsonNode section = sectionType.getFromNode(tree, modelVersion); if (section != null) { markPositionsInSection(section); if (sectionType == ObjectType.PATH) { for (Entry<String, JsonNode> path : Util.iterable(section.fields())) { markPositionsInPath(path.getValue()); } } } } }
private void addPathsIfNeeded(JsonNode tree) { if (ObjectType.PATH.getFromNode(tree, modelVersion).isMissingNode()) { ObjectType.PATH.setInNode(tree, JsonNodeFactory.instance.objectNode(), modelVersion); } } }
private void setObjectRefInfo() { if (fragment != null) { ObjectType.find(fragment, modelVersion).ifPresent(type -> { this.objectType = type; this.isObjectRef = true; this.objectName = decodeFromJsonPointer(type.getObjectName(fragment, modelVersion)); }); } }
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); } }
public Optional<JsonNode> getObject(ObjectType section, String name) { if (isValid()) { JsonNode sectionNode = section.getFromNode(tree, version); return sectionNode.has(name) ? Optional.of(sectionNode.get(name)) : Optional.<JsonNode>empty(); } return Optional.empty(); }
public void applyRetentionPolicy(RetentionPolicy policy) { for (Entry<String, Content> e : contentItems.entrySet()) { Content content = e.getValue(); for (ObjectType section : ObjectType.getTypesForVersion(modelVersion)) { if (policy.shouldRetain(urlCache.get(e.getKey()), section)) { retainObjectsInSection(content, section); } } } }
public Reference(ObjectType section, String objectName, Reference context, Integer modelVersion) { this("#" + section.getPath(modelVersion) + "/" + encodeForJsonPointer(objectName), context, modelVersion); }
/** * Build a JsonNode tree to contain the given value at the given JsonPointer * path. Empty array and object nodes are created and nested as needed. * * @param p a JsonPointer indicating where the value is to appear in the * resulting structure * @param value the value to embed in the structure * @return the created structure */ private JsonNode buildTree(JsonPointer p, JsonNode value) { if (p.matches()) { return value; } else if (p.getMatchingIndex() >= 0) { ArrayNode result = JsonNodeFactory.instance.arrayNode(); result.set(p.getMatchingIndex(), buildTree(p.tail(), value)); return result; } else { ObjectNode result = JsonNodeFactory.instance.objectNode(); result.set(p.getMatchingProperty(), buildTree(p.tail(), value)); return result; } }
public String getSectionPath() { return objectType != null ? objectType.getPathString(modelVersion) : null; }
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(); } }
/** * Localize all the model objects in the given content item * <p> * This is used when to ensure that all model objects appearing in the top-level * spec will retain their original names in the final spec. Model objects * appearing in other specs may have their names changed for disambiguation. */ public void localizeObjects(Content content) { for (ObjectType section : ObjectType.getTypesForVersion(modelVersion, Option.COMPONENT_OBJECTS)) { localizeObjectsInSection(content, section); } localizeObjectsInSection(content, ObjectType.PATH); }
/** * Determines whether the given top-level property name is the first componnet * of any container paths for the given model version. This supports a simple * but not entirely general method of determining whether a property appearing * in the primary source model should be copied as-is into the normalized model. * Assumption is that if it's a contianer prefix, then it should not be. This * works for v2 models, but not quite for v3 models, where vendor extensions of * the /components value need to be handled specially. A more general solution * would require walking the tree to some extent to find nodes that are not * within any containers, and copying them as-is as they are encountered. * * @param propertyName top-level property name appearing in model * @param version model version * @return true if the property is on the path to any model object container */ public static boolean isObjectContainerPrefix(String propertyName, int version) { return typesByVersion.get(version).stream() .anyMatch(type -> type.getPath(version).getMatchingProperty().equals(propertyName)); }
private void sort(ObjectNode tree) { for (ObjectType sectionType : ObjectType.getTypesForVersion(modelVersion)) { JsonNode section = sectionType.getFromNode(tree, modelVersion); if (!section.isMissingNode()) { sortSection(section); if (sectionType == ObjectType.PATH) { for (Entry<String, JsonNode> path : Util.iterable(section.fields())) { sortInPath(path.getValue()); } } } } }
private void addToTree(ObjectNode tree, JsonNode object, ObjectType sectionType, String objectName) { if (sectionType.getFromNode(tree, modelVersion).isMissingNode()) { sectionType.setInNode(tree, tree.objectNode(), modelVersion); } ObjectNode section = (ObjectNode) sectionType.getFromNode(tree, modelVersion); section.set(objectName, object); }
private void reorderTree(JsonNode tree) { for (ObjectType sectionType : ObjectType.getTypesForVersion(modelVersion)) { JsonNode section = sectionType.getFromNode(tree, modelVersion); if (!section.isMissingNode()) { reorderPropertiesInObject((ObjectNode) section); if (sectionType == ObjectType.PATH) { for (Entry<String, JsonNode> path : Util.iterable(section.fields())) { reorderInPath((ObjectNode) path.getValue()); } } } } }