How to document custom POST Action in API Platform through Swagger Decorator?

Viewed 1372

In the docs there is this example, but it shows only how to add one GET operation. I would like to know how can I add a custom POST route to the documentation.

I am having trouble to show the example body request, with the expected values to be sent (username and email, in this example)

My attempt

<?php
// api/src/Swagger/SwaggerDecorator.php

namespace App\Swagger;

use Symfony\Component\Serializer\Normalizer\NormalizerInterface;

final class SwaggerDecorator implements NormalizerInterface
{
    private $decorated;

    public function __construct(NormalizerInterface $decorated)
    {
        $this->decorated = $decorated;
    }

    public function normalize($object, $format = null, array $context = [])
    {
        $docs = $this->decorated->normalize($object, $format, $context);


        $customDefinition = [
            'tags' => [
                'default'
            ],
            'name' => 'fields',
            'description' => 'Testing decorator',
            'default' => 'id',
            'in' => 'query',
            'requestBody' => 
                [
                    'content' => [
                        'application/json' => [
                            'schema' => [
                                'description' => 'abcd',
                                'required' => [
                                    'username', 'email'
                                ],
                                'properties' => [
                                    'username', 'email'
                                ],
                            ]
                        ]
                    ],
                    'description' => 'testing'
                ],
        ];

        $docs['paths']['/testing']['post']['parameters'][] = $customDefinition;

        return $docs;
    }

    public function supportsNormalization($data, $format = null)
    {
        return $this->decorated->supportsNormalization($data, $format);
    }
}

But it doesn't work. enter image description here

1 Answers

you should not put the whole route declaration inside the parameters array, you should create smth like this:

$docs['paths']['/testing']['post'] = $customDefinition;
Related