jane/open-api
Deprecated package for generating API clients/models from OpenAPI specs with Jane. This repository is no longer maintained; use the consolidated JanePHP project instead: https://github.com/janephp/janephp
Installation:
composer require jane/open-api
(Note: This package is deprecated; consider migrating to janephp/janephp for active maintenance.)
Generate Client from OpenAPI Spec:
vendor/bin/openapi --spec=path/to/spec.yaml --output=src/Generated/Client
--spec: Path to your OpenAPI/Swagger spec (YAML or JSON).--output: Directory to generate client classes (e.g., src/Generated/Client).First Use Case:
use Generated\Client\Api\UsersApi;
use Generated\Client\Model\User;
$client = new UsersApi();
$response = $client->getUser(1); // Assuming `getUser` is defined in the spec
$user = User::fromJson($response->getBody());
Autoload Generated Classes:
Ensure the generated src/Generated/Client directory is autoloaded in composer.json:
"autoload": {
"psr-4": {
"Generated\\Client\\": "src/Generated/Client/"
}
}
Run composer dump-autoload.
Dependency Injection:
Bind the generated client to Laravel’s container (e.g., in AppServiceProvider):
$this->app->bind(
Generated\Client\Api\UsersApi::class,
fn($app) => new Generated\Client\Api\UsersApi()
);
Request Customization:
use Generated\Client\Api\UsersApi as BaseUsersApi;
use GuzzleHttp\Client;
class CustomUsersApi extends BaseUsersApi {
public function __construct() {
$client = new Client([
'base_uri' => config('services.api.base_uri'),
'headers' => ['Authorization' => 'Bearer ' . auth()->token()]
]);
parent::__construct($client);
}
}
Handling Responses:
$response = $client->createUser(new User(['name' => 'John']));
$createdUser = User::fromJson($response->getBody());
Error Handling:
Generated\Client\ApiException):
try {
$client->deleteUser(1);
} catch (ApiException $e) {
Log::error('API Error:', ['message' => $e->getMessage()]);
throw new \RuntimeException('Failed to delete user', 0, $e);
}
Deprecation Warning:
PHP Version Compatibility:
nikic/php-parser dependencies.Namespace Collisions:
tags contain special characters (e.g., dots). Use --prefix to customize:
vendor/bin/openapi --prefix=Api --output=src/Generated/
YAML Support:
AllOf/OneOf Handling:
allOf, oneOf) may generate incorrect models. Test thoroughly or extend the generator (see #33).PSR-7 Compliance:
$this->app->singleton(GuzzleHttp\Client::class, fn() => new Client([
'handler' => new \GuzzleHttp\HandlerStack(),
'base_uri' => config('api.base_url'),
]));
Verbose Output:
Run the generator with --verbose to diagnose issues:
vendor/bin/openapi --spec=spec.yaml --output=src/ --verbose
Custom Configuration:
Use a .openapi.php config file to override defaults (e.g., namespace, prefix):
return [
'namespace' => 'App\\Generated\\Client',
'prefix' => 'Api',
'use_date_time_type' => true,
];
Handling Missing Responses: If a 200 response is missing in the spec, the generator may throw warnings. Add a default response to the spec:
responses:
200:
description: Success
schema: {}
Custom Model Mapping:
Extend generated models to add Laravel-specific logic (e.g., fillable attributes):
namespace Generated\Client\Model;
class User extends \Generated\Client\Model\User {
protected $fillable = ['name', 'email'];
}
Middleware Integration: Attach Laravel middleware to API calls by wrapping the client:
$client = new UsersApi();
$client->setHttpClient(new MiddlewareClient([
'middleware' => [
fn(callable $handler) => function ($request, $options) {
$request = $request->withHeader('X-Custom-Header', 'value');
return $handler($request, $options);
},
],
]));
Dynamic Base URLs: Use Laravel’s config to set the base URL dynamically:
$client = new UsersApi();
$client->setBasePath(config('services.api.base_url'));
How can I help you explore Laravel packages today?