PHPackages                             asseco-voice/laravel-open-api - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. asseco-voice/laravel-open-api

ActiveLibrary[API Development](/categories/api)

asseco-voice/laravel-open-api
=============================

Laravel OpenAPI generator

v3.0.0(2y ago)116.4k2[1 issues](https://github.com/asseco-voice/laravel-open-api/issues)[4 PRs](https://github.com/asseco-voice/laravel-open-api/pulls)MITPHPPHP ^8.1

Since Feb 10Pushed 1y ago6 watchersCompare

[ Source](https://github.com/asseco-voice/laravel-open-api)[ Packagist](https://packagist.org/packages/asseco-voice/laravel-open-api)[ RSS](/packages/asseco-voice-laravel-open-api/feed)WikiDiscussions master Synced yesterday

READMEChangelogDependencies (7)Versions (14)Used By (0)

[![](https://github.com/asseco-voice/art/raw/main/evil_logo.png)](https://see.asseco.com)

Laravel OpenApi generator
=========================

[](#laravel-openapi-generator)

This package provides painless OpenApi YML generation from existing routes.

The idea is to have as little work to do as possible in order to generate the API documentation, so the package will try to assume a lot of things such as models from controller names, request and response parameter based on actual tables etc.

For custom inputs/outputs, options will be provided.

Installation
------------

[](#installation)

Install the package through composer. It is automatically registered as a Laravel service provider.

`composer require asseco-voice/laravel-open-api`

Usage
-----

[](#usage)

Running the command `php artisan asseco:open-api` will generate a new `.yml`file at `project_root/open-api.yml`.

What is covered out-of-the-box:

- read all routes
- inferring model name from controller name
- group (tag) them automatically
- get title and description from
- generate request and response parameters

For cases not covered by this convention, refer to [overriding defaults section](#overriding-defaults).

For additional tweaking, refer to [config](#config).

Depending on number of routes, first run may take a few seconds as it is requesting a DB schema for each model it can find. This is cached, so every subsequent run will run much faster.

### Simple out-of-the-box example

[](#simple-out-of-the-box-example)

Given the controller:

```
class UserController extends Controller
{
    /**
     * Store a newly created resource in storage.
     *
     * Create new User object and store it in DB.
     */
    public function store(Request $request): JsonResponse
    {
        $user = User::query()->create($request->all());

        return response()->json($user);
    }

    ...

```

- command will infer `User` as being the main model for the controller
- title: `Store a newly created resource in storage.`
- description: `Create new User object and store it in DB.`
- request data: `User` model table attributes without `id`, `created_at`, `updated_at` attributes
- response data: complete `User` model table attributes

Overriding defaults
-------------------

[](#overriding-defaults)

Custom cases are handled through controller and method annotations in doc blocks:

```
/**
 * My controller doc block
 *
 * @annotation random annotation
 */
class MyController extends Controller
{
    /**
     * My method doc block
     *
     * @annotation another random annotation
     */
    public function index()
    {
        ...
    }

   ...
}

```

### Groups (tags)

[](#groups-tags)

When talking about 'groups', we are actually talking about OpenApi 'tags'.

By default, command will take the controller name, remove `Controller` from it and split `PascalCase` with spaces (i.e. `SysUserController` results in `Sys User` group name).

- `@group` within a controller doc block will override default group.
- `@group` within a method doc block will override default group and controller group making it an operator with the highest precedence.

It is possible to stack multiple group annotations.

### Models

[](#models)

Model is used to try to automatically generate inputs and outputs for standard Laravel CRUD functions.

**Input (request):** model DB schema with either only fillable properties or except guarded. Fillable properties have precedence, guarded will be ignored if fillable exists.

**Output (response):** complete model DB schema without hidden fields.

It is completely valid to have no model associated. In this case, no automatic actions will be performed which require an existing model class.

By default, model name is extracted from controller name. To change this behavior, you have few options:

- Map a specific controller to a specific model. See [config](#config) for details.
- Include `@model` tag within a controller:
    - Specifying namespaced model will use that model `@model My\Namespaced\Model`
    - Not specifying the namespace will use controller's namespace `@model Model`

Controller tag has higher precedence over configuration mapping. If both exist, and controller tag fails, configuration will try to fetch the model as well. Failing on both fronts will result in model being `null`.

It is possible to exclude part of the model for the request:

- `@exclude attribute1 attribute2` is a space separated list of specified model attributes which will not be included in request data.

### Path parameters

[](#path-parameters)

By default, path parameter(s) will be set as integer (assuming most of the path parameters are model IDs). In case you want to use UUIDs, this can be changed in `asseco-open-api` config file by setting `service_uses_uuid` to true.

Override them by including the following in the method doc block:

- `@path` will override what is fetched by default. You must provide it in the following convention `@path name type description` where:
    - `name` - parameter name.
    - `type` - [OpenApi data type](https://swagger.io/docs/specification/data-models/data-types/).
    - `description` - text which will be set as parameter description (not required, empty by default, so it can be omitted).

Examples:

```
@path name type
@path name type Some description

```

It is not possible to set path parameter `required` property. It is automatically set to true because OpenApi doesn't support optional path parameters (even though Laravel does).

### Operation ID

[](#operation-id)

By default, `operationId` will be generated for each route, based on the controller/HTTP method mapping and the name of the model. If you want to override the default, you can use `@operationId`to provide a different suffix to the operation ID (which will replace what would be an auto-generated model).

Prepended controller/HTTP method will stay intact because of possible conflicts when using update routes which map to both PUT and PATCH verbs.

Example:

```
// Provided for an show() method:
@operationId suffix
// this will output getSuffix

// Provided for an update() method:
@operationId suffix
// this will output putSuffix and patchSuffix

```

### Request/response parameters

[](#requestresponse-parameters)

By default, request/response parameter(s) will be [extracted from model](#models).

Override them by including `@request` or/and `@response` in the method doc block.

Example for `@request`, working the same for `@response`:

- `@request` will override what is fetched by default. You must provide it in the following convention `@request name type required description` where:
    - `name` - parameter name.
    - `type` - [OpenApi data type](https://swagger.io/docs/specification/data-models/data-types/).
    - `required` - boolean `true/false` value indicating whether the parameter is required (if omitted, will be set to `true`)
    - `description` - text which will be set as parameter description (if omitted, will be set to empty string)

Examples:

```
@request name type
@request name type true
@request name type false Some description

```

For multiple parameters it is also possible to adopt a different convention:

```
@request
name type required description
name type required description
name type required description

```

In case you want an arbitrary string as a request/response, it can be achieved by using double quotes when setting request/response parameters. This way, all other parameters will be ignored and only the string inside double quotes will be returned.

Example:

```
@response "example"

```

You can include additional input/output models alongside original model with `@requestAppend key Class` or `@responseAppend key Class` in your method doc block. This will append given `Class` properties on already existing model using the `key` as a key.

I.e. having original model `User` (with properties name, email) you want to append `Post` model (with properties title, description) to it as a list of inputs.

```
@model User //
