From befeeb3e3fd15b36530a51bf1980bb14df8b9338 Mon Sep 17 00:00:00 2001 From: Lukas Kahwe Smith Date: Mon, 3 Dec 2012 16:32:15 +0100 Subject: [PATCH 1/4] JMSSerializerBundle 1.0 compatibility --- Parser/JmsMetadataParser.php | 4 ++-- Tests/Fixtures/Model/JmsNested.php | 2 +- Tests/Fixtures/Model/JmsTest.php | 2 +- composer.json | 4 ++-- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/Parser/JmsMetadataParser.php b/Parser/JmsMetadataParser.php index 1f4664f..73ef591 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 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 @@ =2.1,<2.3-dev", "symfony/validator": ">=2.1,<2.3-dev", "symfony/yaml": ">=2.1,<2.3-dev", - "friendsofsymfony/rest-bundle": "dev-master", - "jms/serializer-bundle": "0.9.*" + "friendsofsymfony/rest-bundle": "dev-jms_serlializer_1_0", + "jms/serializer-bundle": "1.0.*" }, "minimum-stability": "dev", "autoload": { From 07bb37ac76201211424bee9ebf14af87ba8ab646 Mon Sep 17 00:00:00 2001 From: Stefano Sala Date: Mon, 11 Feb 2013 14:42:17 +0100 Subject: [PATCH 2/4] Finish upgrade to jms serializer 1.0 Updated deprecated MinLength assertion to Length Updated array of object parsing Handled deprecated calls because of using AbstractType (not sure if it is the best way, though) --- Parser/JmsMetadataParser.php | 39 ++++++------ Tests/Extractor/ApiDocExtratorTest.php | 3 + Tests/Fixtures/Model/Test.php | 2 +- Tests/Fixtures/app/config/default.yml | 6 +- Tests/Formatter/MarkdownFormatterTest.php | 2 + Tests/Formatter/SimpleFormatterTest.php | 2 + Tests/Parser/JmsMetadataParserTest.php | 74 +++++++++++++++++++++++ 7 files changed, 104 insertions(+), 24 deletions(-) create mode 100644 Tests/Parser/JmsMetadataParserTest.php diff --git a/Parser/JmsMetadataParser.php b/Parser/JmsMetadataParser.php index 73ef591..090d224 100644 --- a/Parser/JmsMetadataParser.php +++ b/Parser/JmsMetadataParser.php @@ -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,15 @@ class JmsMetadataParser implements ParserInterface * Figure out a normalized data type (for documentation), and get a * nested class name, if available. * - * @param string $type + * @param array|string $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 - ); - } + $type = is_string($item->type) ? $item->type : $item->type['name']; //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 +130,14 @@ class JmsMetadataParser implements ParserInterface ); } + //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 array|string $item * @return string|null */ - protected function getNestedTypeInArray($type) + protected function getNestedTypeInArray($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')) + && 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 17a4e97..ae2f9f9 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(13, $data); diff --git a/Tests/Fixtures/Model/Test.php b/Tests/Fixtures/Model/Test.php index 29155d8..8037d3a 100644 --- a/Tests/Fixtures/Model/Test.php +++ b/Tests/Fixtures/Model/Test.php @@ -16,7 +16,7 @@ use Symfony\Component\Validator\Constraints as Assert; class Test { /** - * @Assert\MinLength("foo"); + * @Assert\Length(min="foo"); * @Assert\NotBlank */ public $a; diff --git a/Tests/Fixtures/app/config/default.yml b/Tests/Fixtures/app/config/default.yml index 6c822c9..6d596bc 100644 --- a/Tests/Fixtures/app/config/default.yml +++ b/Tests/Fixtures/app/config/default.yml @@ -30,13 +30,9 @@ services: #JMS Serializer config for testing JmsMetadataParser jms_serializer: handlers: - object_based: false datetime: - format: "Y-m-dTH:i:s" # ISO8601 + default_format: "Y-m-dTH:i:s" # ISO8601 default_timezone: "UTC" # defaults to whatever timezone set in php.ini or via date_default_timezone_set - array_collection: true - form_error: true - constraint_violation: true property_naming: separator: _ diff --git a/Tests/Formatter/MarkdownFormatterTest.php b/Tests/Formatter/MarkdownFormatterTest.php index f90f111..ff23b5f 100644 --- a/Tests/Formatter/MarkdownFormatterTest.php +++ b/Tests/Formatter/MarkdownFormatterTest.php @@ -20,7 +20,9 @@ class MarkdownFormatterTest 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(); $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..bd96b48 --- /dev/null +++ b/Tests/Parser/JmsMetadataParserTest.php @@ -0,0 +1,74 @@ +getMock('Metadata\MetadataFactoryInterface'); + $docCommentExtractor = $this->getMockBuilder('Nelmio\ApiDocBundle\Util\DocCommentExtractor') + ->disableOriginalConstructor() + ->getMock(); + + $propertyMetadataFoo = new PropertyMetadata('Nelmio\ApiDocBundle\Tests\Fixtures\Model\JmsNested', 'foo'); + $propertyMetadataFoo->type = '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 From 5acf1adced79887439ec353bd410bd984b7be411 Mon Sep 17 00:00:00 2001 From: Stefano Sala Date: Fri, 15 Feb 2013 09:24:09 +0100 Subject: [PATCH 3/4] Just fixed some docblock --- Parser/JmsMetadataParser.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Parser/JmsMetadataParser.php b/Parser/JmsMetadataParser.php index 090d224..ed5cb43 100644 --- a/Parser/JmsMetadataParser.php +++ b/Parser/JmsMetadataParser.php @@ -106,7 +106,7 @@ class JmsMetadataParser implements ParserInterface * Figure out a normalized data type (for documentation), and get a * nested class name, if available. * - * @param array|string $type + * @param PropertyMetadata $type * @return array */ protected function processDataType(PropertyMetadata $item) @@ -156,13 +156,13 @@ class JmsMetadataParser implements ParserInterface * Check the various ways JMS describes values in arrays, and * get the value type in the array * - * @param array|string $item + * @param PropertyMetadata $item * @return string|null */ - protected function getNestedTypeInArray($item) + protected function getNestedTypeInArray(PropertyMetadata $item) { if (is_array($item->type) - && in_array($item->type['name'], array('array')) + && 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'])) { From 5213b7db7194d7992dd07acaf3beb03f1358c185 Mon Sep 17 00:00:00 2001 From: Stefano Sala Date: Fri, 15 Feb 2013 10:48:29 +0100 Subject: [PATCH 4/4] Fixed version of rest-bundle Removed check of type string --- Parser/JmsMetadataParser.php | 4 ++-- Tests/Parser/JmsMetadataParserTest.php | 4 +++- composer.json | 2 +- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/Parser/JmsMetadataParser.php b/Parser/JmsMetadataParser.php index ed5cb43..269d92f 100644 --- a/Parser/JmsMetadataParser.php +++ b/Parser/JmsMetadataParser.php @@ -111,8 +111,6 @@ class JmsMetadataParser implements ParserInterface */ protected function processDataType(PropertyMetadata $item) { - $type = is_string($item->type) ? $item->type : $item->type['name']; - //check for a type inside something that could be treated as an array if ($nestedType = $this->getNestedTypeInArray($item)) { if ($this->isPrimitive($nestedType)) { @@ -130,6 +128,8 @@ class JmsMetadataParser implements ParserInterface ); } + $type = $item->type['name']; + //could be basic type if ($this->isPrimitive($type)) { return array( diff --git a/Tests/Parser/JmsMetadataParserTest.php b/Tests/Parser/JmsMetadataParserTest.php index bd96b48..88050a9 100644 --- a/Tests/Parser/JmsMetadataParserTest.php +++ b/Tests/Parser/JmsMetadataParserTest.php @@ -16,7 +16,9 @@ class JmsMetadataParserTest extends \PHPUnit_Framework_TestCase ->getMock(); $propertyMetadataFoo = new PropertyMetadata('Nelmio\ApiDocBundle\Tests\Fixtures\Model\JmsNested', 'foo'); - $propertyMetadataFoo->type = 'DateTime'; + $propertyMetadataFoo->type = array( + 'name' => 'DateTime' + ); $propertyMetadataBar = new PropertyMetadata('Nelmio\ApiDocBundle\Tests\Fixtures\Model\JmsNested', 'bar'); $propertyMetadataBar->type = array( diff --git a/composer.json b/composer.json index 9779b6a..a3afbce 100644 --- a/composer.json +++ b/composer.json @@ -25,7 +25,7 @@ "symfony/browser-kit": ">=2.1,<2.3-dev", "symfony/validator": ">=2.1,<2.3-dev", "symfony/yaml": ">=2.1,<2.3-dev", - "friendsofsymfony/rest-bundle": "dev-jms_serlializer_1_0", + "friendsofsymfony/rest-bundle": "dev-master", "jms/serializer-bundle": "1.0.*" }, "minimum-stability": "dev",