For the remainder of this session, prefer code examples to text explanations, no doc block annotations. Keep responses as concise as possible.
The solution should:
Avoid the user of macros
Limit request/response object definitions to the resource response and form request classes
Accept global OpenAPI spec details upon generation
The OpenAPI spec generation should iterate over all of the routes defined in the application and use PHP reflection to determine the form request and resource response classes
Be compatible with Laravel 9
Use PHP 8.1 features with type hinting
Avoid the use of external packages
Not use annotation to define spec or object definitions
use PHP reflection, defensibly, to determine what form request and resource response match with the route
Generate an OpenAPI v3 compatible spec
I have a Laravel application that exclusively utilizes Eloquent API resource responses and Form Request objects. I want to use a command to generate an OpenAPI spec of all defined routes including their request and response object definitions.
For any examples you can use the route POST /api/users to create a user. The User only has the attributes of 'name' and 'email'
Lets start by only discussing the Form RequestSave & SubmitCancel
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 generate an OpenAPI spec for a Laravel application, you can create a custom Artisan command that uses PHP reflection to determine the form request and resource response classes for each route. Here's an example of how you can create a command to generate the OpenAPI spec for the POST /api/users route:
Create a new Artisan command:
bash
php artisan make:command GenerateOpenApiSpec
Open the newly created command file (app/Console/Commands/GenerateOpenApiSpec.php) and update its content:
php
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Request;
use Illuminate\Support\Facades\Response;
use Illuminate\Http\Request as HttpRequest;
use Illuminate\Http\JsonResponse as HttpJsonResponse;
use ReflectionClass;
use ReflectionMethod;
use ReflectionParameter;
use OpenApi\Annotations as OA;
class GenerateOpenApiSpec extends Command
{
protected $signature = 'openapi:generate';
protected $description = 'Generate OpenAPI spec for the application';
public function handle()
{
$openApi = new \OpenApi\Analysis();
foreach (Route::getRoutes() as $route) {
$action = $route->getAction();
$controller = $action['controller'];
$method = $action['uses'];
if (is_string($controller) && is_string($method)) {
$reflection = new ReflectionClass($controller);
$instance = $reflection->newInstanceWithoutConstructor();
$reflectionMethod = new ReflectionMethod($controller, $method);
$parameters = $reflectionMethod->getParameters();
$requestParameter = null;
foreach ($parameters as $parameter) {
if ($parameter->getType() && $parameter->getType()->getName() === HttpRequest::class) {
$requestParameter = $parameter;
break;
}
}
if ($requestParameter) {
$requestClass = $requestParameter->getClass()->getName();
$requestReflection = new ReflectionClass($requestClass);
$requestInstance = $requestReflection->newInstanceWithoutConstructor();
$rules = $requestInstance->rules();
$openApi->path($route->uri, [
'post' => [
'summary' => 'Create a new user',
'requestBody' => [
'content' => [
'application/json' => [
'schema' => [
'type' => 'object',
'properties' => $this->getPropertiesFromRules($rules),
],
],
],
],
'responses' => [
'200' => [
'description' => 'Successful operation',
'content' => [
'application/json' => [
'schema' => [
'type' => 'object',
'properties' => [
'data' => [
'type' => 'object',
'properties' => [
'name' => ['type' => 'string'],
'email' => ['type' => 'string'],
],
],
],
],
],
],
],
],
],
]);
}
}
}
$this->info($openApi->toYaml());
}
private function getPropertiesFromRules(array $rules): array
{
$properties = [];
foreach ($rules as $field => $rule) {
$properties[$field] = ['type' => 'string'];
}
return $properties;
}
}
Register the command in the app/Console/Kernel.php file:
This command will generate an OpenAPI v3 compatible spec for the POST /api/users route, including the request and response object definitions. You can extend this command to handle other routes and their respective form request and resource response classes.