Skip to content
  • About
    • What is Symfony?
    • Community
    • News
    • Contributing
    • Support
  • Documentation
    • Symfony Docs
    • Symfony Book
    • Screencasts
    • Symfony Bundles
    • Symfony Cloud
    • Training
  • Services
    • Platform.sh for Symfony Best platform to deploy Symfony apps
    • SymfonyInsight Automatic quality checks for your apps
    • Symfony Certification Prove your knowledge and boost your career
    • SensioLabs Professional services to help you with Symfony
    • Blackfire Profile and monitor performance of your apps
  • Other
  • Blog
  • Download
sponsored by
  1. Home
  2. Documentation
  3. Bundles
  4. NelmioApiDocBundle
  5. Symfony attributes

Symfony attributes

Edit this page

NelmioApiDocBundle has the ability to automatically create documentation from symfony controller attributes.

MapQueryString

Using the Symfony MapQueryString attribute allows NelmioApiDocBundle to automatically generate your query parameter documentation for your endpoint from your object.

6.3

The MapQueryString attribute was introduced in Symfony 6.3.

Modify generated documentation

Modifying the generated documentation can easily by done in two ways, by: Customizing the documentation of an object's property (#[OA\Property] attribute) Customizing the documentation of a query parameter (#[OA\Parameter] attribute)

Customizing the documentation of a specific query parameter can be done by adding the #[OA\Parameter] attribute to your controller method. Make sure that the in property is set to 'query' and that the name property is set to the object's property name which you want to customize.

1
2
3
4
5
#[OA\Parameter(
    name: 'id',
    description: 'Some additional parameter description',
    in: 'query',
)]

MapQueryParameter

Using the Symfony MapQueryParameter attribute allows NelmioApiDocBundle to automatically generate your query parameter documentation for your endpoint.

6.3

The MapQueryParameter attribute was introduced in Symfony 6.3.

Modify generated documentation

Customizing the documentation of the query parameter can be done by adding the #[OA\Parameter] attribute to your controller method. Make sure that the in property is set to 'query' and that the name property is set to the name of the controller method parameter.

1
2
3
4
5
#[OA\Parameter(
    name: 'id',
    description: 'Some additional parameter description',
    in: 'query',
)]

MapRequestPayload

Using the Symfony MapRequestPayload attribute allows NelmioApiDocBundle to automatically generate your request body documentation for your endpoint.

6.3

The MapRequestPayload attribute was introduced in Symfony 6.3.

Modify generated documentation

Customizing the documentation of the request body can be done by adding the #[OA\RequestBody] attribute to your controller method.

1
2
3
#[OA\RequestBody(
    groups: ["create"],
)

Complete example

1
2
3
4
class UserQuery
{
    public int $userId;
}
1
2
3
4
5
6
7
8
9
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;

class UserDto
{
    #[Groups(["default", "create", "update"])]
    #[Assert\NotBlank(groups: ["default", "create"])]
    public string $username;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
namespace AppBundle\Controller;

use AppBundle\UserDTO;
use AppBundle\UserQuery;
use OpenApi\Attributes as OA;
use Symfony\Component\Routing\Annotation\Route;

class UserController
{
    /**
     * Find user with MapQueryString.
     */
    #[Route('/api/users', methods: ['GET'])]
    #[OA\Parameter(
        name: 'userId',
        description: 'Id of the user to find',
        in: 'query',
    )]
    public function findUser(#[MapQueryString] UserQuery $userQuery)
    {
        // ...
    }

    /**
     * Find user with MapQueryParameter.
     */
    #[Route('/api/users/v2', methods: ['GET'])]
    #[OA\Parameter(
        name: 'userId',
        description: 'Id of the user to find',
        in: 'query',
    )]
    public function findUserV2(#[MapQueryParameter] int $userId)
    {
        // ...
    }

    /**
     * Create a new user.
     */
    #[Route('/api/users', methods: ['POST'])]
    #[OA\RequestBody(
        groups: ['create'],
    )]
    public function createUser(#[MapRequestPayload] UserDTO $user)
    {
        // ...
    }
}

Customization

Imagine you want to add, modify, or remove some documentation for a route argument. For that you will have to create your own describer which implements the RouteArgumentDescriberInterface interface.

Register your route argument describer

Before you can use your custom describer you must register it in your route argument describer as a service and tag it with nelmio_api_doc.route_argument_describer. Services implementing the RouteArgumentDescriberInterface interface are automatically detected and used by NelmioApiDocBundle.

1
2
3
4
5
# config/services.yaml
services:
    App\Describer\CustomRouteArgumentDescriber:
        tags:
            - { name: nelmio_api_doc.route_argument_describer }
1
2
3
4
<!-- config/services.xml -->
<service id="App\Describer\CustomRouteArgumentDescriber">
    <tag name="nelmio_api_doc.route_argument_describer"/>
</service>
1
2
3
4
5
6
7
8
9
// config/services.php
use App\Describer\CustomRouteArgumentDescriber;

return function (ContainerConfigurator $container) {
    $container->services()
        ->set(CustomRouteArgumentDescriber::class)
        ->tag('nelmio_api_doc.route_argument_describer')
    ;
};

Disclaimer

Make sure to use at least php 8.1 (attribute support) to make use of this functionality.

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.
TOC
    Version
    Online Symfony certification, take it now!

    Online Symfony certification, take it now!

    Save your teams and projects before they sink

    Save your teams and projects before they sink

    Version:

    Table of Contents

    • MapQueryString
      • Modify generated documentation
    • MapQueryParameter
      • Modify generated documentation
    • MapRequestPayload
      • Modify generated documentation
    • Complete example
    • Customization
      • Register your route argument describer
    • Disclaimer

    Symfony footer

    Avatar of zenmate, a Symfony contributor

    Thanks zenmate for being a Symfony contributor

    2 commits • 186 lines changed

    View all contributors that help us make Symfony

    Become a Symfony contributor

    Be an active part of the community and contribute ideas, code and bug fixes. Both experts and newcomers are welcome.

    Learn how to contribute

    Symfony™ is a trademark of Symfony SAS. All rights reserved.

    • What is Symfony?

      • What is Symfony?
      • Symfony at a Glance
      • Symfony Components
      • Symfony Releases
      • Security Policy
      • Logo & Screenshots
      • Trademark & Licenses
      • symfony1 Legacy
    • Learn Symfony

      • Symfony Docs
      • Symfony Book
      • Reference
      • Bundles
      • Best Practices
      • Training
      • eLearning Platform
      • Certification
    • Screencasts

      • Learn Symfony
      • Learn PHP
      • Learn JavaScript
      • Learn Drupal
      • Learn RESTful APIs
    • Community

      • Symfony Community
      • SymfonyConnect
      • Events & Meetups
      • Projects using Symfony
      • Contributors
      • Symfony Jobs
      • Backers
      • Code of Conduct
      • Downloads Stats
      • Support
    • Blog

      • All Blog Posts
      • A Week of Symfony
      • Case Studies
      • Cloud
      • Community
      • Conferences
      • Diversity
      • Living on the edge
      • Releases
      • Security Advisories
      • Symfony Insight
      • Twig
      • SensioLabs Blog
    • Services

      • SensioLabs services
      • Train developers
      • Manage your project quality
      • Improve your project performance
      • Host Symfony projects

      Powered by

    Follow Symfony