From 89aec4bfd3767f7c4032434c7b026615c3a799ab Mon Sep 17 00:00:00 2001 From: Benjamin Bender Date: Fri, 22 Mar 2013 14:05:14 +0100 Subject: [PATCH 1/4] Add a little bit more flexibility to ApiDocExtractor --- Extractor/ApiDocExtractor.php | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/Extractor/ApiDocExtractor.php b/Extractor/ApiDocExtractor.php index 8f7631a..046663a 100644 --- a/Extractor/ApiDocExtractor.php +++ b/Extractor/ApiDocExtractor.php @@ -68,11 +68,21 @@ class ApiDocExtractor * You can extend this method if you don't want all the routes * to be included. * - * @return array of Route + * @return \Traverseable Iterator for a RouteCollection */ public function getRoutes() { - return $this->router->getRouteCollection()->all(); + return $this->router->getRouteCollection()->getIterator(); + } + + /** + * Extracts annotations from all known routes + * + * @return array + */ + public function all() + { + return $this->extractAnnotations($this->getRoutes()); } /** @@ -80,14 +90,16 @@ class ApiDocExtractor * - annotation * - resource * + * @param \Traversable $routes The routes for which the annotations should be extracted + * * @return array */ - public function all() + public function extractAnnotations(\Traversable $routes) { $array = array(); $resources = array(); - foreach ($this->getRoutes() as $route) { + foreach ($routes as $route) { if ($method = $this->getReflectionMethod($route->getDefault('_controller'))) { if ($annotation = $this->reader->getMethodAnnotation($method, self::ANNOTATION_CLASS)) { if ($annotation->isResource()) { From d099ffa40f3a8ebb327c854ba871c981ff181246 Mon Sep 17 00:00:00 2001 From: Jordan Stout Date: Thu, 14 Feb 2013 12:20:13 -0800 Subject: [PATCH 2/4] use serializer naming strategy for parameter names --- Parser/JmsMetadataParser.php | 16 +++++++++++++--- Resources/config/services.jms.xml | 1 + 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/Parser/JmsMetadataParser.php b/Parser/JmsMetadataParser.php index 3b154a5..b79aea9 100644 --- a/Parser/JmsMetadataParser.php +++ b/Parser/JmsMetadataParser.php @@ -15,6 +15,7 @@ use Metadata\MetadataFactoryInterface; use Nelmio\ApiDocBundle\Util\DocCommentExtractor; use JMS\Serializer\Metadata\PropertyMetadata; use JMS\Serializer\Metadata\VirtualPropertyMetadata; +use JMS\Serializer\Naming\PropertyNamingStrategyInterface; /** * Uses the JMS metadata factory to extract input/output model information @@ -27,6 +28,11 @@ class JmsMetadataParser implements ParserInterface */ private $factory; + /** + * @var PropertyNamingStrategyInterface + */ + private $namingStrategy; + /** * @var \Nelmio\ApiDocBundle\Util\DocCommentExtractor */ @@ -35,9 +41,13 @@ class JmsMetadataParser implements ParserInterface /** * Constructor, requires JMS Metadata factory */ - public function __construct(MetadataFactoryInterface $factory, DocCommentExtractor $commentExtractor) - { + public function __construct( + MetadataFactoryInterface $factory, + PropertyNamingStrategyInterface $namingStrategy, + DocCommentExtractor $commentExtractor + ) { $this->factory = $factory; + $this->namingStrategy = $namingStrategy; $this->commentExtractor = $commentExtractor; } @@ -85,7 +95,7 @@ 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; + $name = $this->namingStrategy->translateName($item); $dataType = $this->processDataType($item); diff --git a/Resources/config/services.jms.xml b/Resources/config/services.jms.xml index 36eefc5..b174795 100644 --- a/Resources/config/services.jms.xml +++ b/Resources/config/services.jms.xml @@ -10,6 +10,7 @@ From c5e12e3c4f368830f7a1894e89f152f01a84dbbe Mon Sep 17 00:00:00 2001 From: Benjamin Bender Date: Tue, 26 Mar 2013 08:57:49 +0100 Subject: [PATCH 3/4] Fix typo --- Extractor/ApiDocExtractor.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Extractor/ApiDocExtractor.php b/Extractor/ApiDocExtractor.php index 046663a..47c08da 100644 --- a/Extractor/ApiDocExtractor.php +++ b/Extractor/ApiDocExtractor.php @@ -68,7 +68,7 @@ class ApiDocExtractor * You can extend this method if you don't want all the routes * to be included. * - * @return \Traverseable Iterator for a RouteCollection + * @return \Traversable Iterator for a RouteCollection */ public function getRoutes() { From afd07dc5704c316217b71106e0c80f69bb301e8a Mon Sep 17 00:00:00 2001 From: Jordan Stout Date: Tue, 26 Mar 2013 10:37:33 -0700 Subject: [PATCH 4/4] fixed failing tests --- Tests/Formatter/MarkdownFormatterTest.php | 4 ++-- Tests/Formatter/SimpleFormatterTest.php | 4 ++-- Tests/Parser/JmsMetadataParserTest.php | 19 ++++++++++++++++++- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/Tests/Formatter/MarkdownFormatterTest.php b/Tests/Formatter/MarkdownFormatterTest.php index 7f9de03..9572048 100644 --- a/Tests/Formatter/MarkdownFormatterTest.php +++ b/Tests/Formatter/MarkdownFormatterTest.php @@ -250,13 +250,13 @@ nested[parent][nested]: * required: false * description: No description. -nested[parent][nestedArray][]: +nested[parent][nested_array][]: * type: array of objects (JmsNested) * required: false * description: No description. -nestedArray[]: +nested_array[]: * type: array of objects (JmsNested) * required: false diff --git a/Tests/Formatter/SimpleFormatterTest.php b/Tests/Formatter/SimpleFormatterTest.php index 4de4e89..105e822 100644 --- a/Tests/Formatter/SimpleFormatterTest.php +++ b/Tests/Formatter/SimpleFormatterTest.php @@ -345,7 +345,7 @@ With multiple lines.', 'description' => 'No description.', 'readonly' => false, ), - 'nestedArray' => + 'nested_array' => array ( 'dataType' => 'array of objects (JmsNested)', 'required' => false, @@ -356,7 +356,7 @@ With multiple lines.', ), ), ), - 'nestedArray' => + 'nested_array' => array ( 'dataType' => 'array of objects (JmsNested)', 'required' => false, diff --git a/Tests/Parser/JmsMetadataParserTest.php b/Tests/Parser/JmsMetadataParserTest.php index 0f7d5b2..95d9673 100644 --- a/Tests/Parser/JmsMetadataParserTest.php +++ b/Tests/Parser/JmsMetadataParserTest.php @@ -44,6 +44,23 @@ class JmsMetadataParserTest extends \PHPUnit_Framework_TestCase $metadata->addPropertyMetadata($propertyMetadataBar); $metadata->addPropertyMetadata($propertyMetadataBaz); + $propertyNamingStrategy = $this->getMock('JMS\Serializer\Naming\PropertyNamingStrategyInterface'); + + $propertyNamingStrategy + ->expects($this->at(0)) + ->method('translateName') + ->will($this->returnValue('foo')); + + $propertyNamingStrategy + ->expects($this->at(1)) + ->method('translateName') + ->will($this->returnValue('bar')); + + $propertyNamingStrategy + ->expects($this->at(2)) + ->method('translateName') + ->will($this->returnValue('baz')); + $input = new JmsNested(); $metadataFactory->expects($this->once()) @@ -51,7 +68,7 @@ class JmsMetadataParserTest extends \PHPUnit_Framework_TestCase ->with($input) ->will($this->returnValue($metadata)); - $jmsMetadataParser = new JmsMetadataParser($metadataFactory, $docCommentExtractor); + $jmsMetadataParser = new JmsMetadataParser($metadataFactory, $propertyNamingStrategy, $docCommentExtractor); $output = $jmsMetadataParser->parse($input);