Add support for using name in the input and output options for JsonSerializable and validation parsers

This commit is contained in:
Fred Cox 2015-07-01 14:01:44 +03:00
parent 37c6465700
commit 406a4e1b5b
5 changed files with 42 additions and 14 deletions

View File

@ -31,15 +31,21 @@ class JsonSerializableParser implements ParserInterface
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function parse(array $item) public function parse(array $input)
{ {
/** @var \JsonSerializable $obj */ /** @var \JsonSerializable $obj */
$obj = new $item['class'](); $obj = new $input['class']();
$encoded = $obj->jsonSerialize(); $encoded = $obj->jsonSerialize();
$top = $this->getItemMetaData($encoded); $parsed = $this->getItemMetaData($encoded);
return $top['children']; if (isset($input['name']) && !empty($input['name'])) {
$output = array();
$output[$input['name']] = $parsed;
return $output;
}
return $parsed['children'];
} }
public function getItemMetaData($item) public function getItemMetaData($item)
@ -47,10 +53,12 @@ class JsonSerializableParser implements ParserInterface
$type = gettype($item); $type = gettype($item);
$meta = array( $meta = array(
'dataType' => $type, 'dataType' => $type == 'NULL' ? null : $type,
'required' => true, 'actualType' => $type,
'description' => '', 'subType' => null,
'readonly' => false 'required' => null,
'description' => null,
'readonly' => null
); );
if ($type == 'object' && $item instanceof \JsonSerializable) { if ($type == 'object' && $item instanceof \JsonSerializable) {

View File

@ -69,7 +69,24 @@ class ValidationParser implements ParserInterface, PostParserInterface
{ {
$className = $input['class']; $className = $input['class'];
return $this->doParse($className, array()); $parsed = $this->doParse($className, array());
if (isset($input['name']) && !empty($input['name'])) {
$output = array();
$output[$input['name']] = array(
'dataType' => 'object',
'actualType' => 'object',
'class' => $className,
'subType' => null,
'required' => null,
'description' => null,
'readonly' => null,
'children' => $parsed
);
return $output;
}
return $parsed;
} }
/** /**

View File

@ -70,8 +70,9 @@
<tag name="nelmio_api_doc.extractor.parser" /> <tag name="nelmio_api_doc.extractor.parser" />
</service> </service>
<!-- priority=1 means it comes before the validation parser, which can often add better type information -->
<service id="nelmio_api_doc.parser.json_serializable_parser" class="%nelmio_api_doc.parser.json_serializable_parser.class%"> <service id="nelmio_api_doc.parser.json_serializable_parser" class="%nelmio_api_doc.parser.json_serializable_parser.class%">
<tag name="nelmio_api_doc.extractor.parser" /> <tag name="nelmio_api_doc.extractor.parser" priority="1" />
</service> </service>
</services> </services>

View File

@ -164,7 +164,7 @@ class YourController
* `parameters`: an array of parameters; * `parameters`: an array of parameters;
* `input`: the input type associated to the method (currently this supports Form Types, classes with JMS Serializer * `input`: the input type associated to the method (currently this supports Form Types, classes with JMS Serializer
metadata, and classes with Validation component metadata) useful for POST|PUT methods, either as FQCN or as form type metadata, classes with Validation component metadata and classes that implement JsonSerializable) useful for POST|PUT methods, either as FQCN or as form type
(if it is registered in the form factory in the container). (if it is registered in the form factory in the container).
* `output`: the output type associated with the response. Specified and parsed the same way as `input`. * `output`: the output type associated with the response. Specified and parsed the same way as `input`.

View File

@ -64,9 +64,11 @@ class JsonSerializableParserTest extends \PHPUnit_Framework_TestCase
'children' => array( 'children' => array(
'value' => array( 'value' => array(
'dataType' => 'array', 'dataType' => 'array',
'required' => true, 'actualType' => 'array',
'description' => '', 'subType' => null,
'readonly' => false 'required' => null,
'description' => null,
'readonly' => null
) )
) )
) )