Using Swagger Request Validator to Validate Spring Cloud Contracts

Using Swagger Request Validator to Validate Spring Cloud Contracts

1 Comment

The Swagger Request Validator is a slick project supported by the folks at Atlassian.

Using your API definition in Swagger v2 or OpenAPI v3, it allows you to programmatically validate your API matches your API specification.

The Swagger Request Validator may be used standalone, or with Spring MVC, Spring MockMVC, Spring Web Client, REST Assured, WireMock, or Pact.

Validations the Swagger Request Validator can perform include:

  • Valid API Path / Operation
  • Request Body – expected and if matches JSON Schema
  • Missing Header Parameters
  • Missing or Invalid query parameters
  • Valid Content Type Headers
  • Valid Accept Headers
  • Response Body – if expected and if it matches the JSON schema

In this post, I’ll show you how to define contracts in OpenAPI for Spring Cloud Contract, and how to configure the Atlassian Swagger Request Validator to validate API interactions performed by tests automatically generated by Spring Cloud Contract.

In this example, we will look at a simple Payor service, to look up Payor details for a given Payor Id.

API Request

OpenAPI Operation

The OpenAPI operation is defined as follows:

This is a simple API request. A HTTP GET is performed against the path ‘/v1/payors/{payorId}’; where ‘payorId’ is the UUID of the desired payor.

Spring Framework Rest Controller

The Spring Framework Rest Controller is implemented as follows:

API Response

Open API Response Schema

The response for the operation is defined in YAML as follows:

Java Response Objects

The following Java Objects are defined to support the response:

Spring Cloud Contracts Defined in OA3

In this example, we will define 3 contracts. This example uses my Spring Cloud Contact OA3 converter explained here.

Maven POM

In your Maven POM, you will need to add dependencies for Spring Cloud Contract, the SFG Spring Cloud Contract OA3 Converter, Spring (in general), Atlassian.

Note the configuration of the Spring Cloud Contract Maven plugin.

Enabling OA3 Validation With Swagger Request Validator

There are several options to enable OA3 validation with the Swagger Request Validator.

A challenge to deal with is how Spring Cloud Contract (SCC) generates its unit tests.

SCC by default uses Rest Assured, which is supported by the Swagger Request Validator.

But working from the SCC DSL, I was unable to figure out how to enable the OA3 validation.

The easiest approach I was able to find was avoid the Mock Servlet requests and run integration tests using embedded Tomcat (via Spring Boot).

With this integration, its easy to configure an interceptor for the OA3 validation.

OpenAPI Validation Interceptor

Here is the configuration for the OpenAPI Validation Interceptor

Spring Cloud Contract Configuration

Spring Cloud Contract is configured to use Explicit mode – ie web server on localhost.

Spring Cloud Contract Base Test Class

Spring Cloud Contract Maven Configuration

Note the following configuration properties for SCC. Very important to set ‘testMode’ to EXPLICIT.

Test Generated by Spring Cloud Contract

From the configuration above, the following tests are generated by Spring Cloud Contact.

Test Output

For the 3rd test, I configured the service to return back a Payor object missing required properties.

Here is the console output of the test where you can see the validation failure.

Source Code

The complete source code is available here in my GitHub repository.


With the above configuration, your OpenAPI specification will be validated with every request / response made my Spring Cloud Contract.




About jt

    You May Also Like

    One comment

    1. December 17, 2018 at 4:17 pm

      Thanks a lot for your help.


    Leave a Reply