Entity form types breaks the documentation #94

Closed
phidah opened this Issue Nov 13, 2012 · 8 comments

Comments

Projects
None yet
6 participants

phidah commented Nov 13, 2012

I have a method with the following annotation:

/**
 * Create a new product.
 * A product itself is not tied to a particular language - hence no language information is required.
 *
 * @Route("/products")
 * @Method("POST")
 * @ApiDoc(
 *   description="Create new product.",
 *   input="Frisbee\ProductBundle\Form\Type\ProductApiCreateType"
 * )
 */

The ProductApiCreateType form looks like this:

<?php
namespace Frisbee\ProductBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\Collection;

class ProductApiCreateType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('name');
        $builder->add('categories', 'entity', array(
            'class' => 'FrisbeeProductBundle:ProductCategory',
            'property' => 'name',
        ));
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $collectionConstraint = new Collection(array(
            'name' => new NotBlank(),
            'categories' => new NotBlank()
        ));

        $resolver->setDefaults(array(
            'constraints' => $collectionConstraint
        ));
    }

    public function getName()
    {
        return 'product';
    }
}

Whenever the documentation is generated, the following exception is thrown:

The required option "class" is missing.
in /[...]/vendor/symfony/symfony/src/Symfony/Component/OptionsResolver/OptionsResolver.php at line 278

I haven't been able to work around this - any thoughts?

phidah commented Nov 13, 2012

Might be related to this one #59 ?

Owner

Seldaek commented Nov 14, 2012

Can you paste the full stack trace?

phidah commented Nov 14, 2012

The required option "class" is missing.
in /var/www/pm/vendor/symfony/symfony/src/Symfony/Component/OptionsResolver/OptionsResolver.php at line 278    
at OptionsResolver ->validateOptionsCompleteness (array()) 
in /var/www/pm/vendor/symfony/symfony/src/Symfony/Component/OptionsResolver/OptionsResolver.php at line 220    
at OptionsResolver ->resolve (array()) 
in /var/www/pm/vendor/symfony/symfony/src/Symfony/Component/Form/ResolvedFormType.php at line 115    
at ResolvedFormType ->createBuilder (object(FormFactory), 'entity', array(), null) 
in /var/www/pm/vendor/symfony/symfony/src/Symfony/Component/Form/FormFactory.php at line 87    
at FormFactory ->createNamedBuilder ('entity', object(EntityType), null, array(), null) 
in /var/www/pm/vendor/symfony/symfony/src/Symfony/Component/Form/FormFactory.php at line 67    
at FormFactory ->createBuilder (object(EntityType), null, array(), null) 
in /var/www/pm/vendor/symfony/symfony/src/Symfony/Component/Form/FormFactory.php at line 39    
at FormFactory ->create (object(EntityType)) 
in /var/www/pm/src/Nelmio/ApiDocBundle/Parser/FormTypeParser.php at line 101    
at FormTypeParser ->parseForm (object(Form)) 
in /var/www/pm/src/Nelmio/ApiDocBundle/Parser/FormTypeParser.php at line 76    
at FormTypeParser ->parse ('Frisbee\ProductBundle\Form\Type\ProductApiCreateType') 
in /var/www/pm/src/Nelmio/ApiDocBundle/Extractor/ApiDocExtractor.php at line 251    
at ApiDocExtractor ->extractData (object(ApiDoc), object(Route), object(ReflectionMethod)) 
in /var/www/pm/src/Nelmio/ApiDocBundle/Extractor/ApiDocExtractor.php at line 94    
at ApiDocExtractor ->all () 
in /var/www/pm/src/Nelmio/ApiDocBundle/Controller/ApiDocController.php at line 21    
at ApiDocController ->indexAction () 
at call_user_func_array (array(object(ApiDocController), 'indexAction'), array()) 
in /var/www/pm/app/bootstrap.php.cache at line 1426    
at HttpKernel ->handleRaw (object(Request), '1') 
in /var/www/pm/app/bootstrap.php.cache at line 1390    
at HttpKernel ->handle (object(Request), '1', true) 
in /var/www/pm/app/bootstrap.php.cache at line 1566    
at HttpKernel ->handle (object(Request), '1', true) 
in /var/www/pm/app/bootstrap.php.cache at line 617    
at Kernel ->handle (object(Request)) 
in /var/www/pm/web/api_dev.php at line 19    
Contributor

alex88 commented Nov 16, 2012

Bug confirmed here too.. Same issue.. Form working fine in controller, having troubles in the docs

Contributor

alex88 commented Nov 17, 2012

Seems that skipping the Symfony\Bridge\Doctrine\Form\Type\EntityType in the parseForm method in FormTypeParser doesn't gives that exception, so the problem is with that type.

Contributor

alex88 commented Nov 17, 2012

I think that a fix for now is to set the entity field as string in the mapTypes, since most of the time in api calls client should enter some ID or string.

willdurand closed this Mar 17, 2013

bzitzow commented Jun 27, 2013

Although this is no longer throwing an error, it appears the entity field type is still unsupported. Is there a link to another issue on this topic? IE: If that pull request was a temporary fix - is there an issue for a solution to support the entity field type?

wesgood commented Jul 10, 2013

I think for APIs, you have to use a data transformer, as I presume normal Symfony forms include additional metadata for representing an entity in the form. I've had success with an "entity to ID" transformer, like the one at http://lrotherfield.com/blog/symfony2-forms-entity-as-hidden-field/

@damienalexandre damienalexandre pushed a commit to damienalexandre/NelmioApiDocBundle that referenced this issue Dec 12, 2014

@alex88 alex88 Exception handling on unsupported fields types
Since the entity (and maybe others) form field type breaks the doc generations, I've added a try{} catch{} exception handler when you try to a compatible field type in the formParser().

It's a workaround for issue nelmio#94.

Later we can add a better support for other types of fields.
b3d628a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment