java.lang.Object
org.sdase.commons.shared.asyncapi.util.RefUtil

public class RefUtil extends Object
A utility to manipulate $references in schemas.
  • Method Summary

    Modifier and Type
    Method
    Description
    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 $reference properties in the given schema node by removing it from their containing ObjectNode and adding all fields of the ObjectNode returned by the given refRewriter.
    static void
    updateAllRefsRecursively(com.fasterxml.jackson.databind.JsonNode node, Function<com.fasterxml.jackson.databind.node.TextNode,String> refRewriter)
    Rewrites the text value of all $references in the given schema node according the result of the given refRewriter function.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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 $references in the given schema node according the result of the given refRewriter function.

      An example to replace all $ref pointing to definitions with refs pointing to components/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 $references somewhere in the Json structure
      refRewriter - A function that receives the current value of every $ref field and returns the new value as String. 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 $reference properties in the given schema node by removing it from their containing ObjectNode and adding all fields of the ObjectNode returned by the given refRewriter. All existing properties in the containing ObjectNode remain unchanged unless they are part of the returned rewrite object.
      Parameters:
      node - the root node of a schema that may contain $references somewhere in the Json structure
      refRewriter - A function that receives the current value of every $ref field and returns an ObjectNode with all fields that should be ObjectNode.put(…) to the ObjectNode containing the $ref property. If a specific $ref is not handled, the function MUST return an ObjectNode with a $ref property having the given value.