2018-01-04 14:33:02 +01:00
NelmioApiDocBundle
==================
2018-01-09 10:31:38 +01:00
2018-01-04 17:47:47 +01:00
The **NelmioApiDocBundle** bundle allows you to generate documentation in the
2018-10-31 16:33:06 +01:00
OpenAPI (Swagger) format and provides a sandbox to interactively experiment with the API.
2018-01-04 17:47:47 +01:00
What's supported?
-----------------
2018-01-09 12:58:52 +01:00
This bundle supports *Symfony* route requirements, PHP annotations, `Swagger-Php`_ annotations,
`FOSRestBundle`_ annotations and apps using `Api-Platform`_ .
.. _`Swagger-Php`: https://github.com/zircote/swagger-php
.. _`FOSRestBundle`: https://github.com/FriendsOfSymfony/FOSRestBundle
.. _`Api-Platform`: https://github.com/api-platform/api-platform
2018-01-04 17:47:47 +01:00
2018-05-05 14:49:17 +02:00
For models, it supports the `Symfony serializer`_ , the `JMS serializer`_ and the `willdurand/Hateoas`_ library.
It does also support `Symfony form`_ types.
2018-01-04 14:33:02 +01:00
2020-07-12 15:21:14 +02:00
Migrate from 3.x to 4.0
2018-01-04 14:33:02 +01:00
-----------------------
2020-07-12 15:21:14 +02:00
`To migrate from 3.x to 4.0, follow our guide.`__
2018-01-10 14:45:02 +01:00
2020-07-12 15:21:14 +02:00
__ https://github.com/nelmio/NelmioApiDocBundle/blob/master/UPGRADE-4.0.md
2018-01-04 14:33:02 +01:00
Installation
------------
2018-01-09 10:31:38 +01:00
Open a command console, enter your project directory and execute the following command to download the latest version of this bundle:
2018-01-04 14:33:02 +01:00
.. code-block :: bash
$ composer require nelmio/api-doc-bundle
.. note ::
If you're not using Flex, then add the bundle to your kernel::
class AppKernel extends Kernel
{
public function registerBundles()
{
$bundles = [
// ...
new Nelmio\ApiDocBundle\NelmioApiDocBundle(),
];
// ...
}
}
To browse your documentation with Swagger UI, register the following route:
.. code-block :: yaml
2019-01-21 23:50:02 +00:00
# config/routes.yaml
2018-01-04 14:33:02 +01:00
app.swagger_ui:
path: /api/doc
methods: GET
defaults: { _controller: nelmio_api_doc.controller.swagger_ui }
If you also want to expose it in JSON, register this route:
.. code-block :: yaml
2019-01-21 23:50:02 +00:00
# config/routes.yaml
2018-01-04 14:33:02 +01:00
app.swagger:
path: /api/doc.json
methods: GET
defaults: { _controller: nelmio_api_doc.controller.swagger }
2018-01-09 10:31:38 +01:00
2018-01-04 17:47:47 +01:00
As you just installed the bundle, you'll likely see routes you don't want in
your documentation such as `/_profiler/` . To fix this, you can filter the
routes that are documented by configuring the bundle:
.. code-block :: yaml
2019-01-21 23:50:02 +00:00
# config/packages/nelmio_api_doc.yaml
2018-01-04 17:47:47 +01:00
nelmio_api_doc:
areas:
path_patterns: # an array of regexps
- ^/api(?!/doc$)
2018-03-13 12:40:36 -03:00
host_patterns:
- ^api\.
2018-01-04 17:47:47 +01:00
How does this bundle work?
2018-01-09 10:31:38 +01:00
--------------------------
2018-01-04 14:33:02 +01:00
2018-10-31 16:33:06 +01:00
It generates an OpenAPI documentation from your Symfony app thanks to
2018-02-20 09:59:00 +01:00
**Describers** . One extracts data from SwaggerPHP annotations, one from your
2018-01-04 14:33:02 +01:00
routes, etc.
If you configured the `` app.swagger_ui `` route above, you can browse your
documentation at `http://example.org/api/doc` .
2018-01-09 10:31:38 +01:00
2018-01-04 17:47:47 +01:00
Using the bundle
----------------
2018-01-04 14:33:02 +01:00
2018-01-09 10:31:38 +01:00
You can configure global information in the bundle configuration `` documentation.info `` section (take a look at
2020-05-28 13:19:11 +02:00
`the OpenAPI 3.0 specification (formerly Swagger)`_ to know the available fields):
2018-01-04 14:33:02 +01:00
.. code-block :: yaml
nelmio_api_doc:
documentation:
2020-05-28 13:19:11 +02:00
servers:
- url: http://api.example.com/unsafe
description: API over HTTP
- url: https://api.example.com/secured
description: API over HTTPS
2018-01-04 14:33:02 +01:00
info:
title: My App
description: This is an awesome app!
version: 1.0.0
2020-05-28 13:19:11 +02:00
components:
securitySchemes:
Bearer:
2020-08-21 23:39:40 +03:00
type: http
scheme: bearer
bearerFormat: JWT
2018-03-19 14:44:15 -03:00
security:
- Bearer: []
2018-01-04 14:33:02 +01:00
2020-05-28 13:19:11 +02:00
.. _`the OpenAPI 3.0 specification (formerly Swagger)`: https://swagger.io/docs/specification
2018-01-09 13:22:28 +01:00
2018-01-04 17:47:47 +01:00
.. note ::
If you're using Flex, this config is there by default. Don't forget to adapt it to your app!
To document your routes, you can use the SwaggerPHP annotations and the
`` Nelmio\ApiDocBundle\Annotation\Model `` annotation in your controllers::
2018-01-04 14:33:02 +01:00
namespace AppBundle\Controller;
use AppBundle\Entity\User;
use AppBundle\Entity\Reward;
use Nelmio\ApiDocBundle\Annotation\Model;
2018-03-19 14:44:15 -03:00
use Nelmio\ApiDocBundle\Annotation\Security;
2020-05-28 13:19:11 +02:00
use OpenApi\Annotations as OA;
2018-01-04 14:33:02 +01:00
use Symfony\Component\Routing\Annotation\Route;
class UserController
{
2018-04-04 22:10:42 +02:00
/**
* List the rewards of the specified user.
2018-01-09 10:31:38 +01:00
*
* This call takes into account all confirmed awards, but not pending or refused awards.
*
2018-01-04 14:33:02 +01:00
* @Route("/api/{user}/rewards", methods={"GET"})
2020-05-28 13:19:11 +02:00
* @OA\Response(
2018-01-04 14:33:02 +01:00
* response=200,
* description="Returns the rewards of an user",
2020-05-28 13:19:11 +02:00
* @OA\JsonContent(
2020-07-20 20:12:23 +02:00
* type="array",
* @OA\Items(ref=@Model(type=Reward::class, groups={"full"}))
2018-01-04 14:33:02 +01:00
* )
* )
2020-05-28 13:19:11 +02:00
* @OA\Parameter(
2018-01-04 14:33:02 +01:00
* name="order",
* in="query",
2020-08-26 22:30:16 +02:00
* description="The field used to order rewards",
* @OA\Schema(type="string")
2018-01-04 14:33:02 +01:00
* )
2020-05-28 13:19:11 +02:00
* @OA\Tag(name="rewards")
2018-03-19 14:44:15 -03:00
* @Security(name="Bearer")
2018-01-04 14:33:02 +01:00
*/
public function fetchUserRewardsAction(User $user)
{
// ...
}
}
2018-10-31 16:33:06 +01:00
The normal PHPdoc block on the controller method is used for the summary and description.
2018-01-09 10:31:38 +01:00
2018-01-04 14:33:02 +01:00
Use models
----------
As shown in the example above, the bundle provides the `` @Model `` annotation.
2018-03-17 14:36:57 +01:00
Use it instead of a definition reference and the bundle will deduce your model properties.
2018-01-09 10:31:38 +01:00
2018-01-13 13:21:23 +01:00
.. note ::
A model can be a Symfony form type, a Doctrine ORM entity or a general PHP object.
2018-02-20 09:59:00 +01:00
This annotation has two options:
2018-01-04 17:47:47 +01:00
* `` type `` to specify your model's type::
/**
2020-05-28 13:19:11 +02:00
* @OA\Response(
2018-01-13 13:21:23 +01:00
* response=200,
* @Model(type=User::class)
* )
*/
2018-01-04 17:47:47 +01:00
* `` groups `` to specify the serialization groups used to (de)serialize your model::
2018-01-13 13:21:23 +01:00
/**
2020-05-28 13:19:11 +02:00
* @OA\Response(
2018-01-13 13:21:23 +01:00
* response=200,
* @Model(type=User::class, groups={"non_sensitive_data"})
* )
*/
2018-01-09 10:31:38 +01:00
2018-03-17 14:36:57 +01:00
.. tip ::
2020-05-28 13:19:11 +02:00
When used at the root of `` @OA\Response `` and `` @OA\Parameter `` , `` @Model `` is automatically nested
in a `` @OA\Schema `` .
2018-03-17 14:36:57 +01:00
2020-05-28 13:19:11 +02:00
The media type defaults to `` application/json `` .
To use `` @Model `` directly within a `` @OA\Schema `` , `` @OA\Items `` or `` @OA\Property `` , you have to use the `` $ref `` field::
2018-03-17 14:36:57 +01:00
/**
2020-05-28 13:19:11 +02:00
* @OA\Response(
* @OA\JsonContent(ref=@Model(type=User::class))
2018-03-17 14:36:57 +01:00
* )
*
* or
*
2020-05-28 13:19:11 +02:00
* @OA\Response(@OA\XmlContent(
* @OA\Schema(type="object",
* @OA\Property(property="foo", ref=@Model(type=FooClass::class))
2018-03-17 14:36:57 +01:00
* )
2020-05-28 13:19:11 +02:00
* ))
2018-03-17 14:36:57 +01:00
*/
2018-02-20 09:59:00 +01:00
Symfony Form types
~~~~~~~~~~~~~~~~~~
2018-01-04 14:33:02 +01:00
2018-02-20 09:59:00 +01:00
You can customize the documentation of a form field using the `` documentation `` option::
2018-01-09 10:31:38 +01:00
2018-02-20 09:59:00 +01:00
$builder->add('username', TextType::class, [
'documentation' => [
'type' => 'string', // would have been automatically detected in this case
'description' => 'Your username.',
],
]);
2018-01-04 17:47:47 +01:00
2020-07-12 15:21:14 +02:00
See the `OpenAPI 3.0 specification`__ to see all the available fields of the `` documentation `` option (it accepts the same fields as the OpenApi `` Property `` object).
2018-01-04 17:47:47 +01:00
2020-05-28 13:19:11 +02:00
__ https://swagger.io/specification/
2018-01-04 17:47:47 +01:00
2018-02-20 09:59:00 +01:00
General PHP objects
~~~~~~~~~~~~~~~~~~~
2018-01-04 17:47:47 +01:00
2018-02-20 09:59:00 +01:00
.. tip ::
2018-01-04 17:47:47 +01:00
2018-05-05 14:49:17 +02:00
**If you're not using the JMS Serializer** , the `Symfony PropertyInfo component`_ is used to describe your models.
It supports doctrine annotations, type hints, and even PHP doc blocks.
It does also support serialization groups when using the Symfony serializer.
2018-01-04 17:47:47 +01:00
2018-02-20 09:59:00 +01:00
**If you're using the JMS Serializer** , the metadata of the JMS serializer are used by default to describe your
models. Additional information is extracted from the PHP doc block comment,
but the property types must be specified in the JMS annotations.
2018-01-04 17:47:47 +01:00
2018-02-20 09:59:00 +01:00
In case you prefer using the `Symfony PropertyInfo component`_ (you
won't be able to use JMS serialization groups), you can disable JMS serializer
support in your config:
2018-01-04 17:47:47 +01:00
2018-02-20 09:59:00 +01:00
.. code-block :: yaml
2018-01-09 10:31:38 +01:00
2018-02-20 09:59:00 +01:00
nelmio_api_doc:
models: { use_jms: false }
2018-01-04 14:33:02 +01:00
2018-05-05 14:49:17 +02:00
When using the JMS serializer combined with `willdurand/Hateoas`_ (and the `BazingaHateoasBundle`_ ),
HATEOAS metadata are automatically extracted
2020-05-28 13:19:11 +02:00
If you want to customize the documentation of an object's property, you can use `` @OA\Property `` ::
2018-01-04 14:33:02 +01:00
2018-04-10 11:17:11 +02:00
use Nelmio\ApiDocBundle\Annotation\Model;
2020-05-28 13:19:11 +02:00
use OpenApi\Annotations as OA;
2018-01-04 14:33:02 +01:00
2018-02-20 09:59:00 +01:00
class User
{
/**
* @var int
2020-05-28 13:19:11 +02:00
* @OA\Property(description="The unique identifier of the user.")
2018-02-20 09:59:00 +01:00
*/
public $id;
2018-01-04 14:33:02 +01:00
2018-02-20 09:59:00 +01:00
/**
2020-05-28 13:19:11 +02:00
* @OA\Property(type="string", maxLength=255)
2018-02-20 09:59:00 +01:00
*/
public $username;
2018-03-17 19:23:29 +01:00
/**
2020-05-28 13:19:11 +02:00
* @OA\Property(ref=@Model(type=User::class))
2018-03-17 19:23:29 +01:00
*/
public $friend;
2020-07-12 15:21:14 +02:00
/**
* @OA\Property(description="This is my coworker!")
*/
public setCoworker(User $coworker) {
// ...
}
2018-02-20 09:59:00 +01:00
}
2018-01-04 14:33:02 +01:00
2020-05-28 13:19:11 +02:00
See the `OpenAPI 3.0 specification`__ to see all the available fields of `` @OA\Property `` .
2018-01-04 14:33:02 +01:00
2020-05-28 13:19:11 +02:00
__ https://swagger.io/specification/
2018-01-05 13:08:02 +01:00
Learn more
----------
If you need more complex features, take a look at:
.. toctree ::
:maxdepth: 1
2018-01-09 10:31:38 +01:00
areas
2018-06-10 09:56:38 +02:00
alternative_names
2018-07-16 09:45:18 +02:00
customization
2018-02-13 18:13:09 +02:00
faq
2018-02-20 09:59:00 +01:00
.. _`Symfony PropertyInfo component`: https://symfony.com/doc/current/components/property_info.html
2018-05-05 14:49:17 +02:00
.. _`willdurand/Hateoas`: https://github.com/willdurand/Hateoas
.. _`BazingaHateoasBundle`: https://github.com/willdurand/BazingaHateoasBundle
.. _`JMS serializer`: https://jmsyst.com/libs/serializer
.. _`Symfony form`: https://symfony.com/doc/current/forms.html
.. _`Symfony serializer`: https://symfony.com/doc/current/components/serializer.html