diff --git a/Parser/JmsMetadataParser.php b/Parser/JmsMetadataParser.php index 1f4664f..269d92f 100644 --- a/Parser/JmsMetadataParser.php +++ b/Parser/JmsMetadataParser.php @@ -13,8 +13,8 @@ namespace Nelmio\ApiDocBundle\Parser; use Metadata\MetadataFactoryInterface; use Nelmio\ApiDocBundle\Util\DocCommentExtractor; -use JMS\SerializerBundle\Metadata\PropertyMetadata; -use JMS\SerializerBundle\Metadata\VirtualPropertyMetadata; +use JMS\Serializer\Metadata\PropertyMetadata; +use JMS\Serializer\Metadata\VirtualPropertyMetadata; /** * Uses the JMS metadata factory to extract input/output model information @@ -73,11 +73,10 @@ class JmsMetadataParser implements ParserInterface //iterate over property metadata foreach ($meta->propertyMetadata as $item) { - if (!is_null($item->type)) { $name = isset($item->serializedName) ? $item->serializedName : $item->name; - $dataType = $this->processDataType(is_string($item->type) ? $item->type : $item->type['name']); + $dataType = $this->processDataType($item); $params[$name] = array( 'dataType' => $dataType['normalized'], @@ -107,21 +106,13 @@ class JmsMetadataParser implements ParserInterface * Figure out a normalized data type (for documentation), and get a * nested class name, if available. * - * @param string $type + * @param PropertyMetadata $type * @return array */ - protected function processDataType($type) + protected function processDataType(PropertyMetadata $item) { - //could be basic type - if ($this->isPrimitive($type)) { - return array( - 'normalized' => $type, - 'class' => null - ); - } - //check for a type inside something that could be treated as an array - if ($nestedType = $this->getNestedTypeInArray($type)) { + if ($nestedType = $this->getNestedTypeInArray($item)) { if ($this->isPrimitive($nestedType)) { return array( 'normalized' => sprintf("array of %ss", $nestedType), @@ -137,6 +128,16 @@ class JmsMetadataParser implements ParserInterface ); } + $type = $item->type['name']; + + //could be basic type + if ($this->isPrimitive($type)) { + return array( + 'normalized' => $type, + 'class' => null + ); + } + //if we got this far, it's a general class name $exp = explode("\\", $type); @@ -155,15 +156,17 @@ class JmsMetadataParser implements ParserInterface * Check the various ways JMS describes values in arrays, and * get the value type in the array * - * @param string $type + * @param PropertyMetadata $item * @return string|null */ - protected function getNestedTypeInArray($type) + protected function getNestedTypeInArray(PropertyMetadata $item) { - //could be some type of array with , or - $regEx = "/\<([A-Za-z0-9\\\]*)(\,?\s?(.*))?\>/"; - if (preg_match($regEx, $type, $matches)) { - return (!empty($matches[3])) ? $matches[3] : $matches[1]; + if (is_array($item->type) + && in_array($item->type['name'], array('array')) // We have to support ArrayCollection as well + && isset($item->type['params']) + && 1 === count($item->type['params']) + && isset($item->type['params'][0]['name'])) { + return $item->type['params'][0]['name']; } return null; diff --git a/Tests/Extractor/ApiDocExtratorTest.php b/Tests/Extractor/ApiDocExtratorTest.php index fadd412..81b437c 100644 --- a/Tests/Extractor/ApiDocExtratorTest.php +++ b/Tests/Extractor/ApiDocExtratorTest.php @@ -12,6 +12,7 @@ namespace Nelmio\ApiDocBundle\Tests\Extractor; use Nelmio\ApiDocBundle\Tests\WebTestCase; +use Symfony\Component\Form\Test\DeprecationErrorHandler; class ApiDocExtractorTest extends WebTestCase { @@ -19,7 +20,9 @@ class ApiDocExtractorTest extends WebTestCase { $container = $this->getContainer(); $extractor = $container->get('nelmio_api_doc.extractor.api_doc_extractor'); + set_error_handler(array('Symfony\Component\Form\Test\DeprecationErrorHandler', 'handle')); $data = $extractor->all(); + restore_error_handler(); $this->assertTrue(is_array($data)); $this->assertCount(14, $data); diff --git a/Tests/Fixtures/Model/JmsNested.php b/Tests/Fixtures/Model/JmsNested.php index ad29652..90b0348 100644 --- a/Tests/Fixtures/Model/JmsNested.php +++ b/Tests/Fixtures/Model/JmsNested.php @@ -1,7 +1,7 @@ getContainer(); $extractor = $container->get('nelmio_api_doc.extractor.api_doc_extractor'); + set_error_handler(array('Symfony\Component\Form\Test\DeprecationErrorHandler', 'handle')); $data = $extractor->all(); + restore_error_handler(); $result = $container->get('nelmio_api_doc.formatter.markdown_formatter')->format($data); $expected = <<getContainer(); $extractor = $container->get('nelmio_api_doc.extractor.api_doc_extractor'); + set_error_handler(array('Symfony\Component\Form\Test\DeprecationErrorHandler', 'handle')); $data = $extractor->all(); + restore_error_handler(); $result = $container->get('nelmio_api_doc.formatter.simple_formatter')->format($data); $expected = array( diff --git a/Tests/Parser/JmsMetadataParserTest.php b/Tests/Parser/JmsMetadataParserTest.php new file mode 100644 index 0000000..88050a9 --- /dev/null +++ b/Tests/Parser/JmsMetadataParserTest.php @@ -0,0 +1,76 @@ +getMock('Metadata\MetadataFactoryInterface'); + $docCommentExtractor = $this->getMockBuilder('Nelmio\ApiDocBundle\Util\DocCommentExtractor') + ->disableOriginalConstructor() + ->getMock(); + + $propertyMetadataFoo = new PropertyMetadata('Nelmio\ApiDocBundle\Tests\Fixtures\Model\JmsNested', 'foo'); + $propertyMetadataFoo->type = array( + 'name' => 'DateTime' + ); + + $propertyMetadataBar = new PropertyMetadata('Nelmio\ApiDocBundle\Tests\Fixtures\Model\JmsNested', 'bar'); + $propertyMetadataBar->type = array( + 'name' => 'string' + ); + + $propertyMetadataBaz = new PropertyMetadata('Nelmio\ApiDocBundle\Tests\Fixtures\Model\JmsNested', 'baz'); + $propertyMetadataBaz->type = array( + 'name' => 'array', + 'params' => array( + array( + 'name' => 'integer', + 'params' => array() + ) + ) + ); + + $metadata = new ClassMetadata('Nelmio\ApiDocBundle\Tests\Fixtures\Model\JmsNested'); + $metadata->addPropertyMetadata($propertyMetadataFoo); + $metadata->addPropertyMetadata($propertyMetadataBar); + $metadata->addPropertyMetadata($propertyMetadataBaz); + + $input = new JmsNested(); + + $metadataFactory->expects($this->once()) + ->method('getMetadataForClass') + ->with($input) + ->will($this->returnValue($metadata)); + + $jmsMetadataParser = new JmsMetadataParser($metadataFactory, $docCommentExtractor); + + $output = $jmsMetadataParser->parse($input); + + $this->assertEquals(array( + 'foo' => array( + 'dataType' => 'DateTime', + 'required' => false, + 'description' => 'No description.', + 'readonly' => false + ), + 'bar' => array( + 'dataType' => 'string', + 'required' => false, + 'description' => 'No description.', + 'readonly' => false + ), + 'baz' => array( + 'dataType' => 'array of integers', + 'required' => false, + 'description' => 'No description.', + 'readonly' => false + ) + ), $output); + } +} \ No newline at end of file diff --git a/composer.json b/composer.json index d69968d..a3afbce 100644 --- a/composer.json +++ b/composer.json @@ -26,7 +26,7 @@ "symfony/validator": ">=2.1,<2.3-dev", "symfony/yaml": ">=2.1,<2.3-dev", "friendsofsymfony/rest-bundle": "dev-master", - "jms/serializer-bundle": "0.9.*" + "jms/serializer-bundle": "1.0.*" }, "minimum-stability": "dev", "autoload": {