Skip to content
This repository has been archived by the owner on Nov 8, 2022. It is now read-only.

OpenAPI generator for Laravel with minimal configuration

Notifications You must be signed in to change notification settings

avrahamappel-humi/open-api-generator

Repository files navigation

OpenAPI Generator

WIP.

Generate OpenAPI documentation

There are two ways to to get the generator to pick up your routes.

The first way is to decorate the controller method with the Humi\OpenApiGenerator\Attributes\OpenApi attribute.

<?php

use Humi\OpenApiGenerator\Attributes\OpenApi;

class MyController
{
    #[OpenApi]
    public function index()
    {
        // ...
    }
}

The second way is to typehint an instance of the Humi\OpenApiGenerator\RequestInterface as the request argument. Any Laravel FormRequest will do (or really anything, as long as it has a rules method), just add the interface to the class.

<?php

use Illuminate\Http\FormRequest;
use Humi\OpenApiGenerator\RequestInterface;

class MyRequest extends FormRequest implements RequestInterface
{
    public function rules(): array
    {
        return [
            // Some validation rules ...
        ];
    }
}

In your controller:

<?php

class MyController
{
    public function store(MyRequest $request)
    {
        // ...
    }
}

Run the following command to generate documentation:

php artisan open-api:generate

The generated OpenAPI spec will be located in open-api.yml at the root of your project (the file name can be configured in open-api-generator.php).

What's left to do?

  • Add more OpenAPI data types (enum, max/min, etc.)
  • Response types
  • Use $refs

About

OpenAPI generator for Laravel with minimal configuration

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages