zircote/swagger-php
swagger-php generates OpenAPI 3.0/3.1/3.2 documentation from your PHP 8.2+ code using attributes (preferred) or optional Doctrine annotations. Use it via CLI or programmatically, with helpful error reporting and a full documentation site.
The OpenAPI specification defines a minimum set of information for a valid document.
For the most part that consists of some general information about the API like name, version
and at least one endpoint.
The endpoint, in turn, needs to have a path and at least one response.
With the above in mind, a minimal API with a single endpoint could look like this:
<<< @/snippets/minimal_api_at.php
<<< @/snippets/minimal_api_an.php
with the resulting OpenAPI document like this
<<< @/snippets/minimal_api.yaml
::: warning Where to add annotations? Attributes and annotations can be added anywhere in your codebase as long as they are associated with structural elements (classes, methods, properties, etc.). Effectively, annotations can be added anywhere in your code where PHP reflection can find them. :::
Looking at the generated document, you will notice that there are some elements that swagger-php adds automatically
when they are missing.
For the most part those are [@OA](https://github.com/OA)\OpenApi, [@OA](https://github.com/OA)\Components and [@OA](https://github.com/OA)\PathItem. These are defined by the OpenAPI specification
for grouping and as give the generated JSON/Yaml some more structure.
Unless there are specific reasons to add them manually, you should not need to add them yourself.
Your annotations should be placed next to your application code. The idea is that you should be able to see your annotations right next to the code they describe.
This also has the advantage of making it easier to keep both in sync.
swagger-php will scan your project and merge all meta-data into one [@OA](https://github.com/OA)\OpenApi annotation.
::: warning
As of swagger-php v4 all attributes and/or annotations must be associated with
a structural element (class, method, parameter or enum)
:::
How can I help you explore Laravel packages today?