OpenAPI as a tested contract
Describe the interface consumers can rely on.
The idea
An API description is valuable when it matches runtime behavior. Document the booking request, successful response, validation error, authentication scheme, and conflict cases. Review generated OpenAPI changes with the same care as code changes: removing a response property can break clients.
Follow the flow
Make it concrete
This excerpt isolates the concept. Connect it to the booking API as you work through the exercise.
builder.Services.AddOpenApi();
// Map the document only in the intended environment.
if (app.Environment.IsDevelopment())
app.MapOpenApi();
app.MapGet("/bookings/{id:guid}", GetBooking)
.Produces<BookingResponse>(200)
.Produces(404);Decisions that matter
- Specification generation documents known metadata; it does not prove the implementation conforms.
- Keep internal administrative endpoints out of a public document when required.
- Pin the toolchain used to generate clients for repeatable builds.
Your turn
A generated client expects a 200 response, but the endpoint starts returning 204. What checks would catch the change?
Reveal a worked answer
Diff the generated specification, exercise the endpoint in a contract test, and compile or run a representative consumer. Decide whether the new status is a compatible change before releasing it.