zircote / swagger-php
Generate interactive documentation for your RESTful API using PHP attributes (preferred) or PHPDoc annotations
Fund package maintenance!
zircote
Installs: 126 013 238
Dependents: 483
Suggesters: 9
Security: 0
Stars: 5 283
Watchers: 93
Forks: 946
Open Issues: 16
pkg:composer/zircote/swagger-php
Requires
- php: >=8.2
- ext-json: *
- nikic/php-parser: ^4.19 || ^5.0
- phpstan/phpdoc-parser: ^2.0
- psr/log: ^1.1 || ^2.0 || ^3.0
- radebatz/type-info-extras: ^1.0.2
- symfony/deprecation-contracts: ^2 || ^3
- symfony/finder: ^5.0 || ^6.0 || ^7.0 || ^8.0
- symfony/yaml: ^5.4 || ^6.0 || ^7.0 || ^8.0
Requires (Dev)
- composer/package-versions-deprecated: ^1.11
- doctrine/annotations: ^2.0
- friendsofphp/php-cs-fixer: ^3.62.0
- phpstan/phpstan: ^2.0
- phpunit/phpunit: ^11.5
- rector/rector: ^2.3.1
Conflicts
- symfony/process: >=6, <6.4.14
- dev-master / 6.x-dev
- 6.0.3
- 6.0.2
- 6.0.1
- 6.0.0
- 6.0.0-RC4
- 6.0.0-RC3
- 6.0.0-RC2
- 6.0.0-RC1
- 5.x-dev
- 5.7.8
- 5.7.7
- 5.7.6
- 5.7.5
- 5.7.4
- 5.7.3
- 5.7.2
- 5.7.1
- 5.7.0
- 5.6.1
- 5.6.0
- 5.5.2
- 5.5.1
- 5.5.0
- 5.4.2
- 5.4.1
- 5.4.0
- 5.3.2
- 5.3.1
- 5.3.0
- 5.2.0
- 5.1.4
- 5.1.3
- 5.1.2
- 5.1.1
- 5.1.0
- 5.0.7
- 5.0.6
- 5.0.5
- 5.0.4
- 5.0.3
- 5.0.2
- 5.0.1
- 5.0.0
- 4.x-dev
- 4.11.1
- 4.11.0
- 4.10.7
- 4.10.6
- 4.10.5
- 4.10.4
- 4.10.3
- 4.10.2
- 4.10.1
- 4.10.0
- 4.9.3
- 4.9.2
- 4.9.1
- 4.9.0
- 4.8.7
- 4.8.6
- 4.8.5
- 4.8.4
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0.4.1
- 0.4.0
- 0.3.0
- 0.2.1
- 0.1.0
- dev-backport-1928-to-5.x
- dev-backport-1925-to-5.x
This package is auto-updated.
Last update: 2026-01-23 03:10:19 UTC
README
swagger-php
Generate interactive OpenAPI documentation for your RESTful API
using PHP attributes (preferred) or
doctrine annotations (requires additional
doctrine/annotations library).
See the documentation website for supported attributes and annotations.
Annotations are deprecated and may be removed in a future release of swagger-php.
Features
- Compatible with the OpenAPI 3.0, 3.1 and 3.2 specification.
- Extracts information from code and existing phpdoc comments.
- Can be used programmatically or via command-line tool.
- Documentation site with a getting started guide.
- Error reporting (with hints, context).
- All metadata is configured via PHP attributes.
OpenAPI version support
swagger-php allows to generate specs either for OpenAPI 3.0.0, OpenAPI 3.1.0 and OpenAPI 3.2.0.
By default, the spec will be in version 3.0.0. The command line option --version may be used to change to
any other supported version.
Programmatically, the method Generator::setVersion() can be used to change the version.
Requirements
swagger-php requires at least PHP 8.2.
Installation (with Composer)
composer require zircote/swagger-php
For cli usage from anywhere, install swagger-php globally and make sure to place the ~/.composer/vendor/bin directory
in your PATH so the openapi executable can be located by your system.
composer global require zircote/swagger-php
doctrine/annotations
As of version 4.8 the doctrine annotations library is
optional and no longer installed by default.
If your code uses doctrine annotations you will need to install that library manually:
composer require doctrine/annotations
Usage
Use OpenAPI attributes to add metadata to your classes, methods and other structural PHP elements.
use OpenApi\Attributes as OAT; #[OAT\Info(title: 'My First API', version: '0.1')] class MyApi { #[OAT\Get(path: '/api/resource.json')] #[OAT\Response(response: '200', description: 'An example resource')] public function getResource() { // ... } }
Visit the Documentation website for the Getting started guide or look at the examples directory for more examples.
Usage from PHP
Generate always-up-to-date documentation.
<?php require("vendor/autoload.php"); $openapi = (new \OpenApi\Generator->generate(['/path/to/project']); header('Content-Type: application/x-yaml'); echo $openapi->toYaml();
Documentation of how to use the Generator class can be found in
the Generator reference.
Usage from the Command Line Interface
The openapi command line interface can be used to generate the documentation to a static yaml/json file.
./vendor/bin/openapi --help
Automatic type resolution
As of version 6, resolving of types is done using the TypeInfoTypeResolver class. It uses the symfony/type-info
library under the hood which allows handling of complext types.
With this change, swagger-php supports all available native type-hints and also complext generic type-hints via phpdoc
blocks.
This simplifies the definition of schemas.
For example, the following two examples are now equivalent:
class MyClass { #[OAT\Property(items: new OAT\Items(oneOf: [ new OAT\Schema(type: SchemaOne::class), new OAT\Schema(type: SchemaTwo::class), ]))] public array $values; }
class MyClass { /** * @var list<SchemaOne|SchemaTwo> */ public array $values; }
If this is not desired, the LegacyTypeResolver can be used to preserve the old behaviour of version 5.
The LegacyTypeResolver is deprecated and will be removed in a future release.