You may find the solution introduced in How to Mock an API in ONE Minute helpful, as many other developers have. This article aims to introduce an approach for quickly mocking an API. However, it is important to note that the previous solution was only a small part of the tool. Upon further exploration, I discovered additional valuable features that can help you mock complex data even faster.
The first part to talk about is how to import a Swagger API into Apidog to mock some JSON data. Then you can have your mock data directly without any scripts.
But some fields are complicated; you can't mock them without any settings. For example, an enum field has several optional values. It usually appears in status fields. Or an array with a fixed length. It's often used in a table where each row has several attributes, and each page has 20 or 50 rows. Or, an object refers to a schema. OpenAPI specification allows an API to refer to a schema as an object or part of an object.
Luckily, now we can quickly mock all these complex JSON in two minutes in Apidog.
How to Mock Complex APIs Quickly
There are three steps to mock the API.
- Import API definitions.
- Set specific fields.
- Get the mock URL.
So easy. Let's mock it!
Step 1. Import API Definitions.
You just need an OpenAPI JSON or YAML and import it into Apidog.
Step 2. Set Specific Fields
In this sample API, the 'status' field has three optional values: available, pending, and sold. So I hope this field in my mocking data has only one of these values too.
Click 'More' in the 'status' field. You'll see an 'Enum' switch. Turn it on, and you'll be able to add enum values. After that, data of this field will be mocked according to the settings.
Arrays with fixed length
PhotoUrls is an array field. I need it to have a fixed five items.
Click 'More' in the 'PhotoUrls' field. You'll see an 'Element Limit' setting. Set the minItems 5 and maxItems 5.
Object refers to a schema
Apidog support schemas in OpenAPI specification. So you just need to select a Schema for an object, and Apidog will mock the object according to the schema.
For example, the 'Category' field.
Step 3. Get the Mock URL
Click ‘Save', and switch to the 'API' tab. Copy the URL in the "Local Mock" part which starts with "127.0.0.1".
Paste the URL into the browser.
Notice these fields: photoURLs with fixed item quantity(5), while URLs are mocked automatically with available image URLs; status with enum values(sold, pending, or available), and category refers to a schema. The other fields, like city and createdAt are mocked automatically.
And if you reload the URL, the mock data refreshes.
OpenAPI 3.0 data types are based on JSON Schema, which describes the data structure of APIs.
Apidog is compatible with OpenAPI. Length range, format, and pattern(regular expression) of strings, min and max value of integers, readonly/writeonly; all attributes in JSON Schema are supported.
Not only JSON but XML is also supported to mock. You can even set the tag names and prefixes of XML data.
Based on JSON Schema, any complex mock data can be generated easily with a few mouse clicks. Still, you don't need to write any scripts.
Just enjoy it.
Download Apidog here, and wait for my next post to see more powerful mock features.