fusonic / api-documentation-bundle
Symfony bundle for automated documentation with NelmioApiDocBundle.
Installs: 12 146
Dependents: 0
Suggesters: 0
Security: 0
Stars: 3
Watchers: 8
Forks: 0
Open Issues: 0
Type:symfony-bundle
Requires
- php: >=8.2
- nelmio/api-doc-bundle: ^4.29
- symfony/config: ^5.4 || ^6.0 || ^7.0
- symfony/dependency-injection: ^5.4 || ^6.0 || ^7.0
- symfony/dom-crawler: ^5.4 || ^6.0 || ^7.0
- symfony/property-info: ^5.4 || ^6.0 || ^7.0
- symfony/routing: ^5.4 || ^6.0 || ^7.0
- zircote/swagger-php: ^4.7
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.58
- infection/infection: ^0.29
- phpstan/phpstan: ^1.11
- phpstan/phpstan-deprecation-rules: ^1.2
- phpstan/phpstan-phpunit: ^1.4
- phpstan/phpstan-strict-rules: ^1.6
- phpstan/phpstan-symfony: ^1.4
- phpunit/phpunit: ^11.2
- symfony/framework-bundle: ^5.4 || ^6.0 || ^7.0
- symfony/serializer: ^5.4 || ^6.0 || ^7.0
- symfony/test-pack: ^1.0
- symfony/yaml: ^5.4 || ^6.0 || ^7.0
- tomasvotruba/type-coverage: ^0.3
README
About
This bundle makes generating documentation of your (json) API routes easier. It provides a custom route annotation that can parse the input and output model of a route to generate documentation definitions for NelmioApiDocBundle. If you are using type hints for the input and output it can be detected automatically, see Usage on how to do this.
With just NelmioApiDocBundle you will often find yourself writing many annotations with a repetitive pattern. With this bundle common annotation combinations are bundled into one single route attribute.
This bundle can work well together with the http-kernel-bundle.
Install
Use composer to install the bundle from packagist.
composer require fusonic/api-documentation-bundle
Requirements:
- PHP 8.2+
- Symfony 5.4+
In case Symfony did not add the bundle to the bundle configuration, add the following (by default located
in config/bundles.php
):
<?php
return [
// ...
Fusonic\ApiDocumentationBundle\FusonicApiDocumentationBundle::class => ['all' => true],
];
Next you need to configure NelmioApiDocBundle.
There is one optional configuration for this bundle:
fusonic_api_documentation: # An attribute, class, or interface that is used to detect which object to parse the "input" model from. # If you do not configure this, automatic input detection will not work. request_object_class: Fusonic\ApiDocumentationBundle\Tests\App\FromRequest
Usage
Different examples can be found in the tests.
Example route with automatic type detection
If you have some kind of response listener that allows you to return objects directly from your controller then you can use the automatic output detection based on the return type or return annotation.
#[DocumentedRoute(path: '/test-return-type/{id}', methods: ['GET'])] public function testReturnType(#[FromRequest] TestRequest $query): TestResponse { return new TestResponse($query->id); }
If you return an array or a generic type, you can set the return type (e.g.: SomeType[]
or `SomeGeneric).
The only requirement here is that you can only have one return type. Multiple return types are not supported.
Example route with manual input/output
If you do not support argument resolving and returning objects directly you can define the input
and output
classes manually.
#[DocumentedRoute(path: '/test-return-type/{id}', methods: ['GET'], input: TestRequest::class, output: TestResponse::class)] public function testReturnType(int $id): JsonResponse { return new JsonResponse(['id' => $query->id], 200); }
You can also specify builtin types for the output, for example string
:
#[DocumentedRoute(path: '/test-return-type/{id}', methods: ['GET'], input: TestRequest::class, output: 'string')]
If your manually defined output is a collection, you can set outputIsCollection: true
in addition to the output
:
#[DocumentedRoute( path: '/test-return-type/{id}', methods: ['GET'], input: TestRequest::class, description: 'custom description', statusCode: 200, output: 'string', outputIsCollection: true )]
Contributing
This is a subtree split of fusonic/php-extensions repository. Please create your pull requests there.