简体   繁体   English

NelmioApiDocBundle根据输入注释生成一个额外的参数

[英]NelmioApiDocBundle generates an extra parameter based on input annotation

In my symfony2 project, I installed FOSRestBundle and NelmioApiDocBundle in order to create an api. 在我的symfony2项目中,我安装了FOSRestBundle和NelmioApiDocBundle来创建一个api。

I have a strange behavior with my POST route : when I add the annotation property "input", the Nelmio bundle generates an extra parameter in addition to my form fields. 我的POST路线有一个奇怪的行为:当添加注释属性“ input”时,Nelmio包会在表单字段之外生成一个额外的参数。 This extra parameter is the form entity itself. 这个额外的参数是表单实体本身。

Screen : 屏幕:

发布端点的Api Web界面

I tried to debug the moment when Nelmio parses my route's annotations : 我尝试调试Nelmio分析路线注释的时刻:

nelmio调试路由注释解析

We can notice the parameters property is set. 我们可以注意到parameters属性已设置。

Here is the annotations of my post action method : 这是我的后操作方法的注释:

/**
 * 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)

And this is my FormType : 这是我的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';
    }
}

From the NelmioApiDocBundle documentation : NelmioApiDocBundle文档中

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. 如果设置了输入,那么捆绑软件会根据给定的类型自动提取参数,并为每个参数确定其数据类型以及是否需要。

I tried to remove the form fields and the parameter sill remains... I didn't found where I set this required parameter. 我试图删除表单字段,并且仍然保留参数基石...我找不到在哪里设置此必需的参数。

From the documentation you posted: 从您发布的文档中:

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). 即使您使用FormFactoryInterface :: createNamed('','your_form_type'),文档也会生成表单类型名称作为输入的前缀(your_form_type [param] ...而不只是param)。

You can specify which prefix to use with the name key in the input section: 您可以在输入部分中使用名称键指定要使用的前缀:

input = { "class" = "your_form_type", "name" = "" } 输入= {“类别” =“您的表单类型”,“名称” =“”}

Try modifying your @ApiDoc like this: 尝试像这样修改您的@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"
 *   }
 * )

Don't put the FormType as input, but the Entity class that your Type fill. 不要将FormType作为输入,而是将您的Type填充的Entity类作为输入。 if you think about it, your API consumer does not have to know how the data is treated, they only care about the abstract entity they're providing. 如果您考虑一下,您的API使用者就不必知道如何处理数据,他们只在乎所提供的抽象实体。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM