Class RefUtil
java.lang.Object
org.sdase.commons.shared.asyncapi.util.RefUtil
A utility to manipulate
$ref
erences in schemas.-
Method Summary
Modifier and TypeMethodDescriptionstatic void
mergeAllObjectsWithRefsRecursively
(com.fasterxml.jackson.databind.JsonNode node, Function<com.fasterxml.jackson.databind.node.TextNode, com.fasterxml.jackson.databind.node.ObjectNode> refRewriter) Replaces all found$ref
erence properties in the given schemanode
by removing it from their containingObjectNode
and adding all fields of theObjectNode
returned by the givenrefRewriter
.static void
updateAllRefsRecursively
(com.fasterxml.jackson.databind.JsonNode node, Function<com.fasterxml.jackson.databind.node.TextNode, String> refRewriter) Rewrites the text value of all$ref
erences in the given schemanode
according the result of the givenrefRewriter
function.
-
Method Details
-
updateAllRefsRecursively
public static void updateAllRefsRecursively(com.fasterxml.jackson.databind.JsonNode node, Function<com.fasterxml.jackson.databind.node.TextNode, String> refRewriter) Rewrites the text value of all$ref
erences in the given schemanode
according the result of the givenrefRewriter
function.An example to replace all
$ref
pointing todefinitions
with refs pointing tocomponents/schemas
will look like this:RefUtil.updateAllRefsRecursively(schemaNode, refValueTextNode -> { String ref = refValueTextNode.asText(); if (ref.startsWith("#/definitions/")) { return "#/components/schemas/" + ref.substring("#/definitions/".length()); } else { return ref; } });
- Parameters:
node
- the root node of a schema that may contain$ref
erences somewhere in the Json structurerefRewriter
- A function that receives the current value of every$ref
field and returns the new value asString
. If a specific$ref
is not handled, the function MUST return the original String value to keep it.
-
mergeAllObjectsWithRefsRecursively
public static void mergeAllObjectsWithRefsRecursively(com.fasterxml.jackson.databind.JsonNode node, Function<com.fasterxml.jackson.databind.node.TextNode, com.fasterxml.jackson.databind.node.ObjectNode> refRewriter) Replaces all found$ref
erence properties in the given schemanode
by removing it from their containingObjectNode
and adding all fields of theObjectNode
returned by the givenrefRewriter
. All existing properties in the containingObjectNode
remain unchanged unless they are part of the returned rewrite object.- Parameters:
node
- the root node of a schema that may contain$ref
erences somewhere in the Json structurerefRewriter
- A function that receives the current value of every$ref
field and returns anObjectNode
with all fields that should beObjectNode.put(…)
to theObjectNode
containing the$ref
property. If a specific$ref
is not handled, the function MUST return anObjectNode
with a$ref
property having the given value.
-