Skip to content

glennjacobs/openapi-spec-generator

 
 

Repository files navigation

OpenAPI v3 Spec Generator

Latest Version on Packagist Software License Buy us a tree Build Status Total Downloads Maintained by SWIS

Designed to work with Laravel JSON:API

!!! Disclaimer: this project is work in progress and likely contains many bugs, etc !!!

What it can and can't

Can

  • Generate Schemas/Responses/Request/Errors for all default Laravel JSON:API routes
  • Use a seeded database to generate examples

Can't yet

  • Customisation of the generation
  • Generation for custom actions
  • Generation for custom filters
  • Generation for anything custom
  • Generation for MorphTo relations (MorphToMany works)
  • Generation of Pagination Meta
  • Generation of Includes
  • Generation of Authentication/Authorization

TODO

  • Command to generate to storage folder
  • Get basic test suite running with GitHub Actions
  • Add extra operation descriptions via config
  • Add in tags & x-tagGroups (via config)
  • Add tests (Use the dummy by laraveljsonapi to integrate all features)
  • Add custom actions
  • Split schemas/requests/responses by action
  • Consider field attributes
    • bool readonly
    • bool hidden
    • closure based readonly (create/update)
    • closure based hidden
  • List sortable fields
  • Fix includes and relations
    • Add relationship routes
    • Add includes
  • Add authentication
  • Add custom queries/filters
  • Add a way to document custom actions
  • Tidy up the code!!
  • Replace cebe/php-openapi with goldspecdigital/oooas
  • Move to an architecture inspired by vyuldashev/laravel-openapi
  • Use php8 attributes on actions/classes to generate custom docs

🙏 Based upon initial prototype by martianatwork, glennjacobs and byte-it.

Install

Via Composer

composer require swisnl/openapi-spec-generator

Publish the config file

php artisan vendor:publish --provider="LaravelJsonApi\OpenApiSpec\OpenApiServiceProvider"

Usage

Generate the Open API spec

php artisan jsonapi:openapi:generate v1

Note that a seeded DB is required! The seeded data will be used to generate Samples.

Descriptions

It's possible to add descriptions to your endpoints by implementing the DescribesEndpoints interface. The added method receives the generated route name as a parameter. This can be used to generate descriptions for all your schema endpoints.

class Post extends Schema implements DescribesEndpoints
{
    public function describeEndpoint(string $endpoint) {
        if ($endpoint === 'v1.posts.index') {
            return 'Description for index method';
        }

        return 'Default description';
    }
}

Generating Documentation

A quick way to preview your documentation is to use speccy serve command.

Ensure you have installed Speccy globally and then you can use the following command:

speccy serve storage/app/v1_openapi.yaml

Warning: Seems like Speccy is abandoned (wework/speccy#485).

Easily publish your API documentation in a local route by using your OpenAPI document in your Laravel Application directly.

For this to work, you have to generate your spec in a public-available location, like the local 'public' disk available in Laravel applications:

OPEN_API_SPEC_GENERATOR_FILESYSTEM_DISK='public'

After installing it, you should set its url config: STOPLIGHT_OPENAPI_PATH. For example, if you're using the 'public' disk:

OPEN_API_SPEC_GENERATOR_FILESYSTEM_DISK='public'

# '/storage' is the default 'public' URL.
STOPLIGHT_OPENAPI_PATH='/storage/v1_openapi.json' 

Note: If you need a more dynamic way to get access to the spec URL (for example, in S3 you may need to use temporary URLs), you can publish its Blade template and replace some lines to generate your own URI. Also, you may need to add an Fetch interceptor to integrate it with your authentication methods.

With its default route, ¡you just need to access to your /api/docs route to preview your specs! :bowtie:

Check its configuration docs for further options.

In addition to previous Laravel package, you can use the Stoplight Elements by yourself. It is available as React Component, or Web Component, making it easier for integrating into existing Content Management Systems with their own navigation.

This is useful when you need more advanced customizations in the routing system, integrate it in your existing Vue|React|Vanilla application, or publish it as a non-laravel static HTML site. But... you have to setup it manually. 😅

Web component integrated in your Vue application

You can follow the instructions to use the standalone Web Component, grab it into a blade template and armor your view.

It has advanced options, like tryItCredentialPolicy="same-origin" to use your cookie-based authentication (like Sanctum).

Also, in your Blade view or Vue's app initializer, as this package uses Fetch API you can add interceptors to customize the "try it out" feature, like adding default headers for Content-Type and/or Accept to be 'application/vnd.api+json' to your requests.

Other options

You can check a more exhaustive list of options available at https://openapi.tools/#documentation

Change log

Please see CHANGELOG for more information on what has changed recently.

Testing

$ composer test

Contributing

Please see CONTRIBUTING and CODE_OF_CONDUCT for details.

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

Credits

License

Apache License 2.0. All notable changes to the original work can be found in CHANGELOG. Please see License File for more information.

This package is Treeware. If you use it in production, then we ask that you buy the world a tree to thank us for our work. By contributing to the Treeware forest you’ll be creating employment for local families and restoring wildlife habitats.

SWIS ❤️ Open Source

SWIS is a web agency from Leiden, the Netherlands. We love working with open source software.

About

Creates Open API spec for a Laravel JSON:API

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • PHP 100.0%