ak279642 / openapi-generator-laravel
Laravel package for automatic OpenAPI documentation generation from routes, controllers, Form Requests, and API resources, with Swagger UI and Scalar support.
Package info
github.com/Ak279642/openapi-generator-laravel
pkg:composer/ak279642/openapi-generator-laravel
Requires
- php: ^8.2
- illuminate/http: ^10.0|^11.0|^12.0|^13.0
- illuminate/routing: ^10.0|^11.0|^12.0|^13.0
- illuminate/support: ^10.0|^11.0|^12.0|^13.0
- laravel/framework: ^10.0|^11.0|^12.0|^13.0
- nikic/php-parser: ^5.0
- symfony/yaml: ^6.0|^7.0|^8.0
Requires (Dev)
- orchestra/testbench: ^8.0|^9.0|^10.0|^11.0
- phpunit/phpunit: ^10.0|^11.0|^12.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Laravel package for automatic OpenAPI 3.1 documentation generation from application routes, controllers, Form Requests, and JSON Resources.
Requirements
- PHP 8.2+
- Laravel 10, 11, or 12
Installation
composer require ak279642/openapi-generator-laravel php artisan vendor:publish --tag=openapi-generator-config php artisan openapi:generate
Documentation UIs
- Swagger UI:
/docs/swagger - Scalar:
/docs/scalar - JSON specification:
/docs/openapi.json - YAML specification:
/docs/openapi.yaml
The route prefix and middleware are configurable.
Generation
php artisan openapi:generate php artisan openapi:generate --format=json php artisan openapi:generate --format=yaml php artisan openapi:generate --format=both php artisan openapi:generate --check php artisan openapi:generate --no-cache
--output may point to a file for a single format or to a directory for both.
Configuration
Publish config/openapi-generator.php and configure:
- OpenAPI metadata and servers
- API route prefixes and exclusions
- security detection and bearer scheme
- generated file locations
- schema depth
- Swagger/Scalar branding
- documentation middleware
Application conventions
The generator is intentionally convention-light. It reads the Laravel route collection and uses reflection/AST inspection where possible. Form Requests can expose conditional or documentation-specific metadata through an openApiDocs() method when your application uses that convention.
JSON Resources are inspected to build reusable component schemas and nested resource references are protected against recursion.
Inline controller validation
Controller methods no longer need a dedicated Form Request just to appear correctly in the generated OpenAPI document. The generator statically detects common inline Laravel validation patterns such as:
public function store(Request $request) { $validated = $request->validate([ 'name' => 'required|string|max:100', 'email' => ['required', 'email'], 'age' => ['nullable', 'integer', 'min:18'], 'roles' => ['array'], 'roles.*' => ['string'], ]); // ... }
It also detects Validator::make($data, [...]). Detected rules are converted into request-body/query schemas, required fields, formats, enums, limits, nullable fields, nested objects/arrays, and multipart file fields where possible.
Form Requests remain fully supported and take precedence when a controller method uses one.
Array-based response structures
Literal controller response arrays are now inspected and converted into OpenAPI response schemas automatically:
return response()->json([ 'success' => true, 'message' => 'User created', 'data' => [ 'id' => 1, 'name' => 'Avinash', 'active' => true, ], ]);
The generated schema preserves the object/array shape and infers scalar types for nested literal values. Direct array returns are supported as well:
return [ 'success' => true, 'data' => [], ];
Dynamic expressions that cannot be determined safely at generation time are left as open schemas instead of executing application code.
Publishing views
php artisan vendor:publish --tag=openapi-generator-views
Published views live under resources/views/vendor/openapi-generator.
License
MIT