在我的 symfony2 项目中,我安装了 FOSRestBundle 和 NelmioApiDocBundle 以创建一个 api。
我的 POST 路由有一个奇怪的行为:当我添加注释属性“输入”时,除了表单字段之外,Nelmio 包还会生成一个额外的参数。这个额外的参数是表单实体本身。
屏幕 :
我尝试在 Nelmio 解析我的路线注释的那一刻进行调试:
我们可以注意到 parameters
属性设置。
这是我的 post action 方法的注释:
/**
* Create a Punchline from the submitted data.
*
* @ApiDoc(
* description = "Creates a new punchline from the submitted data.",
* input = {
* "class" = "Punchline\BackendBundle\Form\Type\PunchlineType",
* "options" = {"method" = "POST"}
* },
* statusCodes = {
* 201 = "Returned when successful",
* 400 = "Returned when the form has errors"
* }
* )
*
* @param Request $request the request object
*
* @return Response
*/
public function postPunchlineAction(Request $request)
这是我的 FormType :
<?php
namespace Punchline\BackendBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class PunchlineType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('content', 'text')
->add('author', 'author_selector')
->add('single', 'single_selector')
;
}
/**
* @param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Punchline\BackendBundle\Entity\Punchline'
));
}
/**
* @return string
*/
public function getName()
{
return 'punchline';
}
}
来自 NelmioApiDocBundle documentation :
If you set input, then the bundle automatically extracts parameters based on the given type, and determines for each parameter its data type, and if it's required or not.
我试图删除表单字段,但参数仍然存在......我没有找到设置这个必需参数的位置。
最佳答案
从您发布的文档中:
Form Types Features
Even if you use FormFactoryInterface::createNamed('', 'your_form_type') the documentation will generate the form type name as the prefix for inputs (your_form_type[param] ... instead of just param).
You can specify which prefix to use with the name key in the input section:
input = { "class" = "your_form_type", "name" = "" }
尝试像这样修改你的@ApiDoc:
* @ApiDoc(
* description = "Creates a new punchline from the submitted data.",
* input = {
* "class" = "Punchline\BackendBundle\Form\Type\PunchlineType",
* "options" = {"method" = "POST"},
* "name" = ""
* },
* statusCodes = {
* 201 = "Returned when successful",
* 400 = "Returned when the form has errors"
* }
* )
关于symfony - NelmioApiDocBundle 根据输入注释生成额外的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29589180/