symfony - 如何通过 Swagger 装饰器在 API 平台中记录自定义 POST 操作?
问题描述
在文档中有这个例子,但它只显示了如何添加一个 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);
}
}
解决方案
你不应该把整个路由声明放在参数数组中,你应该像这样创建:
$docs['paths']['/testing']['post'] = $customDefinition;
推荐阅读
- reactjs - React - 从 Redux Store 设置受控输入值
- java - 用户使用扫描仪和数组的多个输入
- asp.net-core - 从另一个获取一个动作的 ActionContext
- scala - 回滚失败发布流程
- apache-spark - How can I apply the reduceByKey function to a .distinct() object in PySpark?
- git - 在 Makefile 中制作正确的命令“git credential-osxkeychain”或让 git 在推送时询问我的密码和电子邮件
- java - 有没有办法读取从 Firebase 下载的 Excel 文件?
- python - Pandas drop subset of dataframe
- php - Getting PHP error "Fatal error: require_once() Failed opening required
" - c++ - Any difference between `auto* p = &n` and `auto p = &n`?