diff --git a/Parser/FormTypeParser.php b/Parser/FormTypeParser.php index a84ce26..d67d3f9 100644 --- a/Parser/FormTypeParser.php +++ b/Parser/FormTypeParser.php @@ -279,7 +279,15 @@ class FormTypeParser implements ParserInterface 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) diff --git a/Tests/Fixtures/Form/RequireConstructionType.php b/Tests/Fixtures/Form/RequireConstructionType.php new file mode 100644 index 0000000..4671495 --- /dev/null +++ b/Tests/Fixtures/Form/RequireConstructionType.php @@ -0,0 +1,57 @@ + + * + * 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'; + } +} diff --git a/Tests/Formatter/SimpleFormatterTest.php b/Tests/Formatter/SimpleFormatterTest.php index 38a21ce..6c85ff7 100644 --- a/Tests/Formatter/SimpleFormatterTest.php +++ b/Tests/Formatter/SimpleFormatterTest.php @@ -1080,17 +1080,26 @@ With multiple lines.', 'description' => '', 'sinceVersion' => null, 'untilVersion' => null, + 'actualType' => DataTypes::MODEL, + 'subType' => 'Nelmio\ApiDocBundle\Tests\Fixtures\Model\Test', + 'default' => null, 'children' => array( 'a' => array( 'dataType' => 'string', 'format' => '{length: min: foo}, {not blank}', 'required' => true, - 'readonly' => null + 'readonly' => null, + 'actualType' => DataTypes::STRING, + 'subType' => null, + 'default' => 'nelmio', ), 'b' => array( 'dataType' => 'DateTime', 'required' => null, - 'readonly' => null + 'readonly' => null, + 'actualType' => DataTypes::DATETIME, + 'subType' => null, + 'default' => null, ) ) ) @@ -1207,17 +1216,26 @@ With multiple lines.', 'description' => '', 'sinceVersion' => null, 'untilVersion' => null, + 'actualType' => DataTypes::MODEL, + 'subType' => 'Nelmio\ApiDocBundle\Tests\Fixtures\Model\Test', + 'default' => null, 'children' => array( 'a' => array( 'dataType' => 'string', 'format' => '{length: min: foo}, {not blank}', 'required' => true, - 'readonly' => null + 'readonly' => null, + 'actualType' => DataTypes::STRING, + 'subType' => null, + 'default' => 'nelmio', ), 'b' => array( 'dataType' => 'DateTime', 'required' => null, - 'readonly' => null + 'readonly' => null, + 'actualType' => DataTypes::DATETIME, + 'subType' => null, + 'default' => null, ) ) ) diff --git a/Tests/Parser/FormTypeParserTest.php b/Tests/Parser/FormTypeParserTest.php index 08369f1..5a11485 100644 --- a/Tests/Parser/FormTypeParserTest.php +++ b/Tests/Parser/FormTypeParserTest.php @@ -473,7 +473,57 @@ class FormTypeParserTest extends \PHPUnit_Framework_TestCase '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, + ), + ), + ), + ), + ), ); } }