symfony - NelmioApiDocBundle 根据输入注释生成额外的参数

标签 symfony annotations jmsserializerbundle

在我的 symfony2 项目中,我安装了 FOSRestBundle 和 NelmioApiDocBundle 以创建一个 api。

我的 POST 路由有一个奇怪的行为:当我添加注释属性“输入”时,除了表单字段之外,Nelmio 包还会生成一个额外的参数。这个额外的参数是表单实体本身。

屏幕 :

Api web interface of the post endpoint

我尝试在 Nelmio 解析我的路线注释的那一刻进行调试:

nelmio debug route annotation parsing

我们可以注意到 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/

相关文章:

symfony - 在 Sylius 中安装 Fixtures - Symfony2

java - 如何扩展 Spring 注解 @Transactional

java - 在 butterknife 中绑定(bind)字符串数组

php - 使用 FOSRestBundle 将 POST 请求主体序列化为数组

orm - 使用 JMSSerialize 序列化遵循 SimplifiedYamlDriver 约定的 Doctrine2 实体

Symfony2 部署到 ubuntu 服务器

php - 用 Doctrine 查询语言写入 "NOT IN"

multithreading - 使用 angularJS 应用程序提高 Symfony2 的性能

java - 反对注解的论点

php - JMS 序列化程序。对象数组的 Xml 标记名称