Swagger/OpenAPI in testing
OpenAPI is a machine-readable YAML or JSON format for describing an HTTP API: paths, methods, parameters, schemas and response codes. Swagger is the set of tools around it, such as Swagger UI, and the old name of the specification before version 3.0. QA reads the specification as requirements and logs a mismatch with the server as a defect once it is clear which side is right.
API · Contracts
The docs say 400 but the server returns 422. Who is right, and why is it a bug anyway?
Swagger UI says an invalid POST /v1/transfers returns 400. You send a negative amount and get 422. Then a page loads a transfer whose status is reversed, while the documentation lists three values. Nothing crashed, but a front end written from the specification does not know the fourth status. The specification and the server have drifted apart, and QA is often first to see it.
This lesson covers what OpenAPI and Swagger are, how to read a specification as requirements, and what to do when it lies.
What it is and why it is built this way
OpenAPI (OAS) is a YAML or JSON format for describing an HTTP API: paths, methods, parameters, body schemas, response codes, authorisation. Swagger is the name of the toolset around it (Swagger UI, Swagger Editor, Codegen) and the old name of the specification before version 3.0. So "Swagger documentation" in practice means "an OpenAPI file shown by Swagger UI".
The idea is that the contract is machine-readable. A description that lives only in Confluence is read and understood differently by every person. A file can automatically become an interactive page, a request collection, types, mocks and response checks.
One file, many consumers
The skeleton of an OpenAPI 3.x document: info (name, version), servers, paths (operations) and components (shared schemas, parameters, security schemes referenced through $ref). Version 3.1 aligned schemas with JSON Schema 2020-12, so nullable was replaced by a type array:
# OpenAPI 3.0
note: { type: string, nullable: true }
# OpenAPI 3.1 (JSON Schema 2020-12)
note: { type: [string, 'null'] }
Real projects still contain both 3.0 and 3.1, so check the openapi: field at the top of the file before wiring up a validator.
Example: specification against reality
A fragment of the specification for creating a transfer:
paths:
/v1/transfers:
post:
summary: Create a transfer
requestBody:
required: true
content:
application/json:
schema: { $ref: '#/components/schemas/TransferRequest' }
responses:
'201':
description: Created
content:
application/json:
schema: { $ref: '#/components/schemas/Transfer' }
'400':
description: Invalid request
components:
schemas:
Transfer:
type: object
required: [id, status, amount]
properties:
id: { type: string }
status: { type: string, enum: [pending, completed, failed] }
amount: { type: number }
QA work starts with comparison. An invalid request:
POST /v1/transfers HTTP/1.1
Content-Type: application/json
{ "toAccount": "UA21...", "amount": -5, "currency": "UAH" }
HTTP/1.1 422 Unprocessable Content
Content-Type: application/problem+json
The docs say 400, the server gives 422 and problem+json. A second request shows a data mismatch:
GET /v1/transfers/tr_77 HTTP/1.1
HTTP/1.1 200 OK
Content-Type: application/json
{ "id": "tr_77", "status": "reversed", "amount": 150 }
The schema has three status values, the response has a fourth. How to read it: two mismatches of different weight. The first concerns an error code; a client usually reacts to the 4xx class, so the risk is moderate. The second is more dangerous: a client handling only known values does not know what to show.
What next. First find out which side is right: ask the endpoint author or check the requirements. If the server is right, the bug is in the documentation (fix the specification and tell the consumers). If the documentation is right, the bug is in the code. Both cases are reported, because clients are written from the specification. Do not quietly adjust the test to the real behaviour: that hides the problem.
Limits and alternatives
- A specification can be incomplete. Often only the success response is described, with no errors, headers or pagination. That is a finding in itself (a linter catches it with a custom rule: Spectral's built-in ruleset only requires a 2xx or 3xx), but testing from such a specification means skipping everything undescribed.
- Code-first and design-first. In the first approach the file is generated from code (annotations, the framework); it is more exact about syntax but may describe things nobody intended. In the second the specification is written before the code and it really is the requirement. This decides whom to ask to fix a mismatch.
- Swagger UI "Try it out" suits manual exploration but is not regression: nothing is saved. For repeatable checks import the file into Postman (Import) or automate it in tests.
- Other formats. GraphQL has a schema (SDL) and introspection, gRPC has
.proto, asynchronous APIs are described with AsyncAPI. The principle is the same: a machine-readable contract.
Typical mistakes
- Treating the specification as truth without checking. People write the file too, and it goes stale. Consequence: tests are green against an outdated description while clients break.
- Not reporting a mismatch because "it is only documentation". Front ends and partner integrations are written from it.
- Checking only the success responses from the documentation. 4xx codes and their format are part of the contract too.
- Mixing up
requiredandnullable. A field can be required and still benull, or optional and not arrive at all. These are different cases and the specification must tell them apart.
Check yourself
The specification says an invalid request gives 400. The server returns 422. What is the next step?
Answer
Find out which side is right and report a bug in the right place: in the code if the rules describe 400, or in the documentation if 422 was intended. The test is not quietly adjusted to the real behaviour.
What is the difference between Swagger and OpenAPI?
Answer
OpenAPI is the specification (the format for describing an API). Swagger is the toolset around it, and also the old name of the specification before version 3.0.