【问题标题】:How to document custom POST Action in API Platform through Swagger Decorator?如何通过 Swagger 装饰器在 API 平台中记录自定义 POST 操作?
【发布时间】:2020-04-08 21:29:08
【问题描述】:

In the docs there is this example,但它只显示如何添加一个 GET 操作。 我想知道如何在文档中添加自定义 POST 路由。

我无法显示示例正文请求以及要发送的预期值(在此示例中为用户名和电子邮件)

我的尝试

<?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);
    }
}

但它不起作用。

【问题讨论】:

    标签: symfony swagger openapi api-platform.com


    【解决方案1】:

    你不应该把整个路由声明放在参数数组中,你应该像这样创建:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-28
      • 1970-01-01
      • 2021-03-30
      • 1970-01-01
      • 1970-01-01
      • 2018-04-11
      • 1970-01-01
      • 2014-06-08
      相关资源
      最近更新 更多