How it Works
The generator leverages the existing framework infrastructure to discover and process your API metadata:- Route Discovery: It uses the
Routerand itsRouterCacheto find all registered routes and their corresponding controller classes. - Metadata Extraction: For each discovered route, it calls:
AbstractApivalkController::getRequestDocumentation()AbstractApivalkController::getResponseDocumentation()
- Object Mapping: It maps the Apivalk
Propertysystem to OpenAPI objects (Schemas, Parameters, RequestBodies, Responses). - Serialization: It assembles these objects into a final
OpenAPIobject and serializes it to JSON.
Core Logic
The OpenAPI Object
The OpenAPI class represents the root of the specification. It contains sub-objects for:
InfoObject: API title, version, description.ServerObject: Base URLs for your API.PathsObject: Map of endpoints and their methods.ComponentsObject: Reusable schemas and security schemes.
The Generation Process
TheOpenAPIGenerator coordinates several specialized generators:
PathsGenerator: Processes the list of routes.PathItemGenerator: Handles a single URL (which may have multiple HTTP methods).OperationGenerator: Handles a single HTTP method (GET, POST, etc.) on a path.ParameterGenerator: Converts query and path properties to OpenAPI parameters.RequestBodyGenerator: Converts body properties to a JSON request body schema.ResponseGenerator: Converts response documentation to OpenAPI response schemas.
Usage Example
Security Schemes
You can also define security requirements (like Bearer Auth) by adding aSecuritySchemeObject to the ComponentsObject passed to the generator’s constructor.
Automated Pagination Envelope
If a response documentation hassetHasResponsePagination(true), the generator automatically wraps the properties in a standard pagination envelope containing data, total, page, limit, etc.