Class OpenApiInteractionValidator
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:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classAn exception thrown when theOpenApiInteractionValidatoris unable to load a given API specstatic classA builder used to createFor configured instances of theOpenApiInteractionValidator.static classHolds the source location for an API specification. -
Method Summary
Modifier and TypeMethodDescriptioncreateFor(io.swagger.v3.oas.models.OpenAPI api) Create a new instance using a parsed API specification.Create a new instance using the OpenAPI / Swagger specification at the given location OR an actual specification payload.createForInlineApiSpecification(String specAsString) Create a new instance using given the OpenAPI / Swagger specification.createForSpecificationUrl(String specUrl) Create a new instance using the OpenAPI / Swagger specification at the given location.Validate the given request/response against the API.validateRequest(Request request) Validate the given request against the API.validateResponse(String path, Request.Method method, Response response) Validate the given response against the API.
-
Method Details
-
createFor
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 usingcreateForInlineApiSpecification(String)orcreateForSpecificationUrl(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
OpenApiInteractionValidatorinstances. - 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
OpenApiInteractionValidatorinstances.
-
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
OpenApiInteractionValidatorinstances.
-
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
OpenApiInteractionValidatorinstances.
-
validate
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
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
-