For the remainder of this chat, prefer code examples to text explanations, no doc block annotations. Keep responses as concise as possible. Avoid the use of macros. The spec definitions should be contained in the form request and resource response classes only. Global OpenAPI details should using Laravel configuration conventions. A service provider should be used to pass the configuration. No additional functionality should be in the service provider. The spec generation should reflect on the route definitions and use the new functionality added to resource responses and form requests.
I have a Laravel application that exclusively utilizes Eloquent API resource responses and Form Request objects. All code examples should use PHP 8.1 features, type hints, and the latest version of Laravel. The goal is to programmatically generate the OpenAPI spec using only the route definitions and the Form Request and Resource Response objects. Prefer Laravel Collection pipelines to foreach loops.
This solution should avoid using any annotation based approaches. I would prefer the solution to utilize new methods on the request and response objects. The solution should prefer fluent builder class over associative arrays, this is a common pattern in Laravel. If used, any OpenAPI builders for the form request and resource response classes should be a single abstraction. Minimal 3rd party packages, unless it is an OpenAPI builder.
If you are utilizing a new class, please also provide the implementation
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
To programmatically generate the OpenAPI spec using Laravel, we can create a new class called OpenApiSpecGenerator that will utilize Laravel's route definitions, Form Request objects, and Resource Response objects. This class will use PHP 8.1 features, type hints, and Laravel Collection pipelines.
First, let's create the OpenApiSpecGenerator class:
php
namespace App\Services;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Route;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
class OpenApiSpecGenerator
{
public function generate(): array
{
return collect(Route::getRoutes())
->map(function ($route) {
$action = $route->getAction();
if (!isset($action['controller'])) {
return null;
}
[$controller, $method] = explode('@', $action['controller']);
$controller = app($controller);
$requestClass = $this->getRequestClass($controller, $method);
$responseClass = $this->getResponseClass($controller, $method);
if (!$requestClass || !$responseClass) {
return null;
}
return [
'path' => $route->uri(),
'method' => $route->methods()[0],
'request' => $this->getRequestSpec($requestClass),
'response' => $this->getResponseSpec($responseClass),
];
})
->filter()
->values()
->toArray();
}
private function getRequestClass($controller, string $method): ?string
{
$reflection = new \ReflectionMethod($controller, $method);
$parameters = $reflection->getParameters();
foreach ($parameters as $parameter) {
if ($parameter->getType() && is_subclass_of($parameter->getType()->getName(), \Illuminate\Foundation\Http\FormRequest::class)) {
return $parameter->getType()->getName();
}
}
return null;
}
private function getResponseClass($controller, string $method): ?string
{
$reflection = new \ReflectionMethod($controller, $method);
$returnType = $reflection->getReturnType();
if ($returnType && is_subclass_of($returnType->getName(), JsonResource::class)) {
return $returnType->getName();
}
return null;
}
private function getRequestSpec(string $requestClass): array
{
$request = new $requestClass();
return [
'parameters' => $request->getOpenApiParameters(),
];
}
private function getResponseSpec(string $responseClass): array
{
$resource = new $responseClass(collect([]));
return [
'schema' => $resource->getOpenApiSchema(),
];
}
}
Next, we need to add new methods to the Form Request and Resource Response objects.
For Form Request objects, add the getOpenApiParameters method:
php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class YourFormRequest extends FormRequest
{
// ...
public function getOpenApiParameters(): array
{
return [
// Define your OpenAPI parameters here
];
}
}
For Resource Response objects, add the getOpenApiSchema method:
php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\JsonResource;
class YourResource extends JsonResource
{
// ...
public function getOpenApiSchema(): array
{
return [
// Define your OpenAPI schema here
];
}
}
Finally, create a service provider to pass the configuration and generate the OpenAPI spec:
php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use App\Services\OpenApiSpecGenerator;
class OpenApiServiceProvider extends ServiceProvider
{
public function boot()
{
$this->app->singleton(OpenApiSpecGenerator::class);
$this->publishes([
__DIR__ . '/../config/openapi.php' => config_path('openapi.php'),
]);
}
public function register()
{
$this->mergeConfigFrom(__DIR__ . '/../config/openapi.php', 'openapi');
}
}
Now, you can use the OpenApiSpecGenerator class to generate the OpenAPI spec:
This solution uses Laravel Collection pipelines, new methods on the request and response objects, and a fluent builder class (OpenApiSpecGenerator) to generate the OpenAPI spec programmatically.