draw / open-api-bundle
draw/open-api library integration into Symfony4.
Installs: 10 073
Dependents: 1
Suggesters: 0
Security: 0
Stars: 0
Watchers: 3
Forks: 0
Open Issues: 0
Type:symfony-bundle
Requires
- php: ^7.3
- ext-json: *
- doctrine/annotations: ^1.11
- draw/open-api: ^0.7.59
- jms/serializer-bundle: ^3.7
- symfony/framework-bundle: ^4.4 || ^5.4
Requires (Dev)
- doctrine/doctrine-bundle: ^2.5
- draw/tester-bundle: ^0.7.59
- phpunit/phpunit: ^8.0 || ^9.0
- sensio/framework-extra-bundle: ^5.6 || ^6.2
- symfony/yaml: ^4.4 || ^5.4
Suggests
- nelmio/cors-bundle: If you have a api exposed for browser on different domain/sub-domain you might need cors.
- dev-master / 0.8.x-dev
- 0.7.59
- 0.7.58
- 0.7.57
- 0.7.56
- 0.7.54
- 0.7.53
- v0.7.52
- v0.7.51
- v0.7.50
- v0.7.49
- v0.7.48
- v0.7.47
- v0.7.46
- v0.7.45
- v0.7.44
- v0.7.43
- v0.7.42
- v0.7.41
- v0.7.40
- v0.7.39
- v0.7.38
- v0.7.37
- v0.7.36
- v0.7.35
- v0.7.34
- v0.7.33
- v0.7.32
- v0.7.31
- v0.7.30
- v0.7.29
- v0.7.28
- v0.7.27
- v0.7.26
- v0.7.25
- v0.7.24
- v0.7.23
- v0.7.22
- 0.7.21
- v0.7.20
- v0.7.19
- v0.7.18
- v0.7.17
- v0.7.16
- v0.7.15
- v0.7.14
- v0.7.13
- v0.7.12
- v0.7.11
- v0.7.10
- v0.7.9
- v0.7.8
- v0.7.7
- v0.7.6
- v0.7.5
- v0.7.4
- v0.7.3
- v0.7.2
- v0.7.1
- v0.7.0
- v0.6.0
- v0.5.23
- 0.5.22
- v0.5.21
- v0.5.20
- v0.5.19
- v0.5.18
- v0.5.17
- v0.5.16
- v0.5.15
- v0.5.14
- v0.5.13
- v0.5.12
- v0.5.11
- v0.5.10
- v0.5.9
- v0.5.8
- 0.5.7
- 0.5.6
- 0.5.5
- 0.5.4
- 0.5.3
- 0.5.2
- 0.5.1
- 0.5.0
- dev-packages-git-actions
This package is auto-updated.
Last update: 2024-11-06 23:27:49 UTC
README
Integration for draw/open-api into symfony 4 bundle
The first objective is to be able to generate a Open Api v2 documentation with minimum effort by the programmer. The draw/open-api provide a multitude of extractor to get the information where it can (PHP for example).
The integration with symfony allow you to use most of the Draw\Component\OpenApi\Schema (alias @OpenApi) as annotation above your controller method to document them.
The bundle also provide some tools to provide a rest api without the need of FOSRestBundle.
Sandbox
To install the sandbox you can run
bin/console draw:open-api:install-sandbox
The first argument is the version to install (E.g.: v3.52.5). By default it master.
We recommend that you add this to your composer.json scripts section before the asset:install
{ "scripts": { "auto-scripts": { "cache:clear": "symfony-cmd", "draw:open-api:install-sandbox": "symfony-cmd", "assets:install": "symfony-cmd" } } }
Configuration
Here is a example of the configuration:
draw_open_api:
enableDoctrineSupport: null #null will auto detect if DoctrineBundle is install and consider it true
convertQueryParameterToAttribute: false
responseConverter: false
definitionAliases:
- class: App\Entity\MyUser #This will change reference to class App\Entity\MyUser as User in the Api
alias: User
- class: App\Entity\ #This will Remove App\Entity\ from namespace so the class name would be expose instead of the FQCN
alias: ''
schema: #The schema section is not validate but it must match the Open Api format and will be the starting point of the generated doc
info:
title: 'Documentation for Acme API'
description: 'This is the descriptoin of the 'Acme API'
termsOfService: 'N\A'
contact: ~
version: "5.0"
Controller documentation
To document a controller for Open Api you must use the @OpenApi\Tag or @OpenApi\Operation annotations.
/**
* @OpenApi\Tag("Acme")
*/
public function defaultAction()
{
//...
}
/**
* @OpenApi\Operation(
* operationId="default",
* tags={"Acme"}
* )
*/
public function defaultAction()
{
//...
}
If you plan to use the Open Api codegen we recommend using the @OpenApi\Operation since it will give you control over the operationId, otherwise it will use the route name.
Query Parameters
If you want to inject configured query parameters in a controller you must set the convertQueryParameterToAttribute to true in the configuration.
draw_open_api: convertQueryParameterToAttribute: true
You must also add the annotation Draw\Component\OpenApi\Schema\QueryParameter to your controller. This will provide the documentation information for Open Api and also configure which query parameters should be injected.
/**
* @OpenApi\QueryParameter(name="param1")
*
* @param string $param1
*/
public function createAction($param1 = 'default')
{
//...
}
View
To allow the automatic serialization of response you must active it:
draw_open_api: responseConverter: true
The will detect if the return value of your controller is not a response and will serialized it according to the Draw\Bundle\OpenApiBundle\Response\Serialization annotation.
By default if there is not annotation the serializer context will not have any value and the response will be 200. Using the view allow to override the serializer groups and version, the status code of the response. The Serialization annotation is also use for the Open Api documentation, the headers attribute is use for that.
If your controller return null the status code will be set to 204 by default (not content).
/**
* @Draw\Bundle\OpenApiBundle\Response\Serialization(
* statusCode=201,
* serializerGroups={"MyGroup"},
* headers={
* "X-Acme-CustomHeader"=@OpenApi\Header(type="string", description="The is a custom header")
* }
* )
*/
public function createAction()
{
//...
}
The Draw\Bundle\OpenApiBundle\Response\Serialization implement Sensio\Bundle\FrameworkExtraBundle\Configuration\ConfigurationInterface
with a alias draw_open_api_serialization $request->attributes->get('_draw_open_api_serialization');
.
Instead of putting a serializerVersion on each header you can create a listener that will set the version base on something else. Here is a example of a listener that will take from the url path /api/v{number}/....:
<?php namespace App\Listener; use Draw\Bundle\OpenApiBundle\Response\Serialization; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\HttpKernel\Event\ViewEvent; use Symfony\Component\HttpKernel\KernelEvents; class VersionListener implements EventSubscriberInterface { public static function getSubscribedEvents() { return [ //It must be after reading __template attribute but before the serializer listener pass KernelEvents::VIEW => ['onKernelView', 31] ]; } public function onKernelView(ViewEvent $event) { $request = $event->getRequest(); $pathInfo = $request->getPathInfo(); $sections = explode('/', $pathInfo, 4); if(!isset($sections[2])) { return; } $version = trim($sections[2], 'v'); if($sections[2] != ('v' . $version)) { return; } $view = $request->attributes->get('_draw_open_api_serialization', new Serialization([])); if($view instanceof Serialization && $view->getSerializerVersion() === null) { $view->setSerializerVersion($version); } $request->attributes->set('_draw_open_api_serialization', $view); } }