private JsonNode normalizeToJsonInternal(URL resolutionBase) throws GenerationException { if (spec != null) { return new OpenApiReferenceProcessor(options).of(spec).inline(resolutionBase); } else if (tree != null) { return new OpenApiReferenceProcessor(options).of(tree).inline(resolutionBase); } else { throw new IllegalStateException("Cannot call normalize() before supplying a model spec"); } }
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++); } } }
ObjectNode tree = buildObjectTree(); addNonObjects(tree, topLevel.getTree()); sortTree(tree); markPositions(tree);
JsonNode result = maybeInline(Util.safeDeepCopy(replacement.get()), ref); addRefFileUrls(result, ref); if (ref.isDefinitionRef()) { maybeInlineObjectFields(node, base); maybeInlineArrayElements(node, base); 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 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++); } } } } } }
if (!isVendorExtension(name)) { JsonNode obj = sectionObj.remove(name); sectionObj.set(name, obj);
private void sortResponsesInMethod(ObjectNode method) { ObjectNode responses = (ObjectNode) method.get("responses"); if (responses != null) { List<String> names = Lists.newArrayList(responses.fieldNames()); // numeric responses first, followed by named responses Collections.sort(names, new Comparator<String>() { @Override public int compare(String s1, String s2) { if (numberPat.matcher(s1).matches()) { if (numberPat.matcher(s2).matches()) { return Integer.valueOf(s1) - Integer.valueOf(s2); } else { return -1; } } else if (numberPat.matcher(s2).matches()) { return 1; } else { return String.CASE_INSENSITIVE_ORDER.compare(s1, s2); } } }); for (String name : names) { if (!isVendorExtension(name)) { JsonNode obj = responses.remove(name); responses.set(name, obj); } } } }