mirror of
https://github.com/retailcrm/NelmioApiDocBundle.git
synced 2025-02-08 18:49:26 +03:00
updated JMSMetadataParser to support nested models
This commit is contained in:
parent
b085338166
commit
185d0e588b
@ -61,26 +61,97 @@ class JmsMetadataParser implements ParserInterface
|
||||
if (!is_null($item->type)) {
|
||||
$name = isset($item->serializedName) ? $item->serializedName : $item->name;
|
||||
|
||||
//TODO: check for nested type
|
||||
|
||||
$params[$name] = array(
|
||||
'dataType' => $item->type,
|
||||
'dataType' => $this->getNormalizedType($item->type),
|
||||
'required' => false, //TODO: can't think of a good way to specify this one, JMS doesn't have a setting for this
|
||||
'description' => $this->getDescription($input, $item->name),
|
||||
'readonly' => $item->readOnly
|
||||
);
|
||||
|
||||
//check for nested classes w/ JMS metadata
|
||||
if ($nestedInputClass = $this->getNestedClass($item->type)) {
|
||||
$params[$name]['children'] = $this->parse($nestedInputClass);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $params;
|
||||
}
|
||||
|
||||
/**
|
||||
* There are various ways via JMS to declare arrays of objects, but that's an internal
|
||||
* implementation detail.
|
||||
*
|
||||
* @param string $type
|
||||
* @return string
|
||||
*/
|
||||
protected function getNormalizedType($type)
|
||||
{
|
||||
if (in_array($type, array('boolean', 'integer', 'string', 'double', 'array', 'DateTime'))) {
|
||||
return $type;
|
||||
}
|
||||
|
||||
if(false !== strpos($type, "array") || false !== strpos($type, "ArrayCollection")) {
|
||||
if ($nested = $this->getNestedClassInArray($type)) {
|
||||
$exp = explode("\\", $nested);
|
||||
return sprintf("array of objects (%s)", end($exp));
|
||||
} else {
|
||||
return "array";
|
||||
}
|
||||
}
|
||||
|
||||
$exp = explode("\\", $type);
|
||||
return sprintf("object (%s)", end($exp));
|
||||
}
|
||||
|
||||
/**
|
||||
* Check the various ways JMS describes custom classes in arrays, and
|
||||
* get the name of the class in the array, if available.
|
||||
*
|
||||
* @param string $type
|
||||
* @return string|false
|
||||
*/
|
||||
protected function getNestedClassInArray($type)
|
||||
{
|
||||
|
||||
//could be some type of array with <V>, or <K,V>
|
||||
$regEx = "/\<([A-Za-z0-9\\\]*)(\,?\s?(.*))?\>/";
|
||||
if (preg_match($regEx, $type, $matches)) {
|
||||
$matched = (!empty($matches[3])) ? $matches[3] : $matches[1];
|
||||
return in_array($matched, array('boolean', 'integer', 'string', 'double', 'array', 'DateTime')) ? false : $matched;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Scan the JMS Serializer types for reference to a class.
|
||||
*
|
||||
* http://jmsyst.com/bundles/JMSSerializerBundle/master/reference/annotations#type
|
||||
*
|
||||
* @param string $type
|
||||
* @return string|false
|
||||
*/
|
||||
protected function getNestedClass($type)
|
||||
{
|
||||
if (in_array($type, array('boolean', 'integer', 'string', 'double', 'array', 'DateTime'))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
//could be a nested object of some sort
|
||||
if ($nested = $this->getNestedClassInArray($type)) {
|
||||
return $nested;
|
||||
}
|
||||
|
||||
//or could just be a class name (potentially)
|
||||
return (null === $this->factory->getMetadataForClass($type)) ? false : $type;
|
||||
}
|
||||
|
||||
protected function getDescription($className, $propertyName)
|
||||
{
|
||||
$description = "No description.";
|
||||
|
||||
//TODO: regex comment to get description - or move doc comment parsing functionality from `ApiDocExtractor` to a new location
|
||||
//in order to reuse it here
|
||||
//TODO: abstract docblock parsing utility and implement here
|
||||
|
||||
return $description;
|
||||
}
|
||||
|
@ -31,6 +31,8 @@ interface ParserInterface
|
||||
* - required boolean
|
||||
* - description string
|
||||
* - readonly boolean
|
||||
* - children (optional) array of nested property names mapped to arrays
|
||||
* in the format described here
|
||||
*
|
||||
* @param string $item The string type of input to parse.
|
||||
* @return array
|
||||
|
20
Tests/Fixtures/Model/JmsNested.php
Normal file
20
Tests/Fixtures/Model/JmsNested.php
Normal file
@ -0,0 +1,20 @@
|
||||
<?php
|
||||
namespace Nelmio\ApiDocBundle\Tests\Fixtures\Model;
|
||||
|
||||
use JMS\SerializerBundle\Annotation as JMS;
|
||||
|
||||
class JmsNested
|
||||
{
|
||||
|
||||
/**
|
||||
* @JMS\Type("DateTime");
|
||||
* @JMS\ReadOnly
|
||||
*/
|
||||
public $foo;
|
||||
|
||||
/**
|
||||
* @JMS\Type("string");
|
||||
*/
|
||||
public $bar;
|
||||
|
||||
}
|
@ -29,5 +29,15 @@ class JmsTest
|
||||
* @JMS\Type("array");
|
||||
*/
|
||||
public $arr;
|
||||
|
||||
/**
|
||||
* @JMS\Type("Nelmio\ApiDocBundle\Tests\Fixtures\Model\JmsNested");
|
||||
*/
|
||||
public $nested;
|
||||
|
||||
/**
|
||||
* @JMS\Type("array<Nelmio\ApiDocBundle\Tests\Fixtures\Model\JmsNested>");
|
||||
*/
|
||||
public $nestedArray;
|
||||
|
||||
}
|
||||
|
@ -185,6 +185,18 @@ arr:
|
||||
* required: false
|
||||
* description: No description.
|
||||
|
||||
nested:
|
||||
|
||||
* type: object (JmsNested)
|
||||
* required: false
|
||||
* description: No description.
|
||||
|
||||
nestedArray:
|
||||
|
||||
* type: array of objects (JmsNested)
|
||||
* required: false
|
||||
* description: No description.
|
||||
|
||||
|
||||
### `ANY` /my-commented/{id}/{page} ###
|
||||
|
||||
|
@ -210,7 +210,47 @@ class SimpleFormatterTest extends WebTestCase
|
||||
'required' => false,
|
||||
'description' => 'No description.',
|
||||
'readonly' => false
|
||||
)
|
||||
),
|
||||
'nested' => array(
|
||||
'dataType' => 'object (JmsNested)',
|
||||
'required' => false,
|
||||
'description' => 'No description.',
|
||||
'readonly' => false,
|
||||
'children' => array(
|
||||
'foo' => array(
|
||||
'dataType' => 'DateTime',
|
||||
'required' => false,
|
||||
'description' => 'No description.',
|
||||
'readonly' => true,
|
||||
),
|
||||
'bar' => array(
|
||||
'dataType' => 'string',
|
||||
'required' => false,
|
||||
'description' => 'No description.',
|
||||
'readonly' => false,
|
||||
)
|
||||
)
|
||||
),
|
||||
'nestedArray' => array(
|
||||
'dataType' => 'array of objects (JmsNested)',
|
||||
'required' => false,
|
||||
'description' => 'No description.',
|
||||
'readonly' => false,
|
||||
'children' => array(
|
||||
'foo' => array(
|
||||
'dataType' => 'DateTime',
|
||||
'required' => false,
|
||||
'description' => 'No description.',
|
||||
'readonly' => true,
|
||||
),
|
||||
'bar' => array(
|
||||
'dataType' => 'string',
|
||||
'required' => false,
|
||||
'description' => 'No description.',
|
||||
'readonly' => false,
|
||||
)
|
||||
)
|
||||
),
|
||||
),
|
||||
'description' => 'Testing JMS'
|
||||
),
|
||||
|
Loading…
x
Reference in New Issue
Block a user