digit-soft / laravel-swagger-generator
Swagger yaml generator
Installs: 7 613
Dependents: 0
Suggesters: 0
Security: 0
Stars: 13
Watchers: 4
Forks: 6
Open Issues: 3
Requires
- php: ^8.0
- doctrine/annotations: ^1.6
- laravel/framework: ^7.30.6||^8.75||^9.1.9||^10.0||^11.0
- phpdocumentor/reflection-docblock: ^4.3|^5.0
- symfony/yaml: ^6.1
- dev-master
- v1.x-dev
- 1.5.1
- 1.5.0
- 1.4.11
- 1.4.10
- 1.4.9
- 1.4.8
- 1.4.6
- 1.4.5
- 1.4.4
- 1.4.3
- 1.4.1
- 1.4.0
- 1.3.6
- 1.3.5
- 1.3.4
- 1.3.3
- 1.3.2
- 1.3.1
- 1.3.0
- 1.2.1
- 1.1.23
- 1.1.22
- 1.1.21
- 1.1.20
- 1.1.19
- 1.1.18
- 1.1.17
- 1.1.16
- 1.1.15
- 1.1.14
- 1.1.12
- 1.1.11
- 1.1.0
- 1.0.19
- 1.0.18
- 1.0.17
- 1.0.16
- 1.0.15
- 1.0.14
- 1.0.12
- 1.0.11
- 1.0.10
- 1.0.9
- 1.0.8
- 1.0.7
- 1.0.6
- 1.0.5
- 1.0.4
- 1.0.2
- 1.0.1
- 1.0.0
- 0.5.11
- 0.5.9
- 0.5.8
- 0.5.6
- 0.5.5
- 0.5.3
- 0.4.9
- 0.3.8
- dev-add-license-1
This package is auto-updated.
Last update: 2024-12-20 08:38:10 UTC
README
This package is made to automate API documentation for Swagger (Open Auth 3.0)
Publish config
php artisan vendor:publish --provider="DigitSoft\Swagger\SwaggerGeneratorServiceProvider" --tag="config"
Usage
To generate doc file (YML) run following command:
php artisan swagger:generate
To see problems with generation use diagnose
mode, where file will not be generated, only helpful information will be printed.
php artisan swagger:generate --diagnose
Describing your code
Annotations list
Responses
Responses are parsed only if explicitly documented by @Annotation
. It must be placed in PHPDoc of controller method that route use.
RAW response:
/** * Controller method PHPDoc * * @OA\Response(true,contentType="application/json",description="Boolean response") * * @param Request $request * @return \Illuminate\Http\JsonResponse */
JSON RAW response:
/** * Controller method PHPDoc * * @OA\ResponseJson({"key":"value"},status=201,description="User data response") * * @param Request $request * @return \Illuminate\Http\JsonResponse */
or
/** * Controller method PHPDoc * * @OA\ResponseJson({ * @OA\ResponseParam("key",type="string",example="value",description="Some parameter"), * },status=201,description="User data response") * * @param Request $request * @return \Illuminate\Http\JsonResponse */
Response from class properties:
/** * Controller method PHPDoc * * @OA\ResponseClass("App\User",description="User model response") * * @param Request $request * @return \Illuminate\Http\JsonResponse */
In example above response data will be parsed from App\User
PHPDoc.
@property
descriptions (property name, type and description)@property-read
descriptions (if setwith
property inResponseClass
annotation)@OA\Property
annotations (property name, type, description, example etc.)
@OA\ResponseClass
use cases,
first is standard use but with additional properties
/** * @OA\ResponseClass("App\User",with={"profile"},status=201) */
As items list
/** * @OA\ResponseClass("App\User",asList=true) */
As paged items list
/** * @OA\ResponseClass("App\User",asPagedList=true) */
Error responses
/** * @OA\ResponseError(403) // Forbidden * @OA\ResponseError(404) // Not found * @OA\ResponseError(422) // Validation error */
Request bodies
Request data is parsed from ::rules()
method of FormRequest
class, that used in controller method for the route and it's annotations (@OA\RequestBody
, @OA\RequestBodyJson
, @OA\RequestParam
).
From ::rules()
method we can obtain only name and type of parameter and suggest some example,
but if you want fully describe parameters of request body you must place appropriate annotations in FormRequest
class for route.
Examples
/** * @OA\RequestBodyJson({ * @OA\RequestParam("first_name",type="string",description="User name"), * @OA\RequestParam("email",type="string",description="User email"), * @OA\RequestParamArray("phones",items="string",description="User phones array"), * }) */
Tags
Tags can be defined in Controller class or method that route uses.
Do not use space
in tag names, link with such tag name will be broken in Swagger UI, so better idea to use dash -
or underscore _
, or even just a CamelCased
tag names.
Tags defined in controller will be applied to ALL controller methods.
/** * @OA\Tag("Tag-name") */
Secured
This annotation is used to mark route as secured
, and tells to swagger, that you must provide valid user credentials to access this route.
Place it in controller method.
/** * @OA\Secured() */
Property
@OA\Property
annotation is used to describe class properties as an alternative or addition to PHPDoc @property
.
You can place example of property (if property is an associative array for example)
or fully describe property if you dont want to place @property
declaration for it.
/** * @OA\Property("notification_settings",type="object",example={"marketing":false,"user_actions":true},description="User notification settings") */
PropertyIgnore
@OA\PropertyIgnore
annotation is used to remove given property from object description.
/** * @OA\PropertyIgnore("property_name") */
Symlink
This annotation can be used to describe symlink to another class (e.g. for response). All data in class PHPDoc in which it appears will be ignored.
You must use full namespace of annotations, e.g. OA\Property
.
Besides, you can import a namespace for better code completion as in example beyond.
namespace App\Models; use OA; /** * Test model class * * @OA\Property("id",type="integer",description="Primary key") * * @property string $name String name */ class TestModel {}
More
There is abstract annotation class OA\DescriptionExtender
, you can use it for your own annotations, those must add some information to route's description.
Example is bellow.
<?php namespace App\Components\Annotations\Swagger; use OA\DescriptionExtender; use Doctrine\Common\Annotations\Annotation; use Doctrine\Common\Annotations\Annotation\{Attribute, Attributes}; /** * Describes needed permission * @Annotation * @Attributes({ * @Attribute("value",type="string"), * }) * @package App\Components\Annotations\Swagger */ class Permission extends DescriptionExtender { /** * @inheritdoc */ public function __toString() { return '**Permission:** `' . $this->value . '`'; } }
You can use annotation from example:
<?php use App\Components\Annotations\Swagger as SWA; /** * Controller method PHPDoc * * @OA\ResponseClass("App\User",description="User model response") * @SWA\Permission("articles.can-update") * * @param Request $request * @return \Illuminate\Http\JsonResponse */
and it will add following text into route's description
Description for route goes here...bla-bla-bla...
**Permission:** `articles.can-update`