Swagger/OpenAPI Data Generation

Published on April 18, 2024

Simple tool for generating high-quality data for any REST API defined in a Swagger/OpenAPI document.

OpenAPI/Swagger specification is the industry standard for REST APIs. Credit to Swagger.io.

Don’t have time to read? Watch the video here instead.

Motivation

When testing out your REST APIs, you normally just quickly run a sanity check from your browser. Chuck in localhost:8080/my/endpoint and hope to see a response. You add in some unit tests to help cover the main use cases. Once you have deployed it to a test environment, you use a tool like cURL or Postman to simulate the required user input/behaviour. Then, if you wanted to load-test your API, you would use a tool like Gatling or JMeter to help you execute them.

After doing this multiple times, you may start to think, is there a better way of doing this? You will notice the following:

  • You test the happy path
  • Data is manually generated
  • Only a small number of combinations are tested
  • Simulating production is difficult
  • Multiple tools used to test different aspects
  • Have not validated data pushed to downstream data sources
  • No test data clean-up

Solution

Swagger (or OpenAPI) specification is the industry standard when sharing the definition of your REST APIs. So why not leverage this when testing your application? This is the contract that consumers must adhere to. It contains all the metadata defining the restrictions on what data can be sent and retrieved from the REST API. Given Data Caterer is a data generation tool that works based on metadata, we only need to feed that metadata in!

Add in a new connection to the OpenAPI/Swagger JSON file location.

Add new OpenAPI/Swagger metadata source. Credit myself.
Petstore example API from Petstore Swagger. Credit myself.

Set up your data generation plan to automatically generate from a metadata source. We point to the petstore-api that we just created.

Define your data generation to automatically retrieve the schema from petstore-api. Credit myself.

Then bam! We have some data coming through.

httpbin docker image. Credit myself.

Conclusion

A quick and simple way to test out your REST APIs should only involve using the Swagger/OpenAPI specification. It contains all the required metadata needed, can be updated easily and best of all, shared with anyone.

For further customisations such as defining relationships between fields, creating data validations based on the generated data or deleting consumed data, check out the website to find out more. Thanks for reading!

Website.

GitHub repo.