OpenAPI and Swagger¶
The OpenAPI Specification (OAS) is a language-independent standard for describing HTTP APIs. An OpenAPI document can define paths, operations, parameters, request bodies, responses, schemas, authentication methods, and reusable components in JSON or YAML.
Swagger is a family of tools associated with OpenAPI. Examples include:
- Swagger UI: interactive HTML documentation;
- Swagger Editor: editing and validation of OpenAPI documents;
- Swagger Codegen: generation of client libraries and server stubs.
The terms are related but not interchangeable: OpenAPI is the specification, while Swagger refers primarily to tooling.


How Does OpenAPI Support Testing?¶
Interactive Examination¶
Swagger UI can send HTTP requests from the browser and display response bodies, headers, and status codes. This is useful for exploration and demonstrations, but manually pressing Execute is not a repeatable automated test.
Contract-Based Testing¶
An OpenAPI document can serve as a machine-readable API contract. Testing tools can use it to check whether:
- requests use declared paths, parameters, and schemas;
- responses use documented status codes and content types;
- response bodies conform to declared schemas;
- required fields and validation constraints are respected.
The contract must itself be reviewed. A response can conform perfectly to an incorrect or incomplete OpenAPI document.
Test Generation¶
Tools can derive candidate inputs and tests from the contract, including:
- valid requests;
- omitted required fields;
- invalid types or formats;
- boundary values;
- undocumented status codes;
- schema-violating responses.
Code generators primarily create clients and server stubs. They do not by themselves prove that the API behaves correctly; meaningful assertions still require requirements and test oracles.
Change Detection and CI/CD¶
An OpenAPI diff tool can compare two contract versions and report changes such as removed fields, new required parameters, or modified response schemas. CI pipelines may combine:
- syntax and schema validation;
- compatibility analysis;
- generated-client compilation;
- contract and integration tests;
- publication of API documentation.
Compatibility is contextual
Not every textual difference is breaking, and some behavioural breaking changes are invisible in the schema. Compatibility decisions must consider actual clients, versioning rules, and documented semantics.
FastAPI Exercise¶
FastAPI derives an OpenAPI description from Python type annotations, route declarations, validation rules, and metadata. It normally exposes Swagger UI and ReDoc automatically.
OpenAPI and FastAPI
Create and activate a virtual environment:
1 2 3 4 5 | |
On Windows PowerShell, activate it with:
1 | |
Install FastAPI and Uvicorn:
1 | |
Create main.py:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | |
Run the application:
1 | |
Here, main is the module name and app is the FastAPI object.
Open:
- Swagger UI: http://127.0.0.1:8000/docs
- ReDoc: http://127.0.0.1:8000/redoc
- OpenAPI JSON: http://127.0.0.1:8000/openapi.json
Use Swagger UI to call both an existing and a missing user:
GET /users/1should return HTTP 200;GET /users/999should return HTTP 404;GET /users/0should fail path-parameter validation.
What is Uvicorn?
Uvicorn is an ASGI server implementation used to run asynchronous Python web applications. FastAPI is the web framework; Uvicorn is the server that hosts the application.
What is FastAPI?
FastAPI is a Python web framework for building APIs using standard type annotations. It can generate an OpenAPI description and interactive documentation from application declarations.
Spring Boot Exercise¶
The springdoc-openapi library can derive an OpenAPI description from Spring configuration, controller structure, validation constraints, and annotations, and can provide Swagger UI.
OpenAPI and Spring Boot
Create a Maven project with a current compatible Spring Boot release and Java 17 or later. Add:
spring-boot-starter-web;spring-boot-starter-validation;spring-boot-starter-test;- a
springdoc-openapi-starter-webmvc-uiversion compatible with the selected Spring Boot version.
At the time of this material's revision, Spring Boot 4 projects use the springdoc-openapi 3.x line. Always check the springdoc compatibility information when changing either dependency.
The springdoc dependency for a current Spring Boot 4 project is:
1 2 3 4 5 | |
Application entry point:
1 2 3 4 5 6 7 8 9 10 11 12 | |
Create UserController.java:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | |
Run the application and open:
- Swagger UI: http://localhost:8080/swagger-ui.html
- OpenAPI JSON: http://localhost:8080/v3/api-docs
- OpenAPI YAML: http://localhost:8080/v3/api-docs.yaml
Verify at least:
GET /api/usersreturns HTTP 200 and an array;- a valid
POST /api/usersreturns HTTP 201; - a missing name or invalid email is rejected with HTTP 400.
Generated documentation is not automatically complete
Frameworks can infer paths and schemas, but they cannot infer every business rule, security requirement, example, or semantic guarantee. Generated OpenAPI documents must therefore be reviewed and tested like other development artefacts.