draw/open-api
PHP library for manipulating OpenAPI (Swagger) documents. Focused on OpenAPI/Swagger 2.0 only, providing tools to read, update, and work with API definitions in code.
Installation
composer require draw/open-api
Add to composer.json if not autoloaded:
"autoload": {
"psr-4": {
"App\\": "app/",
"Draw\\OpenApi\\": "vendor/draw/open-api/src/"
}
}
Run composer dump-autoload.
First Use Case Load an existing OpenAPI 2.0 spec (YAML/JSON):
use Draw\OpenApi\Loader;
$loader = new Loader();
$spec = $loader->loadFromFile(__DIR__.'/path/to/spec.yaml');
// or
$spec = $loader->loadFromString(file_get_contents('spec.yaml'));
Key Classes to Know
Loader: Parse YAML/JSON into PHP objects.Spec: Root object representing the OpenAPI spec.Operation: Represents a single API endpoint (e.g., GET /users).Schema: Defines request/response models.Loader).Spec object.$yaml = $loader->dumpToYaml($spec);
file_put_contents('updated-spec.yaml', $yaml);
$spec = $loader->loadFromString('{}');
foreach (Route::getRoutes()->getRoutes() as $route) {
$operation = $spec->addOperation(
$route->methods()[0],
$route->uri(),
['summary' => 'Dynamic endpoint']
);
}
use Draw\OpenApi\Schema;
$schema = new Schema(['type' => 'object', 'properties' => ['name' => ['type' => 'string']]]);
$data = ['name' => 'John'];
$isValid = $schema->validate($data); // Returns boolean or validation errors
public function handle($request, Closure $next) {
$schema = $this->app['openapi']->getSchema('UserCreate');
if (!$schema->validate($request->all())) {
abort(422, 'Invalid request data');
}
return $next($request);
}
$spec = $loader->loadFromFile('spec.yaml');
foreach ($spec->getSchemas() as $name => $schema) {
$modelClass = $this->generateModel($name, $schema);
file_put_contents("app/Models/{$name}.php", $modelClass);
}
$spec = $loader->loadFromFile('spec.yaml');
$response = Http::get('https://api.example.com/users/1');
$operation = $spec->getOperation('GET', '/users/{id}');
$this->assertEquals(
$operation->getResponses()['200']->getSchema(),
$response->object()
);
Loader::validate() to check spec version:
if (!$loader->validate($spec)->isValid()) {
throw new \RuntimeException('Invalid OpenAPI 2.0 spec');
}
$ref references may cause infinite loops.Schema::resolveReferences() to resolve them:
$resolvedSchema = $schema->resolveReferences($spec->getDefinitions());
&anchor) are not supported.yaml2json spec.yaml > spec.json
$spec = Cache::remember('openapi-spec', 60, function() {
return $loader->loadFromFile('spec.yaml');
});
Draw\OpenApi\ValidatorInterface.Draw\OpenApi\Schema or add to Loader.class CustomSchema extends Schema {
public function validate($data) {
// Custom logic
}
}
print_r($spec->toArray()); // Convert to associative array
jsonlint.com or yamllint.com to pre-check files before loading.public function register() {
$this->app->singleton(Loader::class, function() {
return new Loader();
});
}
$spec = app(Loader::class)->loadFromFile('spec.yaml');
How can I help you explore Laravel packages today?