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|int

  • type: 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-Type based on content

  • Custom 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

  1. Swagger Editor (online)

  2. VS Code Extension

    • "OpenAPI (Swagger) Editor" by 42Crunch

    • Autocompletion and validation

  3. Stoplight Studio

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.

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

Aspect
OpenAPI
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

  1. Use examples in all endpoints

  2. Define path parameter types

  3. Organize with tags

  4. Use $ref to reuse

❌ Avoid

  1. Specs without examples

  2. Complex 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

See Also

Last updated

Was this helpful?