Class Library


  • public class Library
    extends Object
    The most common entry points into using the data models library. Provides convenience methods for performing common actions such as i/o, visiting, and validation.
    Author:
    eric.wittmann@gmail.com, jsenko@redhat.com
    • Constructor Detail

      • Library

        public Library()
    • Method Detail

      • addReferenceResolver

        public static void addReferenceResolver​(IReferenceResolver resolver)
        Adds a reference resolver to the library. The resolver will be used whenever the library needs to resolve a $ref reference.
        Parameters:
        resolver -
      • removeReferenceResolver

        public static void removeReferenceResolver​(IReferenceResolver resolver)
      • createDocument

        public static Document createDocument​(ModelType type)
        Creates a new, empty document of the given type.
        Parameters:
        type -
      • readDocument

        public static Document readDocument​(com.fasterxml.jackson.databind.node.ObjectNode json)
        Reads an entire document from JSON data. The JSON data (already parsed, not in string format) is read as a data model Document and returned.
        Parameters:
        json -
      • readDocumentFromJSONString

        public static Document readDocumentFromJSONString​(String jsonString)
        Reads an entire document from a JSON formatted string. This will parse the given string into JSON data and then call Library.readDocument.
        Parameters:
        jsonString -
      • writeDocument

        public static com.fasterxml.jackson.databind.node.ObjectNode writeDocument​(Document document)
        Called to serialize a given data model node to a JSON object.
        Parameters:
        document -
      • writeDocumentToJSONString

        public static String writeDocumentToJSONString​(Document document)
        Called to serialize a given data model to a JSON formatted string.
        Parameters:
        document -
      • readNode

        public static Node readNode​(com.fasterxml.jackson.databind.node.ObjectNode json,
                                    Node node)
        Call this to do a "partial read" on a given node. You must pass the JSON data for the node type and an empty instance of the node class. For example, you could read just an Operation by passing the JSON data for the operation along with an instance of e.g. OpenApi30Operation and this will read the data and store it in the instance.
        Parameters:
        json -
        node -
      • writeNode

        public static com.fasterxml.jackson.databind.node.ObjectNode writeNode​(Node node)
        Called to serialize a given data model node to a JSON object.
        Parameters:
        node -
      • writeNodeToString

        public static String writeNodeToString​(Node node)
        Called to serialize a given data model node to a JSON string.
        Parameters:
        node -
      • visitNode

        public static void visitNode​(Node node,
                                     Visitor visitor)
        Visits a node with the given visitor. Convenience method really - you could just call node.accept(visitor) ... and probably should.
        Parameters:
        node -
        visitor -
      • visitTree

        public static void visitTree​(Node node,
                                     Visitor visitor,
                                     TraverserDirection direction)
        Visits an entire tree (either up or down).
        Parameters:
        node -
        visitor -
        direction -
      • createNodePath

        public static NodePath createNodePath​(Node node)
        Called to create a node path for a given data model node.
        Parameters:
        node -
      • parseNodePath

        public static NodePath parseNodePath​(String path)
        Called to create a node path instance for a stringified node path.
        Parameters:
        path -
      • resolveNodePath

        public static Node resolveNodePath​(NodePath nodePath,
                                           Document doc)
        Resolves the given node path relative to a root document.
        Parameters:
        nodePath -
        doc -
      • transformDocument

        public static Document transformDocument​(Document source,
                                                 ModelType toType)
        Transforms from older versions of specs to newer versions. Currently supports: - OpenAPI 2.0 document into a 3.0 document - OpenAPI 3.0 document into a 3.1 document - AsyncAPI early version to any later version
        Parameters:
        source -
        toType -
      • cloneDocument

        public static Document cloneDocument​(Document source)
        Clones the given document by serializing it to a JS object, and then re-parsing it.
        Parameters:
        source -
      • cloneDocument

        public static Document cloneDocument​(Document source,
                                             UnaryOperator<com.fasterxml.jackson.databind.node.ObjectNode> transformer)
        Clones the given document by serializing it to a JS object, and then re-parsing it.
        Parameters:
        source -
        transformer -
      • validate

        public static List<ValidationProblem> validate​(Node node,
                                                       IValidationSeverityRegistry severityRegistry)
        Called to validate a data model node. All validation rules will be evaluated and reported. The list of validation problems found during validation is returned. In addition, validation problems will be reported on the individual nodes themselves. Validation problem severity is determined by checking with the included severity registry. If the severity registry is null, a default registry is used.
        Parameters:
        node -
        severityRegistry -
      • dereferenceDocument

        public static Document dereferenceDocument​(Document source)
        Dereferences a document - this will take all external references ($ref) found in the document and pull them into this document. It will then update any external reference to instead point to the local copy. The result is a functionally equivalent document with all resolvable external references removed.
        Parameters:
        source - the source document
      • dereferenceDocument

        public static Document dereferenceDocument​(Document source,
                                                   boolean strict)
        Dereferences a document - this will take all external references ($ref) found in the document and pull them into this document. It will then update any external reference to instead point to the local copy. The result is a functionally equivalent document with all resolvable external references removed.
        Parameters:
        source - the source document
        strict - if true, throws an exception if unresolvable references remain
      • dereferenceDocument

        public static Document dereferenceDocument​(Document source,
                                                   IReferenceResolver resolver,
                                                   boolean strict)
        Dereferences a document - this will take all external references ($ref) found in the document and pull them into this document. It will then update any external reference to instead point to the local copy. The result is a functionally equivalent document with all resolvable external references removed.
        Parameters:
        source - the source document
        resolver - a custom reference resolver to use on this dereference operation
        strict - if true, throws an exception if unresolvable references remain