Class OpenApiInteractionValidator

java.lang.Object
com.atlassian.oai.validator.OpenApiInteractionValidator

public class OpenApiInteractionValidator extends Object
Validates a HTTP interaction (request/response pair) with a Swagger v2 / OpenAPI v3 specification.

Validation errors are provided in a @ValidationReport that can be used to inspect the failures.

New instances should be created via the createFor(String) method.

See Also:
  • Method Details

    • createFor

      public static OpenApiInteractionValidator.Builder createFor(@Nonnull String specUrlOrPayload)
      Create a new instance using the OpenAPI / Swagger specification at the given location OR an actual specification payload.

      Supports both Swagger v2 and OpenAPI v3 specifications, in both JSON and YAML formats.

      The URL can be an absolute HTTP/HTTPS URL, a File URL or a classpath location (without the classpath: scheme).

      For example:

           // Create from a publicly hosted HTTP location
           .createFor("http://api.myservice.com/swagger.json")
      
           // Create from a file on the local filesystem
           .createFor("file://Users/myuser/tmp/api.yaml");
      
           // Create from a classpath resource in the /api package
           .createFor("/api/swagger.json");
      
           // Create from an OpenAPI / Swagger payload
           .createFor("{\"swagger\": \"2.0\", ...}")
       
      Note: This method may log exceptions during normal operation. To avoid this, consider using createForInlineApiSpecification(String) or createForSpecificationUrl(String) instead. This method may be deprecated in the future.
      Parameters:
      specUrlOrPayload - The location of the OpenAPI / Swagger specification to use in the validator, or the inline specification to use.
      Returns:
      A new builder instance to use for creating and configuring OpenApiInteractionValidator instances.
      See Also:
    • createForInlineApiSpecification

      public static OpenApiInteractionValidator.Builder createForInlineApiSpecification(@Nonnull String specAsString)
      Create a new instance using given the OpenAPI / Swagger specification.

      Supports both Swagger v2 and OpenAPI v3 specifications, in both JSON and YAML formats.

      Parameters:
      specAsString - The OpenAPI / Swagger specification to use in the validator
      Returns:
      A new builder instance to use for creating and configuring OpenApiInteractionValidator instances.
    • createForSpecificationUrl

      public static OpenApiInteractionValidator.Builder createForSpecificationUrl(@Nonnull String specUrl)
      Create a new instance using the OpenAPI / Swagger specification at the given location.

      Supports both Swagger v2 and OpenAPI v3 specifications, in both JSON and YAML formats.

      The URL can be an absolute HTTP/HTTPS URL, a File URL or a classpath location (without the classpath: scheme).

      For example:

           // Create from a publicly hosted HTTP location
           .createFor("http://api.myservice.com/swagger.json")
      
           // Create from a file on the local filesystem
           .createFor("file://Users/myuser/tmp/api.yaml");
      
           // Create from a classpath resource in the /api package
           .createFor("/api/swagger.json");
      
           // Create from an OpenAPI / Swagger payload
           .createFor("{\"swagger\": \"2.0\", ...}")
       
      Parameters:
      specUrl - The location of the OpenAPI / Swagger specification to use in the validator
      Returns:
      A new builder instance to use for creating and configuring OpenApiInteractionValidator instances.
    • createFor

      public static OpenApiInteractionValidator.Builder createFor(@Nonnull io.swagger.v3.oas.models.OpenAPI api)
      Create a new instance using a parsed API specification.
      Parameters:
      api - The API specification to use for validation
      Returns:
      A new builder instance to use for creating and configuring OpenApiInteractionValidator instances.
    • validate

      @Nonnull public ValidationReport validate(@Nonnull Request request, @Nonnull Response response)
      Validate the given request/response against the API.

      See class docs for more information on the validation performed.

      Parameters:
      request - The request to validate (required)
      response - The response to validate (required)
      Returns:
      The outcome of the validation
    • validateRequest

      @Nonnull public ValidationReport validateRequest(@Nonnull Request request)
      Validate the given request against the API.

      See class docs for more information on the validation performed.

      Parameters:
      request - The request to validate (required)
      Returns:
      The outcome of the request validation
    • validateResponse

      @Nonnull public ValidationReport validateResponse(@Nonnull String path, @Nonnull Request.Method method, @Nonnull Response response)
      Validate the given response against the API.

      See class docs for more information on the validation performed.

      Parameters:
      path - The request path (required)
      method - The request method (required)
      response - The response to validate (required)
      Returns:
      The outcome of the response validation