OpenAPI Integration
This document explains what OpenAPI is, how the reference architecture uses it, and how to keep the spec in sync with your code.
What Is OpenAPI?
OpenAPI (formerly Swagger) is a language-agnostic specification for describing HTTP APIs. A single openapi.json file documents every endpoint, its parameters, request bodies, and response schemas. This single source of truth drives:
- Runtime routing –
OpenApiRouteListreadsopenapi.jsonto map incoming requests to controller methods. - Swagger UI – An interactive browser UI is served at
http://localhost:8080/docs/so you can explore and test endpoints without a separate tool. - Contract testing –
OpenApiValidation(viaByJG\ApiTools) asserts that every test response conforms to the declared schema.
How swagger-php Scans src/
The project uses zircote/swagger-php to scan PHP attribute annotations in src/ and produce public/docs/openapi.json.
Run the generator whenever you add or change annotations:
APP_ENV=dev composer run openapi
The composer script invokes swagger-php with the src/ directory as the scan target. The output is written to public/docs/openapi.json.
The Generated File
public/docs/openapi.json is generated and should be committed to version control so that:
- The application can boot without running the generator.
- Tests can validate responses even in CI environments.
- Swagger UI always has an up-to-date spec to render.
Swagger UI
When the dev server is running, open http://localhost:8080/docs/ to browse the interactive API documentation. Every endpoint, parameter, and schema defined in openapi.json is rendered with a built-in "Try it out" feature.
Contract Testing Overview
BaseApiTestCase mixes in OpenApiValidation, which intercepts every sendRequest() call and validates the HTTP response against the schema declared in openapi.json. This means:
- A missing required field in the response body causes the test to fail.
- An undocumented status code causes the test to fail.
- Schema drift between code and spec is caught automatically in CI.
See Testing for practical examples.
Keeping the Spec in Sync
Whenever you change PHP attribute annotations in src/, rerun APP_ENV=dev composer run openapi. The regenerated public/docs/openapi.json is what OpenApiRouteList reads at runtime and what the tests validate against.
A common workflow:
- Add or update
#[OA\Get],#[OA\Post], etc. attributes in a controller. - Run
APP_ENV=dev composer run openapito regenerate the spec. - Run
APP_ENV=test composer run testto verify the new endpoint passes contract tests. - Commit both the updated controller and the regenerated
openapi.json.
Related Documentation
- REST Controllers - Defining routes with PHP attributes
- Testing - Contract testing with OpenApiValidation
- Request Lifecycle - How the spec drives runtime routing