From fd8ee2679a10cc3f562e11ae28d4d38468724ec2 Mon Sep 17 00:00:00 2001 From: Christophe Coevoet Date: Thu, 21 Jun 2012 00:11:32 +0200 Subject: [PATCH 1/2] Added the support of form types defined as services --- Formatter/AbstractFormatter.php | 2 +- Parser/FormTypeParser.php | 8 +++++--- README.md | 3 ++- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/Formatter/AbstractFormatter.php b/Formatter/AbstractFormatter.php index bb05506..a6f9199 100644 --- a/Formatter/AbstractFormatter.php +++ b/Formatter/AbstractFormatter.php @@ -120,7 +120,7 @@ abstract class AbstractFormatter implements FormatterInterface $data['requirements'] = $requirements; if (null !== $formType = $apiDoc->getFormType()) { - $data['parameters'] = $this->parser->parse(new $formType()); + $data['parameters'] = $this->parser->parse($formType); if ('PUT' === $method) { // All parameters are optional with PUT (update) diff --git a/Parser/FormTypeParser.php b/Parser/FormTypeParser.php index c59c3d0..b19de40 100644 --- a/Parser/FormTypeParser.php +++ b/Parser/FormTypeParser.php @@ -11,7 +11,6 @@ namespace Nelmio\ApiDocBundle\Parser; -use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilder; use Symfony\Component\Form\FormFactoryInterface; @@ -47,11 +46,14 @@ class FormTypeParser * - required * - description * - * @param AbstractType $type + * @param string|\Symfony\Component\Form\FormTypeInterface $type * @return array */ - public function parse(AbstractType $type) + public function parse($type) { + if (is_string($type) && class_exists($type)) { + $type = new $type(); + } $form = $this->formFactory->create($type); $parameters = array(); diff --git a/README.md b/README.md index 365fb87..351471a 100644 --- a/README.md +++ b/README.md @@ -95,7 +95,8 @@ The following properties are available: * `filters`: an array of filters; -* `formType`: the Form Type associated to the method, useful for POST|PUT methods. +* `formType`: the Form Type associated to the method, useful for POST|PUT methods, either as FQCN or + as form type (if it is registered in the form factory in the container) Each _filter_ has to define a `name` parameter, but other parameters are free. Filters are often optional parameters, and you can document them as you want, but keep in mind to be consistent for the whole documentation. From 5b70c775c031d5c26c619eca239b41e21873f503 Mon Sep 17 00:00:00 2001 From: Christophe Coevoet Date: Thu, 21 Jun 2012 00:12:21 +0200 Subject: [PATCH 2/2] Added a test for types defined as services --- Tests/Extractor/ApiDocExtratorTest.php | 2 +- Tests/Fixtures/Controller/TestController.php | 10 ++++ Tests/Fixtures/Form/DependencyType.php | 50 ++++++++++++++++++++ Tests/Fixtures/app/config/default.yml | 6 +++ Tests/Fixtures/app/config/routing.yml | 6 +++ Tests/Formatter/MarkdownFormatterTest.php | 13 +++++ Tests/Formatter/SimpleFormatterTest.php | 24 ++++++++-- 7 files changed, 107 insertions(+), 4 deletions(-) create mode 100644 Tests/Fixtures/Form/DependencyType.php diff --git a/Tests/Extractor/ApiDocExtratorTest.php b/Tests/Extractor/ApiDocExtratorTest.php index 8e912ab..8990184 100644 --- a/Tests/Extractor/ApiDocExtratorTest.php +++ b/Tests/Extractor/ApiDocExtratorTest.php @@ -22,7 +22,7 @@ class ApiDocExtractorTest extends WebTestCase $data = $extractor->all(); $this->assertTrue(is_array($data)); - $this->assertCount(8, $data); + $this->assertCount(9, $data); foreach ($data as $d) { $this->assertTrue(is_array($d)); diff --git a/Tests/Fixtures/Controller/TestController.php b/Tests/Fixtures/Controller/TestController.php index def145b..57907eb 100644 --- a/Tests/Fixtures/Controller/TestController.php +++ b/Tests/Fixtures/Controller/TestController.php @@ -71,4 +71,14 @@ class TestController public function yetAnotherAction() { } + + /** + * @ApiDoc( + * description="create another test", + * formType="dependency_type" + * ) + */ + public function anotherPostAction() + { + } } diff --git a/Tests/Fixtures/Form/DependencyType.php b/Tests/Fixtures/Form/DependencyType.php new file mode 100644 index 0000000..9f7731b --- /dev/null +++ b/Tests/Fixtures/Form/DependencyType.php @@ -0,0 +1,50 @@ + + * + * 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 DependencyType extends AbstractType +{ + public function __construct(array $mandatoryArgument) + { + } + + /** + * {@inheritdoc} + */ + public function buildForm(FormBuilderInterface $builder, array $options) + { + $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 'dependency_type'; + } +} diff --git a/Tests/Fixtures/app/config/default.yml b/Tests/Fixtures/app/config/default.yml index 6b4401c..d159f8d 100644 --- a/Tests/Fixtures/app/config/default.yml +++ b/Tests/Fixtures/app/config/default.yml @@ -22,3 +22,9 @@ twig: services: nemlio.test.controller: class: Nelmio\ApiDocBundle\Tests\Fixtures\Controller\TestServiceController + nelmio.test.type: + class: Nelmio\ApiDocBundle\Tests\Fixtures\Form\DependencyType + arguments: + - [foo, bar] + tags: + - { name: form.type, alias: dependency_type } diff --git a/Tests/Fixtures/app/config/routing.yml b/Tests/Fixtures/app/config/routing.yml index 7e7b090..6e04d20 100644 --- a/Tests/Fixtures/app/config/routing.yml +++ b/Tests/Fixtures/app/config/routing.yml @@ -28,6 +28,12 @@ test_route_6: requirements: id: \d+ +test_route_7: + pattern: /another-post + defaults: { _controller: NelmioApiDocTestBundle:Test:anotherPost, _format: json } + requirements: + _method: POST + test_service_route_1: pattern: /tests defaults: { _controller: nemlio.test.controller:indexAction, _format: json } diff --git a/Tests/Formatter/MarkdownFormatterTest.php b/Tests/Formatter/MarkdownFormatterTest.php index ddb986d..11223b5 100644 --- a/Tests/Formatter/MarkdownFormatterTest.php +++ b/Tests/Formatter/MarkdownFormatterTest.php @@ -107,6 +107,19 @@ b: # others # +### `POST` /another-post ### + +_create another test_ + +#### Parameters #### + +a: + + * type: string + * required: true + * description: A nice description + + ### `ANY` /any ### _Action without HTTP verb_ diff --git a/Tests/Formatter/SimpleFormatterTest.php b/Tests/Formatter/SimpleFormatterTest.php index 216e899..1b6e975 100644 --- a/Tests/Formatter/SimpleFormatterTest.php +++ b/Tests/Formatter/SimpleFormatterTest.php @@ -140,6 +140,24 @@ class SimpleFormatterTest extends WebTestCase 'others' => array( 0 => + array( + 'method' => 'POST', + 'uri' => '/another-post', + 'requirements' => + array( + ), + 'parameters' => + array( + 'a' => + array( + 'dataType' => 'string', + 'required' => true, + 'description' => 'A nice description', + ), + ), + 'description' => 'create another test', + ), + 1 => array( 'method' => 'ANY', 'uri' => '/any', @@ -148,7 +166,7 @@ class SimpleFormatterTest extends WebTestCase ), 'description' => 'Action without HTTP verb', ), - 1 => + 2 => array( 'method' => 'ANY', 'uri' => '/any/{foo}', @@ -158,7 +176,7 @@ class SimpleFormatterTest extends WebTestCase ), 'description' => 'Action without HTTP verb', ), - 2 => + 3 => array( 'method' => 'ANY', 'uri' => '/my-commented/{id}/{page}', @@ -169,7 +187,7 @@ class SimpleFormatterTest extends WebTestCase ), 'description' => 'This method is useful to test if the getDocComment works. And, it supports multilines until the first \'@\' char.', ), - 3 => + 4 => array( 'method' => 'ANY', 'uri' => '/yet-another/{id}',