What is openapi-sampler?
The openapi-sampler npm package is used to generate sample data from OpenAPI specifications. It helps in creating mock data for API responses based on the defined schemas in the OpenAPI document.
What are openapi-sampler's main functionalities?
Generate Sample from Schema
This feature allows you to generate a sample object based on a given JSON schema. The sample data will adhere to the types and examples specified in the schema.
const openapiSampler = require('openapi-sampler');
const schema = {
type: 'object',
properties: {
id: { type: 'integer', example: 1 },
name: { type: 'string', example: 'John Doe' }
}
};
const sample = openapiSampler.sample(schema);
console.log(sample);
Generate Sample from OpenAPI Document
This feature allows you to generate sample data directly from an OpenAPI document. It extracts the schema from the specified path and response, and generates a sample based on it.
const openapiSampler = require('openapi-sampler');
const openapiDoc = {
openapi: '3.0.0',
info: { title: 'Sample API', version: '1.0.0' },
paths: {
'/user': {
get: {
responses: {
'200': {
description: 'A user object',
content: {
'application/json': {
schema: {
type: 'object',
properties: {
id: { type: 'integer', example: 1 },
name: { type: 'string', example: 'John Doe' }
}
}
}
}
}
}
}
}
}
};
const sample = openapiSampler.sample(openapiDoc.paths['/user'].get.responses['200'].content['application/json'].schema);
console.log(sample);
Other packages similar to openapi-sampler
swagger-mock-api
The swagger-mock-api package is used to create a mock server based on Swagger (OpenAPI) definitions. It allows you to simulate API endpoints and responses for testing purposes. Unlike openapi-sampler, which focuses on generating sample data, swagger-mock-api provides a full mock server implementation.
swagger-jsdoc
The swagger-jsdoc package is used to generate Swagger (OpenAPI) documentation from JSDoc comments in your code. While it does not generate sample data, it helps in creating and maintaining OpenAPI documentation, which can then be used with tools like openapi-sampler to generate sample data.
json-schema-faker
The json-schema-faker package generates fake data based on JSON Schema definitions. It is similar to openapi-sampler in that it generates sample data, but it is more focused on JSON Schema rather than OpenAPI specifically. It offers more customization options for generating fake data.
openapi-sampler
Tool for generation samples based on OpenAPI payload/response schema
Features
- deterministic (given a particular input, will always produce the same output)
- Supports
allOf
- Supports
additionalProperties
- Uses
default
, example
and enum
where possible - Full array support: supports
minItems
, and tuples (items
as an array) - Supports
minLength
, maxLength
, min
, max
, exclusiveMinimum
, exclusiveMaximum
- Supports the next
string
formats:
- email
- password
- date-time
- date
- ipv4
- ipv6
- hostname
- uri
- Infers schema type automatically following same rules as json-schema-faker
- Support for
$ref
resolving
Installation
Install using npm
npm install openapi-sampler --save
Then require it in your code:
var OpenAPISampler = require('openapi-sampler');
Usage
OpenAPISampler.sample(schema, [options], [spec])
- schema (required) -
object
A OpenAPI Schema Object - options (optional) -
object
Available options:
- skipReadOnly -
boolean
Don't include readOnly
object properties - skipWriteOnly -
boolean
Don't include writeOnly
object properties
- spec - whole specification where the schema is taken from. Required only when schema may contain
$ref
. spec must not contain any external references
Example
const OpenAPISampler = require('.');
OpenAPISampler.sample({
type: 'object',
properties: {
a: {type: 'integer', minimum: 10},
b: {type: 'string', format: 'password', minLength: 10},
c: {type: 'boolean', readOnly: true}
}
}, {skipReadOnly: true});