Installation Add the package via Composer:
composer require splash/openapi
Publish the config file (if needed):
php artisan vendor:publish --provider="Splash\OpenApi\OpenApiServiceProvider"
First Use Case: Generating a Client
Use the OpenApiClientGenerator to create a client from an OpenAPI spec:
use Splash\OpenApi\OpenApiClientGenerator;
$generator = new OpenApiClientGenerator();
$client = $generator->generateClientFromSpec('path/to/openapi.json');
Key Classes to Explore
OpenApiClientGenerator: Core class for client generation.OpenApiRequest: Handles API requests with OpenAPI specs.OpenApiResponse: Processes API responses.OpenApiSchema: Manages schema validation and parsing.Spec-Based Client Generation Dynamically generate a client from an OpenAPI spec (YAML/JSON) at runtime or during deployment:
$client = $generator->generateClientFromSpec(config('openapi.spec_path'));
Request Handling
Use OpenApiRequest to send requests with automatic schema validation:
$request = new OpenApiRequest($client, 'get', '/users/{id}', ['id' => 1]);
$response = $request->send();
Response Processing
Parse and validate responses using OpenApiResponse:
$data = $response->getData();
$response->validateSchema($client->getSchema('User'));
Middleware Integration Attach middleware (e.g., auth, logging) to requests:
$request->addMiddleware(new AuthMiddleware());
darkaonline/l5-swagger).$this->app->bind(OpenApiClientGenerator::class, function ($app) {
return new OpenApiClientGenerator(config('openapi.spec_path'));
});
config/openapi.php:
'spec_path' => storage_path('app/openapi/spec.json'),
Schema Mismatches
validateSchema() on responses and use OpenApiRequest::validate() for requests.'validation' => [
'strict' => true,
],
Caching Generated Clients
$client = Cache::remember('openapi_client', now()->addHours(1), function () {
return $generator->generateClientFromSpec(config('openapi.spec_path'));
});
Deprecated OpenAPI Features
Middleware Order
$request->addMiddleware(new LoggingMiddleware(), 'last');
debug flag in config to log request/response details:
'debug' => env('APP_DEBUG', false),
dd($client->getOperations());
spectral or openapi-linter to validate specs before runtime.Custom Schema Validators
Extend OpenApiSchema to add custom validation rules:
class CustomSchema extends OpenApiSchema {
public function validateCustomRule($data) {
// Custom logic
}
}
Plugin System Use Laravel's service provider to register custom plugins:
OpenApiClientGenerator::extend(function ($generator) {
$generator->addPlugin(new MyCustomPlugin());
});
Event Listeners
Listen to OpenApi.RequestSent or OpenApi.ResponseReceived events for logging/auditing:
event(new OpenApiRequestSent($request));
config_cache).How can I help you explore Laravel packages today?