API / Swagger Interview questions
How do you document request and response examples in OpenAPI?
OpenAPI lets you attach concrete example values to a schema, parameter, request body, or response, giving consumers a realistic sample of actual data alongside the abstract type/shape description — useful because a schema alone doesn't always convey what "realistic" values actually look like.
responses: '200': content: application/json: schema: $ref: '#/components/schemas/User' examples: basicUser: summary: A typical active user value: id: 42 email: jane@example.com status: active suspendedUser: summary: A suspended account value: id: 43 email: bob@example.com status: suspended
OpenAPI supports two related mechanisms: a single example field for one sample value, or an examples map (as shown above) for multiple named examples covering different scenarios, which Swagger UI renders as a selectable dropdown so a consumer can inspect several realistic variations of the same response.
Providing multiple named examples is especially valuable for polymorphic or conditionally-shaped responses — showing what a response looks like in both a success and an edge-case scenario communicates far more than the schema's type constraints alone ever could.
More Related questions...