API / Swagger Interview questions
How do you mock an API server using an OpenAPI specification?
Because an OpenAPI document already fully describes every endpoint's expected request and response shapes, a mock server can be generated directly from it — returning example or schema-generated responses for each operation — without any real backend logic behind it, which is especially useful for frontend teams needing to start integration before the real API exists.
# Example using Prism, a popular OpenAPI mock server prism mock openapi.yaml
A mock server built this way inspects each incoming request, matches it against a path and operation in the spec, and returns a response built from that operation's documented examples if present, or a value auto-generated to satisfy the response schema's constraints if no explicit example was provided.
Some mock tools go further and support dynamic behavior driven by request content — returning a documented 404 response when a path parameter matches a specific "not found" test value, or validating that incoming requests actually conform to the spec and rejecting ones that don't — which is useful for catching integration bugs on the consuming side even before the real backend exists to catch them.
More Related questions...