OpenAPI Format
Learn how to use OpenAPI (Swagger) specifications with moclojer. Import your OpenAPI 3.0/3.1 specs and generate mocks automatically.
Moclojer supports OpenAPI 3.0 and 3.1 (formerly known as Swagger), allowing you to use your existing API specifications to generate mocks automatically. No need to rewrite anything in moclojer YAML!
What Is OpenAPI?
OpenAPI Specification is a standard for describing RESTful APIs in a machine-readable format. It's widely used for:
API documentation
Automatic SDK generation
Contract validation
Mock servers (like moclojer!)
Why Use OpenAPI with Moclojer?
β Reusability: Use your existing OpenAPI specs β Standardization: OpenAPI is an industry standard β Zero configuration: Moclojer converts automatically β Validation: OpenAPI specs include validation schemas β Living documentation: Your spec is documentation + mock
OpenAPI Support in Moclojer
Supported Versions
β OpenAPI 3.0.x
β OpenAPI 3.1.x
β οΈ Swagger 2.0 (partial support - recommend converting to 3.x)
Accepted Formats
β JSON (
.json)β YAML (
.yml,.yaml)
Quick Start
1. Create or Get an OpenAPI Spec
Minimal example (openapi.yml):
2. Start Moclojer with OpenAPI
3. Test
Response:
π Done! Moclojer automatically converted your OpenAPI spec into functional endpoints.
How Moclojer Converts OpenAPI
Automatic Conversion
Moclojer automatically detects it's an OpenAPI spec and converts:
Results in (internal moclojer equivalent):
Path Parameters
OpenAPI uses {param}, moclojer converts to :param:
Converted to: /users/:userId/posts/:postId
With types:
type: integerβ:userId|inttype: stringβ:userId|string
Query Parameters
Moclojer understands and accepts:
Request Body
Moclojer uses the example for the response.
Complete OpenAPI Examples
Example 1: Simple User API
Test:
Example 2: E-commerce API
Multiple Responses by Status Code
OpenAPI allows defining multiple responses:
Moclojer uses the first example found (usually 200).
To simulate errors, create specific endpoints:
Headers and Content-Type
OpenAPI defines headers automatically:
Moclojer automatically adds:
Content-Typebased oncontentCustom headers defined in
headers
Schemas and $ref
OpenAPI uses $ref to reuse schemas:
Moclojer resolves $ref and uses the provided example.
Security Schemes (Authentication)
OpenAPI defines security schemes:
Note: Moclojer does not validate authentication automatically. To simulate:
Useful Tools
OpenAPI Editors
Swagger Editor (online)
Validates spec in real-time
VS Code Extension
"OpenAPI (Swagger) Editor" by 42Crunch
Autocompletion and validation
Stoplight Studio
Visual editor
Spec Validation
Spec Generation
Limitations and Workarounds
1. Request Validation
Limitation: Moclojer does not validate requests against the schema.
Moclojer accepts any request, even without name.
Workaround: Use tools like Prism for real validation.
2. Multiple Responses by Status
Limitation: Moclojer uses only one example per endpoint.
Workaround: Create separate endpoints for each status code.
3. Callbacks and Links
Limitation: OpenAPI 3.x supports complex callbacks and links, moclojer ignores them.
4. oneOf, anyOf, allOf
Limitation: Complex schemas are not processed.
Workaround: Use explicit example.
OpenAPI vs Native Moclojer YAML
Standard
Industry (portable)
Moclojer-specific
Verbosity
More verbose
More concise
Tools
Many (editors, validators)
Few
Documentation
Spec = documentation
Mock only
Dynamic
Static examples
Dynamic templates
Validation
Schema validation (with tools)
None
Learning curve
Higher (complex spec)
Lower (simple YAML)
When to use OpenAPI:
Already have existing OpenAPI specs
Want to generate SDKs/documentation
Need standardization across teams
API goes beyond mocks (production)
When to use native YAML:
Want dynamic responses with templates
Need quick and simple mocks
Don't need portability
Want minimalist configuration
Combining OpenAPI + Moclojer YAML
You can use both in the same project:
Start with multiple files:
Alternative: Convert OpenAPI to moclojer YAML:
Best Practices
β
Do
Use
examplesin all endpointsDefine path parameter types
Organize with tags
Use
$refto reuse
β Avoid
Specs without
examplesComplex paths without types
Troubleshooting
"OpenAPI spec not detected"
Cause: Missing required openapi field.
Solution:
"No examples found"
Cause: Spec without example or examples.
Solution: Add explicit examples:
"Path parameters not working"
Cause: Incorrect syntax.
Solution:
Next Steps
Postman Format - Use Postman Collections
YAML Format - Native moclojer syntax
Path Parameters - Dynamic parameters
HTTP Methods - GET, POST, PUT, DELETE
See Also
Last updated
Was this helpful?