Download the PHP package savionicodemos/laravel-auto-swagger without Composer
On this page you can find all versions of the php package savionicodemos/laravel-auto-swagger. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package laravel-auto-swagger
Laravel Auto Swagger Docs
Simple to use OAS3 compatible documentation generator.
Also includes Swagger UI.
About
This package is heavily inspired by the Mezatsong/laravel-swagger-docs.
The set of functions that this package has are included:
- OAS3 support (with constant verifications to ensure syntax and compatibility)
- Custom decorators
- Custom responses
- Custom Schemas
- Custom Schema builders
- Automatic Parameters generation based on path and Form Request classes
- Inclusion of Swagger UI with option to select between different UI drivers
- Automatic Schema generations by Models or custom Schema classes.
- Generate operation tags based on route prefix or controller's name
Installation
Install package through composer
Publish configuration files and views
Edit the swagger.php
configuration file for your liking
Usage
Laravel Auto Swagger Docs works based on recommended practices by Laravel. It will parse your routes and generate a path object for each one. If you inject Form Request classes in your controller's actions as request validation, it will also generate the parameters for each request that has them. For the parameters, it will take into account wether the request is a GET/HEAD/DELETE or a POST/PUT/PATCH request and make its best guess as to the type of parameter object it should generate. It will also generate the path parameters if your route contains them. Finally, this package will also scan any documentation you have in your action methods and add it as summary and description to that path, along with any appropriate annotations such as @deprecated.
One thing to note is this library leans on being explicit. It will choose to include keys even if they have a default. For example it chooses to say a route has a deprecated value of false rather than leaving it out. I believe this makes reading the documentation easier by not leaving important information out. The file can be easily cleaned up afterwards if the user chooses to leave out the defaults.
Command line
Generating the swagger documentation is easy, simply run php artisan swagger:generate
in your project root. The output of the command will be stored in your storage path linked in config file.
If you wish to generate docs for a subset of your routes, you can pass a filter using --filter
, for example: php artisan swagger:generate --filter="/api"
You can also configure your swagger.php file to always generate schema when accessing Swagger UI or just by adding this line in your .env: SWAGGER_GENERATE_ALWAYS=true
By default, laravel-swagger prints out the documentation in json format, if you want it in YAML format you can override the format using the --format
flag. Make sure to have the yaml extension installed if you choose to do so.
Format options are:
json
yaml
Annotations syntax
The annotations are written in the PHPDoc block of your controller methods. The syntax is as follows:
The syntax is basically a JSON inside a comment block. Just be aware to follow strictly the JSON syntax.
Rules
- The
key: value
pairs must be separated by a column:
- The
key: value
pairs must be always in the same line. If you want a longer description, you can write it in the comment itself that this description will be used as the description of the route. You can see the example in the@Request()
decorator. - When you need to use an array, you can use the square brackets
[]
to define it as in JSON.
@Request() decorator
You can have only one @Request()
decorator.
@Response() decorator
You can have multiple @Response
decorators
- The
code
property is required and must be the first in property - You can use the optional
description
property to describe your response - You can use the optional
ref
property to refer a Model or a custom Schema, you can also add an[]
in the final of the Schema name to refer an array of that Schema or use the full Schema path inside, finally you can use a schema builder
[!NOTE] You can see all available schema builder or create your own schema builder, explore swagger.schema_builders config for more information
Custom Validators
These validators are made purely for visual purposes, however, some of them can actually do validation. But where to insert it? You can simply insert it in the rules
array in your Form Request class like a normal Laravel validation.
swagger_default
It sets the default value for the parameter in the documentation
swagger_example
It sets the example value for the parameter in the documentation
[!NOTE] The difference between
swagger_default
andswagger_example
is thatswagger_default
will inform to the user that this value is the default value for this parameter inside the application.swagger_example
will just illustrate an example of how the parameter should be filled.
swagger_description
It sets the description for the parameter in the documentation
swagger_min
swagger_max
All versions of laravel-auto-swagger with dependencies
doctrine/dbal Version *
phpdocumentor/reflection-docblock Version ^5.2