Generador de Mock Data OpenAPI
Genera payloads mock JSON realistas desde esquemas OpenAPI con salida reproducible
Generating Mock Data from OpenAPI Specifications
OpenAPI Mock Data Generation creates realistic JSON payloads from your API specification schemas, enabling frontend development, integration testing, and API prototyping without requiring a running backend service. By reading your OpenAPI 3.x or Swagger 2.0 specification, the generator produces response bodies that conform to your defined schemas — respecting types, formats, constraints, enums, and nested object structures — giving development teams working test data immediately.
Unlike generic data generators that produce random values, schema-driven mock generation understands string formats (email, UUID, date-time, URI, IPv4, IPv6), respects min/max constraints on numbers and arrays, generates valid enum values, and maintains referential consistency across nested objects. A configurable seed ensures reproducible output for deterministic test suites that produce identical mock data across runs and environments.
Schema-Driven Data Generation
The generator reads your OpenAPI schema definitions and produces values that match each field's type and format specification:
type: string, format: email→ generates realistic email addressestype: string, format: uuid→ generates valid UUID v4 stringstype: string, format: date-time→ generates ISO 8601 timestampstype: integer, minimum: 1, maximum: 100→ generates integers within rangetype: array, minItems: 2, maxItems: 5→ generates arrays with valid lengthenum: ["active", "inactive", "pending"]→ selects from allowed values
For properties without explicit formats, the generator infers appropriate values from property
names — a field named email generates email-like strings, phone
generates phone number patterns, and url or website generates
URI-formatted strings. This heuristic approach produces more realistic payloads than purely
random string generation.
Reproducible Output with Seeded Randomness
Configurable seed values make mock generation deterministic — the same seed always produces the same output, enabling several important workflows:
- Snapshot testing: Generate mock responses with a fixed seed and compare against stored snapshots. If the schema changes, the snapshot diff reveals exactly which fields changed.
- Cross-team consistency: Frontend and QA teams use the same seed to ensure they work with identical test data, eliminating data-dependent test failures.
- Debugging: Reproduce specific test scenarios by sharing the seed value used during a failed test run, guaranteeing identical mock data for investigation.
- CI stability: Deterministic mocks prevent flaky tests caused by random data that occasionally triggers edge cases differently across runs.
When no seed is specified, the generator uses cryptographic randomness for unique output on each invocation — useful for exploratory testing and discovering how the schema handles varied data.
Use Cases for Mock Data Generation
Schema-driven mock generation serves multiple development workflows:
- Frontend development: Build UI components against realistic response shapes before the backend API is implemented, using generated mocks as temporary data sources.
- API prototyping: Validate your schema design by reviewing generated sample responses — if the mock data looks wrong, the schema definition likely needs adjustment.
- Contract testing: Generate request bodies that conform to your input schemas and verify your API handles them correctly, testing the full range of valid inputs.
- Documentation examples: Populate API documentation with realistic example responses generated directly from the specification, ensuring examples stay in sync with schema changes.
- Load testing: Generate large volumes of valid request payloads for performance testing without manually crafting test data for every endpoint.
Code Examples
OpenAPI Schema and Generated Mock Response
# OpenAPI Schema Definition
components:
schemas:
User:
type: object
required: [id, email, name]
properties:
id:
type: string
format: uuid
email:
type: string
format: email
name:
type: string
minLength: 2
role:
type: string
enum: [admin, editor, viewer]
created_at:
type: string
format: date-time
# Generated Mock (seed: 42):
# {
# "id": "a1b2c3d4-e5f6-4789-abcd-ef0123456789",
# "email": "user_7k2m@example.com",
# "name": "Morgan Blake",
# "role": "editor",
# "created_at": "2024-03-15T09:22:41Z"
# }Using Mock Data in Frontend Development
// mock-api.ts — Development mock using generated data
import mockUsers from './generated/users-seed-42.json';
export async function fetchUsers(): Promise<User[]> {
if (import.meta.env.DEV) {
// Use generated mock data during development
return new Promise(resolve =>
setTimeout(() => resolve(mockUsers), 200)
);
}
// Real API call in production
const res = await fetch('/api/users');
return res.json();
}Standards & Specifications
- OpenAPI Specification 3.1 — Defines schema objects and data types that the mock generator interprets