Class OpenApiContract

java.lang.Object
com.irurueta.authserver.testsupport.OpenApiContract

public final class OpenApiContract extends Object
Compares an OpenAPI contract under apis/ with the OpenAPI document a running application serves. Both are reduced to a sorted summary of what a client depends on: the info title and version, the security schemes, for every operation under a path prefix its operation id, tags, security requirements, parameters, request body and status codes, the schema of every media type an answer declares, and the properties and required properties of every schema. Descriptions are left out: they are the contract's prose, rendered by springdoc from the annotations generated from it. Two documents describe the same API when their summaries are equal, and assertEquals on the two summaries lists exactly what drifted.

springdoc documents a few things the contract does not say: it repeats the return type of an operation under every media type the operation produces and on answers declared without a body (such as a 401), and it adds schemas of its own. restrictTo(SortedMap, SortedMap) leaves those out of the served summary; an operation, a status code, a media type or a schema the contract declares and the application does not serve, or serves differently, is still reported.

Documents are read with SnakeYAML's safe constructor, which reads YAML and JSON alike, and $refs to #/components/... are resolved, so a contract may share parameters and responses that the served document repeats inline.

  • Field Details

  • Constructor Details

    • OpenApiContract

      private OpenApiContract()
      Utility class: not meant to be instantiated.
  • Method Details

    • read

      public static Map<String,Object> read(Path file) throws IOException
      Reads an OpenAPI document from a file.
      Parameters:
      file - YAML or JSON file.
      Returns:
      the document.
      Throws:
      IOException - if the file cannot be read.
    • parse

      public static Map<String,Object> parse(String text)
      Parses an OpenAPI document.
      Parameters:
      text - YAML or JSON text.
      Returns:
      the document.
    • summarize

      public static SortedMap<String,String> summarize(Map<String,Object> document, String pathPrefix)
      Summarizes the API a document describes.
      Parameters:
      document - the OpenAPI document.
      pathPrefix - prefix of the paths that belong to the API, such as /api/.
      Returns:
      the summary, sorted by key.
    • restrictTo

      public static SortedMap<String,String> restrictTo(SortedMap<String,String> served, SortedMap<String,String> contract)
      Restricts the summary of a served document to the schemas and the media types of the answers the contract declares, leaving out what the server documents by itself.
      Parameters:
      served - the summary of the served document.
      contract - the summary of the contract.
      Returns:
      the restricted summary.
    • summarizePath

      private static void summarizePath(Map<String,Object> document, String path, Map<String,Object> item, Object globalSecurity, Map<String,String> summary)
      Summarizes every operation of a path.
      Parameters:
      document - the document, to resolve references.
      path - the path.
      item - the path item.
      globalSecurity - the security requirements of the document, used by operations that declare none.
      summary - the summary to fill.
    • describeScheme

      private static String describeScheme(Map<String,Object> scheme)
      Describes a security scheme by the fields a client relies on.
      Parameters:
      scheme - the scheme.
      Returns:
      its description.
    • describeSchemaProperties

      private static String describeSchemaProperties(Map<String,Object> schema)
      Describes the properties of a schema.
      Parameters:
      schema - the schema.
      Returns:
      its sorted property names and required property names.
    • describeSecurity

      private static String describeSecurity(List<Object> requirements)
      Describes security requirements, an absent or empty list meaning a public operation.
      Parameters:
      requirements - the requirements.
      Returns:
      their sorted description, such as [bearer[admin]].
    • describeParameters

      private static String describeParameters(Map<String,Object> document, List<Object> parameters)
      Describes parameters by location, name and whether they are required.
      Parameters:
      document - the document, to resolve references.
      parameters - the parameters.
      Returns:
      their sorted description.
    • describeBody

      private static String describeBody(Map<String,Object> body)
      Describes a request body.
      Parameters:
      body - the body, already resolved, empty when the operation takes none.
      Returns:
      its description.
    • describeContent

      private static String describeContent(Map<String,Object> content)
      Describes a content map by media type and schema.
      Parameters:
      content - the content map, empty when there is no body.
      Returns:
      its sorted description.
    • describeSchema

      private static String describeSchema(Map<String,Object> schema)
      Describes a schema by name, or by type for an inline one.
      Parameters:
      schema - the schema, empty when there is none.
      Returns:
      its description, such as ClientResponse or array<ClientResponse>.
    • resolve

      private static Object resolve(Map<String,Object> document, Object node)
      Resolves a local reference, such as #/components/responses/Unauthorized.
      Parameters:
      document - the document.
      node - the node, which may be a reference.
      Returns:
      the node it refers to, or the node itself when it is not a reference.
    • sorted

      private static List<String> sorted(Collection<?> values)
      Sorts values by their text.
      Parameters:
      values - the values.
      Returns:
      a sorted list of their texts.
    • map

      private static Map<String,Object> map(Object node)
      Reads a node as a map.
      Parameters:
      node - the node.
      Returns:
      the node, or an empty map when it is absent or not a map.
    • list

      private static List<Object> list(Object node)
      Reads a node as a list.
      Parameters:
      node - the node.
      Returns:
      the node, or an empty list when it is absent or not a list.