How it Works
The generator leverages the existing framework infrastructure to discover and process your API metadata:- Route Discovery: It uses the
Routerto 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 and Components
TheComponentsObject is where you define reusable elements for your OpenAPI specification, such as security schemes, common schemas, or parameters.
Automatic Security Scheme Generation
If you use security requirements in your routes but haven’t defined them in theComponentsObject, the OpenAPIGenerator will attempt to automatically generate a base authorization part for you:
- If the scheme name contains “bearer”, it defaults to an
httptype with abearerscheme. - If the scheme name contains “oauth2”, it defaults to an
oauth2type with a basic password flow. - If the scheme name contains “fido”, it defaults to an
apiKeytype in theheader. - Otherwise, it defaults to an
apiKeytype in theheader.
Best Practice: Explicit Definition
While automatic generation works for simple cases, it is highly recommended to explicitly define your security schemes when initializing theOpenAPIGenerator.
By defining your own SecuritySchemeObject in the ComponentsObject, you gain full control over the documentation. This is especially important for complex schemes like OAuth2, where you can define OAuthFlows (authorization URLs, token URLs, scopes). This allows tools like Swagger UI to provide a functional “Authorize” button that works instantly with your authentication provider.
Crucially, the name you use in your routes (RouteAuthorization) must match the key used in ComponentsObject::setSecuritySchemes().
Keep in mind: Future Auto-Discovery. Apivalk is moving towards full auto-discovery of security schemes. In the future, it will be able to automatically detect and document your security configuration (including names and versions) directly from your middleware and authenticators.
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.