Class OpenAPIUtil


  • public class OpenAPIUtil
    extends Object
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static JsonNode buildSchema​(io.swagger.v3.oas.models.media.Schema<?> schema, io.swagger.v3.oas.models.OpenAPI api, OpenAPIParseOption parseOption)
      Build a JSON schema based on the provided Schema.
      static String getDelimiter​(String name, io.swagger.v3.oas.models.parameters.Parameter.StyleEnum style)
      Returns a delimiter String corresponding to the Open Api parameter style enum and parameter name.
      static String getFirstRequestContentType​(io.swagger.v3.oas.models.Operation operation)
      Fetches the first request content type from the given operation.
      static io.swagger.v3.oas.models.PathItem.HttpMethod getHttpMethod​(io.swagger.v3.oas.models.OpenAPI api, String operationId)
      Retrieves the HTTP method based off of the "name" field of the operation
      static JSONUtil.NodeType getNodeType​(io.swagger.v3.oas.models.media.Schema<?> schema)
      Fetches the JSONUtil.NodeType based off of the Schema definition
      static JSONUtil.NodeType getNodeType​(io.swagger.v3.oas.models.media.Schema<?> schema, JSONUtil.NodeType defaultValue)
      Fetches the JSONUtil.NodeType based off of the schema definition.
      static io.swagger.v3.oas.models.Operation getOperation​(io.swagger.v3.oas.models.OpenAPI api, io.swagger.v3.oas.models.PathItem.HttpMethod method, String path)
      Fetch the Operation by path and method.
      static io.swagger.v3.oas.models.Operation getOperation​(io.swagger.v3.oas.models.OpenAPI api, String operationId)
      Retrieves the operation by the "name" field of the operation.
      static List<io.swagger.v3.oas.models.parameters.Parameter> getParametersFromOperation​(io.swagger.v3.oas.models.OpenAPI api, io.swagger.v3.oas.models.Operation operation)
      Retrieves all of the valid supported parameters OpenAPIParameterType for this operation.
      static List<io.swagger.v3.oas.models.parameters.Parameter> getParametersFromOperationByType​(io.swagger.v3.oas.models.OpenAPI api, io.swagger.v3.oas.models.Operation operation, OpenAPIParameterType filterType)
      Retrieves the specified OpenAPIParameterType parameter for this operation.
      static String getPath​(io.swagger.v3.oas.models.OpenAPI api, String operationId)
      Returns the path based off of the Operation ID
      protected static io.swagger.v3.oas.models.Paths getPaths​(io.swagger.v3.oas.models.OpenAPI api)  
      static io.swagger.v3.oas.models.parameters.RequestBody getRequestBody​(io.swagger.v3.oas.models.OpenAPI api, io.swagger.v3.oas.models.PathItem.HttpMethod method, String path)
      Retrieves the request body by http method and path.
      static io.swagger.v3.oas.models.parameters.RequestBody getRequestBody​(io.swagger.v3.oas.models.OpenAPI api, String operationId)
      Retrieves the request body for the operation, based off of the "name" field of the operation.
      static io.swagger.v3.oas.models.media.Content getRequestContent​(io.swagger.v3.oas.models.OpenAPI api, io.swagger.v3.oas.models.PathItem.HttpMethod method, String path)
      Fetches the request content type based off of the PathItem.HttpMethod and the path.
      static io.swagger.v3.oas.models.media.Content getResponseContent​(io.swagger.v3.oas.models.OpenAPI api, io.swagger.v3.oas.models.Operation operation, Pattern pattern)
      Attempts to fetch the first Response content based off of a response code pattern.
      static io.swagger.v3.oas.models.media.Schema<?> getSchema​(io.swagger.v3.oas.models.media.Content content)
      Fetches the schema object from the given Content.
      static io.swagger.v3.oas.models.media.Schema<?> getSchema​(io.swagger.v3.oas.models.OpenAPI api, io.swagger.v3.oas.models.media.Content content)
      Fetches the schema object from Content type map.
      static io.swagger.v3.oas.models.media.Schema<?> getSchema​(io.swagger.v3.oas.models.OpenAPI api, io.swagger.v3.oas.models.media.Schema<?> schema)
      Fetches the schema, if this is a referenced / shared schema, then the reference will be resolved for you
      static io.swagger.v3.oas.models.media.Schema<?> getSchema​(io.swagger.v3.oas.models.OpenAPI api, io.swagger.v3.oas.models.parameters.RequestBody request)
      Fetches the schema for the request body.
      static String getSerializedHeaderParameter​(String name, String parameterValue, OpenAPIDataType dataType)
      Updates input header parameter value according to the data type value from Open Api Specification.
      static String getSerializedPathParameter​(String name, String parameterValue, io.swagger.v3.oas.models.parameters.Parameter.StyleEnum style, boolean explode, OpenAPIDataType dataType)
      Updates input path parameter value according to the style, explode and data type values from Open Api Specification.
      static List<Map.Entry<String,​String>> getSerializedQueryParameter​(String name, String parameterValue, io.swagger.v3.oas.models.parameters.Parameter.StyleEnum style, boolean explode, OpenAPIDataType dataType)
      Updates input query parameter value according to the style, explode and data type values from Open Api Specification.
      static io.swagger.v3.oas.models.parameters.Parameter getSharedParameter​(io.swagger.v3.oas.models.OpenAPI api, String ref)
      Retrieves the shared parameter under the "components" section based off of the $ref value
      static io.swagger.v3.oas.models.parameters.RequestBody getSharedRequest​(io.swagger.v3.oas.models.OpenAPI api, String ref)
      Retrieves the shared response under the "components" section based off of the $ref value
      static io.swagger.v3.oas.models.responses.ApiResponse getSharedResponse​(io.swagger.v3.oas.models.OpenAPI api, String ref)
      Retrieves the shared response under the "components" section based off of the $ref value
      static io.swagger.v3.oas.models.media.Schema<?> getSharedSchema​(io.swagger.v3.oas.models.OpenAPI api, String ref)
      Retrieves the shared schema under the "components" section based off of the $ref value.
      static boolean isParameterSupported​(io.swagger.v3.oas.models.OpenAPI api, io.swagger.v3.oas.models.parameters.Parameter parameter)
      Returns true if the parameter is supported.
      static boolean isRequestBodyRequired​(io.swagger.v3.oas.models.Operation operation)
      Returns a boolean value of the "required" attribute from request body.
      static boolean isSimpleSchema​(io.swagger.v3.oas.models.media.Schema<?> schema)
      Returns true if Schema.getType() is Boolean, Number, Integer, String.
      static boolean isStructured​(io.swagger.v3.oas.models.media.Content content)
      Content is considered structured if the schema associated with it is not null.
      static io.swagger.v3.oas.models.parameters.Parameter resolveParameter​(io.swagger.v3.oas.models.OpenAPI api, io.swagger.v3.oas.models.parameters.Parameter parameter)
      If the incoming parameter is a reference parameter, then this will resolve and return the reference parameter.
      static io.swagger.v3.oas.models.parameters.RequestBody resolveRequestBody​(io.swagger.v3.oas.models.OpenAPI api, io.swagger.v3.oas.models.parameters.RequestBody requestBody)
      If the incoming response is a reference request, then this will resolve and return the reference request.
      static io.swagger.v3.oas.models.responses.ApiResponse resolveResponse​(io.swagger.v3.oas.models.OpenAPI api, io.swagger.v3.oas.models.responses.ApiResponse response)
      If the incoming response is a reference response, then this will resolve and return the reference response.
      static void validateSpecFileLocation​(String spec)
      Validates that the spec file path passed in is usable
    • Method Detail

      • getSchema

        public static io.swagger.v3.oas.models.media.Schema<?> getSchema​(io.swagger.v3.oas.models.OpenAPI api,
                                                                         io.swagger.v3.oas.models.parameters.RequestBody request)
        Fetches the schema for the request body. If this is a referenced / shared schema, then the reference will be resolved for you
        Parameters:
        api - the api definition
        request - the selected request
        Returns:
        the actual schema definition
      • getSchema

        public static io.swagger.v3.oas.models.media.Schema<?> getSchema​(io.swagger.v3.oas.models.OpenAPI api,
                                                                         io.swagger.v3.oas.models.media.Content content)
        Fetches the schema object from Content type map. Currently only supports extracting schema objects from MediaType of type CONTENT_TYPE. Also resolves any $ref pointers in the schema if present.
        Parameters:
        api - the api definition
        content - the selected content
        Returns:
        the actual schema
      • getSchema

        public static io.swagger.v3.oas.models.media.Schema<?> getSchema​(io.swagger.v3.oas.models.media.Content content)
        Fetches the schema object from the given Content. This will NOT resolve $ref for you as just has knowledge of the incoming Content object. Returns null if no content is found within Content
        Parameters:
        content - the selected content
        Returns:
        the content
      • getSchema

        public static io.swagger.v3.oas.models.media.Schema<?> getSchema​(io.swagger.v3.oas.models.OpenAPI api,
                                                                         io.swagger.v3.oas.models.media.Schema<?> schema)
        Fetches the schema, if this is a referenced / shared schema, then the reference will be resolved for you
        Parameters:
        api - the api definition
        schema - the selected schema
        Returns:
        the actual schema definition
      • getSharedSchema

        public static io.swagger.v3.oas.models.media.Schema<?> getSharedSchema​(io.swagger.v3.oas.models.OpenAPI api,
                                                                               String ref)
        Retrieves the shared schema under the "components" section based off of the $ref value.
        Parameters:
        api - the api definition
        ref - the reference path
        Returns:
        the shared schema
      • getSharedParameter

        public static io.swagger.v3.oas.models.parameters.Parameter getSharedParameter​(io.swagger.v3.oas.models.OpenAPI api,
                                                                                       String ref)
        Retrieves the shared parameter under the "components" section based off of the $ref value
        Parameters:
        api - the api definition
        ref - the reference path
        Returns:
        the shared parameter
      • getNodeType

        public static JSONUtil.NodeType getNodeType​(io.swagger.v3.oas.models.media.Schema<?> schema)
        Fetches the JSONUtil.NodeType based off of the Schema definition
        Parameters:
        schema - the selected schema
        Returns:
        the NodeType
      • getNodeType

        public static JSONUtil.NodeType getNodeType​(io.swagger.v3.oas.models.media.Schema<?> schema,
                                                    JSONUtil.NodeType defaultValue)
        Fetches the JSONUtil.NodeType based off of the schema definition. Will use default if not given / invalid.
        Parameters:
        schema - the selected schema
        defaultValue - default value if node type isn't given
        Returns:
        the nodetype
      • isStructured

        public static boolean isStructured​(io.swagger.v3.oas.models.media.Content content)
        Content is considered structured if the schema associated with it is not null.
        Parameters:
        content - the selected content
        Returns:
        true if content is Structured, false otherwise.
      • isSimpleSchema

        public static boolean isSimpleSchema​(io.swagger.v3.oas.models.media.Schema<?> schema)
        Returns true if Schema.getType() is Boolean, Number, Integer, String. Returns false otherwise
        Parameters:
        schema -
        Returns:
        true if it's a simple schema, false otherwise
      • getResponseContent

        public static io.swagger.v3.oas.models.media.Content getResponseContent​(io.swagger.v3.oas.models.OpenAPI api,
                                                                                io.swagger.v3.oas.models.Operation operation,
                                                                                Pattern pattern)
        Attempts to fetch the first Response content based off of a response code pattern. If a response code pattern is not matched, then it will attempt to return the value from the "default" in the response. null if nothing is found.
        Parameters:
        api - the api definition
        operation - the selected operation
        pattern - the response pattern
        Returns:
        the Content
      • resolveResponse

        public static io.swagger.v3.oas.models.responses.ApiResponse resolveResponse​(io.swagger.v3.oas.models.OpenAPI api,
                                                                                     io.swagger.v3.oas.models.responses.ApiResponse response)
        If the incoming response is a reference response, then this will resolve and return the reference response. Otherwise, it will return the response itself
        Parameters:
        api - the api definition
        response - the response
        Returns:
        the shared parameter
      • getSharedResponse

        public static io.swagger.v3.oas.models.responses.ApiResponse getSharedResponse​(io.swagger.v3.oas.models.OpenAPI api,
                                                                                       String ref)
        Retrieves the shared response under the "components" section based off of the $ref value
        Parameters:
        api - the api definition
        ref - the reference path
        Returns:
        the shared parameter
      • resolveRequestBody

        public static io.swagger.v3.oas.models.parameters.RequestBody resolveRequestBody​(io.swagger.v3.oas.models.OpenAPI api,
                                                                                         io.swagger.v3.oas.models.parameters.RequestBody requestBody)
        If the incoming response is a reference request, then this will resolve and return the reference request. Otherwise, it will return the request itself
        Parameters:
        api - the api definition
        requestBody - the requestBody
        Returns:
        the shared parameter
      • getSharedRequest

        public static io.swagger.v3.oas.models.parameters.RequestBody getSharedRequest​(io.swagger.v3.oas.models.OpenAPI api,
                                                                                       String ref)
        Retrieves the shared response under the "components" section based off of the $ref value
        Parameters:
        api - the api definition
        ref - the reference path
        Returns:
        the shared parameter
      • getOperation

        public static io.swagger.v3.oas.models.Operation getOperation​(io.swagger.v3.oas.models.OpenAPI api,
                                                                      io.swagger.v3.oas.models.PathItem.HttpMethod method,
                                                                      String path)
        Fetch the Operation by path and method. This will return null if no operations were found. This will throw an IllegalArgument exception when the following holds true: 1. Invalid path on the Api specs 2. Invalid Method passed in
        Parameters:
        api - the api definition
        method - the selected http method
        path - the selected path
        Returns:
        the operation
      • getParametersFromOperation

        public static List<io.swagger.v3.oas.models.parameters.Parameter> getParametersFromOperation​(io.swagger.v3.oas.models.OpenAPI api,
                                                                                                     io.swagger.v3.oas.models.Operation operation)
        Retrieves all of the valid supported parameters OpenAPIParameterType for this operation. If the parameters are reference parameters ($ref), then this utility will resolve the parameter reference for you.
        Parameters:
        api - the api definition
        operation - the selected operation
        Returns:
        list of parameters
      • getParametersFromOperationByType

        public static List<io.swagger.v3.oas.models.parameters.Parameter> getParametersFromOperationByType​(io.swagger.v3.oas.models.OpenAPI api,
                                                                                                           io.swagger.v3.oas.models.Operation operation,
                                                                                                           OpenAPIParameterType filterType)
        Retrieves the specified OpenAPIParameterType parameter for this operation. If the parameters are reference parameters ($ref), then this utility will resolve the parameter reference for you.
        Parameters:
        api - the api definition
        operation - the selected operation
        filterType - the parameter to filter upon
        Returns:
        list of parameters
      • resolveParameter

        public static io.swagger.v3.oas.models.parameters.Parameter resolveParameter​(io.swagger.v3.oas.models.OpenAPI api,
                                                                                     io.swagger.v3.oas.models.parameters.Parameter parameter)
        If the incoming parameter is a reference parameter, then this will resolve and return the reference parameter. Otherwise, it will return the parameter itself.
        Parameters:
        api - the api definition
        parameter - the parameter to resolve
        Returns:
        the resolved parameter
      • isParameterSupported

        public static boolean isParameterSupported​(io.swagger.v3.oas.models.OpenAPI api,
                                                   io.swagger.v3.oas.models.parameters.Parameter parameter)
        Returns true if the parameter is supported. The following are not supported: Cookie Parameters Header Parameters with values of: "ACCEPT", "CONTENT-TYPE", "AUTHORIZATION"
        Parameters:
        api -
        parameter -
        Returns:
        true if the parameter is supported, false otherwise
      • getOperation

        public static io.swagger.v3.oas.models.Operation getOperation​(io.swagger.v3.oas.models.OpenAPI api,
                                                                      String operationId)
        Retrieves the operation by the "name" field of the operation.
        Parameters:
        api - the api definition
        operationId - the operation's id
        Returns:
        the operation
      • getHttpMethod

        public static io.swagger.v3.oas.models.PathItem.HttpMethod getHttpMethod​(io.swagger.v3.oas.models.OpenAPI api,
                                                                                 String operationId)
        Retrieves the HTTP method based off of the "name" field of the operation
        Parameters:
        api - the api definition
        operationId - the operation id
        Returns:
        the httpMethod
      • getRequestBody

        public static io.swagger.v3.oas.models.parameters.RequestBody getRequestBody​(io.swagger.v3.oas.models.OpenAPI api,
                                                                                     io.swagger.v3.oas.models.PathItem.HttpMethod method,
                                                                                     String path)
        Retrieves the request body by http method and path.
        Parameters:
        api - the api definition
        method - the selected http method
        path - the selected path
        Returns:
        the RequestBody
      • getRequestBody

        public static io.swagger.v3.oas.models.parameters.RequestBody getRequestBody​(io.swagger.v3.oas.models.OpenAPI api,
                                                                                     String operationId)
        Retrieves the request body for the operation, based off of the "name" field of the operation.
        Parameters:
        api - the api definition
        operationId - the operation id
        Returns:
        the RequestBody
      • isRequestBodyRequired

        public static boolean isRequestBodyRequired​(io.swagger.v3.oas.models.Operation operation)
        Returns a boolean value of the "required" attribute from request body. Returns false if request body is null/missing or "required" attribute is null/missing.
        Parameters:
        operation - the selected operation
        Returns:
        true if the request body is required, false otherwise
      • getRequestContent

        public static io.swagger.v3.oas.models.media.Content getRequestContent​(io.swagger.v3.oas.models.OpenAPI api,
                                                                               io.swagger.v3.oas.models.PathItem.HttpMethod method,
                                                                               String path)
        Fetches the request content type based off of the PathItem.HttpMethod and the path. If no content is given, then an empty content is returned.
        Parameters:
        api - the api definition
        method - the selected http method
        path - the selected path
        Returns:
        the Content
      • getFirstRequestContentType

        public static String getFirstRequestContentType​(io.swagger.v3.oas.models.Operation operation)
        Fetches the first request content type from the given operation. If the request body is not defined or the content type is not given, then a null string is returned.
        Parameters:
        operation - the selected operation
        Returns:
        the request content type as String
      • getPath

        public static String getPath​(io.swagger.v3.oas.models.OpenAPI api,
                                     String operationId)
        Returns the path based off of the Operation ID
        Parameters:
        api - the api definition
        operationId - the selected operation id
        Returns:
        the Path
      • getPaths

        protected static io.swagger.v3.oas.models.Paths getPaths​(io.swagger.v3.oas.models.OpenAPI api)
      • getSerializedPathParameter

        public static String getSerializedPathParameter​(String name,
                                                        String parameterValue,
                                                        io.swagger.v3.oas.models.parameters.Parameter.StyleEnum style,
                                                        boolean explode,
                                                        OpenAPIDataType dataType)
        Updates input path parameter value according to the style, explode and data type values from Open Api Specification. Refer Open Api Parameter Serialization doc for more information.
        Parameters:
        name - Path parameter name.
        parameterValue - Path parameter value.
        style - Path parameter style.
        explode - boolean explode property from OpenAPI spec: specifies whether arrays and objects should generate separate parameters for each array item or object property.
        dataType - OpenAPIDataType of the parameter.
        Returns:
        Updated path parameter value based on the style, explode and data type values.
      • getSerializedQueryParameter

        public static List<Map.Entry<String,​String>> getSerializedQueryParameter​(String name,
                                                                                       String parameterValue,
                                                                                       io.swagger.v3.oas.models.parameters.Parameter.StyleEnum style,
                                                                                       boolean explode,
                                                                                       OpenAPIDataType dataType)
        Updates input query parameter value according to the style, explode and data type values from Open Api Specification. Refer Open Api Parameter Serialization doc for more information.
        Parameters:
        name - Query parameter name.
        parameterValue - Query parameter value.
        style - Query parameter style.
        explode - boolean explode property from OpenAPI spec: specifies whether arrays and objects should generate separate parameters for each array item or object property.
        dataType - OpenAPIDataType of the parameter.
        Returns:
        Updated query parameter value based on the style, explode and data type values.
      • getSerializedHeaderParameter

        public static String getSerializedHeaderParameter​(String name,
                                                          String parameterValue,
                                                          OpenAPIDataType dataType)
        Updates input header parameter value according to the data type value from Open Api Specification. Refer Open Api Parameter Serialization doc for more information.
        Parameters:
        name - Header parameter name.
        parameterValue - Header parameter value.
        dataType - OpenAPIDataType of the parameter.
        Returns:
        Updated header parameter value based on the data type values.
      • getDelimiter

        public static String getDelimiter​(String name,
                                          io.swagger.v3.oas.models.parameters.Parameter.StyleEnum style)
        Returns a delimiter String corresponding to the Open Api parameter style enum and parameter name.
        Parameters:
        name - Open Api Parameter Name.
        style - Open Api Parameter Style Enum.
        Returns:
        the delimiter
      • buildSchema

        public static JsonNode buildSchema​(io.swagger.v3.oas.models.media.Schema<?> schema,
                                           io.swagger.v3.oas.models.OpenAPI api,
                                           OpenAPIParseOption parseOption)
        Build a JSON schema based on the provided Schema. Currently only supports building schemas with root type of "object". Supports building schemas with ComposedSchema elements by resolving "allOf", "anyOf", "oneOf" keywords used to combine schemas.
        Parameters:
        schema - the schema
        api - the OpenAPI specification
        parseOption - parse options for the spec
        Returns:
        JsonNode containing the schema
        Throws:
        IllegalArgumentException - if the schema is null
        IllegalArgumentException - if Schema.getType() is not JSONUtil.NodeType.OBJECT
      • validateSpecFileLocation

        public static void validateSpecFileLocation​(String spec)
        Validates that the spec file path passed in is usable
        Parameters:
        spec - path to spec file
        Throws:
        IllegalArgumentException - if the spec file path is not referencing a file that is readable and accessible by the running application