Merge pull request #387 from giosh94mhz/form_type_parser_should_use_type_constructor

This commit is contained in:
William DURAND 2014-06-27 10:59:06 +02:00
commit 94ec568237
4 changed files with 139 additions and 6 deletions

View File

@ -279,7 +279,15 @@ class FormTypeParser implements ParserInterface
private function getTypeInstance($type) private function getTypeInstance($type)
{ {
return unserialize(sprintf('O:%d:"%s":0:{}', strlen($type), $type)); $refl = new \ReflectionClass($type);
$constructor = $refl->getConstructor();
// this fallback may lead to runtime exception, but try hard to generate the docs
if ($constructor && $constructor->getNumberOfRequiredParameters() > 0) {
return $refl->newInstanceWithoutConstructor();
}
return $refl->newInstance();
} }
private function createForm($item) private function createForm($item)

View File

@ -0,0 +1,57 @@
<?php
/*
* This file is part of the NelmioApiDocBundle.
*
* (c) Nelmio <hello@nelm.io>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Nelmio\ApiDocBundle\Tests\Fixtures\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class RequireConstructionType extends AbstractType
{
private $noThrow;
public function __construct($optionalArgs = null)
{
$this->noThrow = true;
}
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
if ($this->noThrow !== true)
throw new \RuntimeException(__CLASS__ . " require contruction");
$builder
->add('a', null, array('description' => 'A nice description'))
;
}
/**
* {@inheritdoc}
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Nelmio\ApiDocBundle\Tests\Fixtures\Model\Test',
));
return;
}
public function getName()
{
return 'require_construction_type';
}
}

View File

@ -1080,17 +1080,26 @@ With multiple lines.',
'description' => '', 'description' => '',
'sinceVersion' => null, 'sinceVersion' => null,
'untilVersion' => null, 'untilVersion' => null,
'actualType' => DataTypes::MODEL,
'subType' => 'Nelmio\ApiDocBundle\Tests\Fixtures\Model\Test',
'default' => null,
'children' => array( 'children' => array(
'a' => array( 'a' => array(
'dataType' => 'string', 'dataType' => 'string',
'format' => '{length: min: foo}, {not blank}', 'format' => '{length: min: foo}, {not blank}',
'required' => true, 'required' => true,
'readonly' => null 'readonly' => null,
'actualType' => DataTypes::STRING,
'subType' => null,
'default' => 'nelmio',
), ),
'b' => array( 'b' => array(
'dataType' => 'DateTime', 'dataType' => 'DateTime',
'required' => null, 'required' => null,
'readonly' => null 'readonly' => null,
'actualType' => DataTypes::DATETIME,
'subType' => null,
'default' => null,
) )
) )
) )
@ -1207,17 +1216,26 @@ With multiple lines.',
'description' => '', 'description' => '',
'sinceVersion' => null, 'sinceVersion' => null,
'untilVersion' => null, 'untilVersion' => null,
'actualType' => DataTypes::MODEL,
'subType' => 'Nelmio\ApiDocBundle\Tests\Fixtures\Model\Test',
'default' => null,
'children' => array( 'children' => array(
'a' => array( 'a' => array(
'dataType' => 'string', 'dataType' => 'string',
'format' => '{length: min: foo}, {not blank}', 'format' => '{length: min: foo}, {not blank}',
'required' => true, 'required' => true,
'readonly' => null 'readonly' => null,
'actualType' => DataTypes::STRING,
'subType' => null,
'default' => 'nelmio',
), ),
'b' => array( 'b' => array(
'dataType' => 'DateTime', 'dataType' => 'DateTime',
'required' => null, 'required' => null,
'readonly' => null 'readonly' => null,
'actualType' => DataTypes::DATETIME,
'subType' => null,
'default' => null,
) )
) )
) )

View File

@ -473,7 +473,57 @@ class FormTypeParserTest extends \PHPUnit_Framework_TestCase
'readonly' => false, 'readonly' => false,
), ),
), ),
) ),
array(
array('class' => 'Nelmio\ApiDocBundle\Tests\Fixtures\Form\RequireConstructionType'),
array(
'require_construction_type' => array(
'dataType' => 'object (RequireConstructionType)',
'required' => true,
'description' => '',
'readonly' => false,
'default' => null,
'actualType' => 'model',
'subType' => 'Nelmio\ApiDocBundle\Tests\Fixtures\Form\RequireConstructionType',
'children' => array(
'a' => array(
'dataType' => 'string',
'actualType' => 'string',
'subType' => null,
'default' => null,
'required' => true,
'description' => 'A nice description',
'readonly' => false,
),
),
),
),
),
array(
array('class' => 'Nelmio\ApiDocBundle\Tests\Fixtures\Form\DependencyType'),
array(
'dependency_type' => array(
'dataType' => 'object (DependencyType)',
'required' => true,
'description' => '',
'readonly' => false,
'default' => null,
'actualType' => 'model',
'subType' => 'Nelmio\ApiDocBundle\Tests\Fixtures\Form\DependencyType',
'children' => array(
'a' => array(
'dataType' => 'string',
'actualType' => 'string',
'subType' => null,
'default' => null,
'required' => true,
'description' => 'A nice description',
'readonly' => false,
),
),
),
),
),
); );
} }
} }