mirror of
https://github.com/retailcrm/NelmioApiDocBundle.git
synced 2025-02-09 02:59:27 +03:00
Merge pull request #28 from stof/service_form_types
Added the support of form types defined as services
This commit is contained in:
commit
5bcdbcb808
@ -120,7 +120,7 @@ abstract class AbstractFormatter implements FormatterInterface
|
|||||||
$data['requirements'] = $requirements;
|
$data['requirements'] = $requirements;
|
||||||
|
|
||||||
if (null !== $formType = $apiDoc->getFormType()) {
|
if (null !== $formType = $apiDoc->getFormType()) {
|
||||||
$data['parameters'] = $this->parser->parse(new $formType());
|
$data['parameters'] = $this->parser->parse($formType);
|
||||||
|
|
||||||
if ('PUT' === $method) {
|
if ('PUT' === $method) {
|
||||||
// All parameters are optional with PUT (update)
|
// All parameters are optional with PUT (update)
|
||||||
|
@ -11,7 +11,6 @@
|
|||||||
|
|
||||||
namespace Nelmio\ApiDocBundle\Parser;
|
namespace Nelmio\ApiDocBundle\Parser;
|
||||||
|
|
||||||
use Symfony\Component\Form\AbstractType;
|
|
||||||
use Symfony\Component\Form\FormBuilder;
|
use Symfony\Component\Form\FormBuilder;
|
||||||
use Symfony\Component\Form\FormFactoryInterface;
|
use Symfony\Component\Form\FormFactoryInterface;
|
||||||
|
|
||||||
@ -47,11 +46,14 @@ class FormTypeParser
|
|||||||
* - required
|
* - required
|
||||||
* - description
|
* - description
|
||||||
*
|
*
|
||||||
* @param AbstractType $type
|
* @param string|\Symfony\Component\Form\FormTypeInterface $type
|
||||||
* @return array
|
* @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);
|
$form = $this->formFactory->create($type);
|
||||||
|
|
||||||
$parameters = array();
|
$parameters = array();
|
||||||
|
@ -95,7 +95,8 @@ The following properties are available:
|
|||||||
|
|
||||||
* `filters`: an array of filters;
|
* `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
|
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.
|
parameters, and you can document them as you want, but keep in mind to be consistent for the whole documentation.
|
||||||
|
@ -22,7 +22,7 @@ class ApiDocExtractorTest extends WebTestCase
|
|||||||
$data = $extractor->all();
|
$data = $extractor->all();
|
||||||
|
|
||||||
$this->assertTrue(is_array($data));
|
$this->assertTrue(is_array($data));
|
||||||
$this->assertCount(8, $data);
|
$this->assertCount(9, $data);
|
||||||
|
|
||||||
foreach ($data as $d) {
|
foreach ($data as $d) {
|
||||||
$this->assertTrue(is_array($d));
|
$this->assertTrue(is_array($d));
|
||||||
|
@ -71,4 +71,14 @@ class TestController
|
|||||||
public function yetAnotherAction()
|
public function yetAnotherAction()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ApiDoc(
|
||||||
|
* description="create another test",
|
||||||
|
* formType="dependency_type"
|
||||||
|
* )
|
||||||
|
*/
|
||||||
|
public function anotherPostAction()
|
||||||
|
{
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
50
Tests/Fixtures/Form/DependencyType.php
Normal file
50
Tests/Fixtures/Form/DependencyType.php
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
<?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 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';
|
||||||
|
}
|
||||||
|
}
|
@ -22,3 +22,9 @@ twig:
|
|||||||
services:
|
services:
|
||||||
nemlio.test.controller:
|
nemlio.test.controller:
|
||||||
class: Nelmio\ApiDocBundle\Tests\Fixtures\Controller\TestServiceController
|
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 }
|
||||||
|
@ -28,6 +28,12 @@ test_route_6:
|
|||||||
requirements:
|
requirements:
|
||||||
id: \d+
|
id: \d+
|
||||||
|
|
||||||
|
test_route_7:
|
||||||
|
pattern: /another-post
|
||||||
|
defaults: { _controller: NelmioApiDocTestBundle:Test:anotherPost, _format: json }
|
||||||
|
requirements:
|
||||||
|
_method: POST
|
||||||
|
|
||||||
test_service_route_1:
|
test_service_route_1:
|
||||||
pattern: /tests
|
pattern: /tests
|
||||||
defaults: { _controller: nemlio.test.controller:indexAction, _format: json }
|
defaults: { _controller: nemlio.test.controller:indexAction, _format: json }
|
||||||
|
@ -107,6 +107,19 @@ b:
|
|||||||
|
|
||||||
# others #
|
# others #
|
||||||
|
|
||||||
|
### `POST` /another-post ###
|
||||||
|
|
||||||
|
_create another test_
|
||||||
|
|
||||||
|
#### Parameters ####
|
||||||
|
|
||||||
|
a:
|
||||||
|
|
||||||
|
* type: string
|
||||||
|
* required: true
|
||||||
|
* description: A nice description
|
||||||
|
|
||||||
|
|
||||||
### `ANY` /any ###
|
### `ANY` /any ###
|
||||||
|
|
||||||
_Action without HTTP verb_
|
_Action without HTTP verb_
|
||||||
|
@ -140,6 +140,24 @@ class SimpleFormatterTest extends WebTestCase
|
|||||||
'others' =>
|
'others' =>
|
||||||
array(
|
array(
|
||||||
0 =>
|
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(
|
array(
|
||||||
'method' => 'ANY',
|
'method' => 'ANY',
|
||||||
'uri' => '/any',
|
'uri' => '/any',
|
||||||
@ -148,7 +166,7 @@ class SimpleFormatterTest extends WebTestCase
|
|||||||
),
|
),
|
||||||
'description' => 'Action without HTTP verb',
|
'description' => 'Action without HTTP verb',
|
||||||
),
|
),
|
||||||
1 =>
|
2 =>
|
||||||
array(
|
array(
|
||||||
'method' => 'ANY',
|
'method' => 'ANY',
|
||||||
'uri' => '/any/{foo}',
|
'uri' => '/any/{foo}',
|
||||||
@ -158,7 +176,7 @@ class SimpleFormatterTest extends WebTestCase
|
|||||||
),
|
),
|
||||||
'description' => 'Action without HTTP verb',
|
'description' => 'Action without HTTP verb',
|
||||||
),
|
),
|
||||||
2 =>
|
3 =>
|
||||||
array(
|
array(
|
||||||
'method' => 'ANY',
|
'method' => 'ANY',
|
||||||
'uri' => '/my-commented/{id}/{page}',
|
'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.',
|
'description' => 'This method is useful to test if the getDocComment works. And, it supports multilines until the first \'@\' char.',
|
||||||
),
|
),
|
||||||
3 =>
|
4 =>
|
||||||
array(
|
array(
|
||||||
'method' => 'ANY',
|
'method' => 'ANY',
|
||||||
'uri' => '/yet-another/{id}',
|
'uri' => '/yet-another/{id}',
|
||||||
|
Loading…
x
Reference in New Issue
Block a user