diff --git a/Parser/JmsMetadataParser.php b/Parser/JmsMetadataParser.php index aa9a4f3..1f48f52 100644 --- a/Parser/JmsMetadataParser.php +++ b/Parser/JmsMetadataParser.php @@ -131,21 +131,23 @@ class JmsMetadataParser implements ParserInterface, PostParserInterface } } - $params[$name] = array( - 'dataType' => $dataType['normalized'], - 'actualType' => $dataType['actualType'], - 'subType' => $dataType['class'], - 'required' => false, - 'default' => isset($defaultProperties[$item->name]) ? $defaultProperties[$item->name] : null, - //TODO: can't think of a good way to specify this one, JMS doesn't have a setting for this - 'description' => $this->getDescription($item), - 'readonly' => $item->readOnly, - 'sinceVersion' => $item->sinceVersion, - 'untilVersion' => $item->untilVersion, - ); + if (!$dataType['inline']) { + $params[$name] = array( + 'dataType' => $dataType['normalized'], + 'actualType' => $dataType['actualType'], + 'subType' => $dataType['class'], + 'required' => false, + 'default' => isset($defaultProperties[$item->name]) ? $defaultProperties[$item->name] : null, + //TODO: can't think of a good way to specify this one, JMS doesn't have a setting for this + 'description' => $this->getDescription($item), + 'readonly' => $item->readOnly, + 'sinceVersion' => $item->sinceVersion, + 'untilVersion' => $item->untilVersion, + ); - if (!is_null($dataType['class']) && false === $dataType['primitive']) { - $params[$name]['class'] = $dataType['class']; + if (!is_null($dataType['class']) && false === $dataType['primitive']) { + $params[$name]['class'] = $dataType['class']; + } } // if class already parsed, continue, to avoid infinite recursion @@ -155,8 +157,14 @@ class JmsMetadataParser implements ParserInterface, PostParserInterface // check for nested classes with JMS metadata if ($dataType['class'] && false === $dataType['primitive'] && null !== $this->factory->getMetadataForClass($dataType['class'])) { - $visited[] = $dataType['class']; - $params[$name]['children'] = $this->doParse($dataType['class'], $visited, $groups); + $visited[] = $dataType['class']; + $children = $this->doParse($dataType['class'], $visited, $groups); + + if ($dataType['inline']) { + $params = array_merge($params, $children); + } else { + $params[$name]['children'] = $children; + } } } } @@ -181,6 +189,7 @@ class JmsMetadataParser implements ParserInterface, PostParserInterface 'actualType' => DataTypes::COLLECTION, 'class' => $this->typeMap[$nestedType], 'primitive' => true, + 'inline' => false, ); } @@ -191,6 +200,7 @@ class JmsMetadataParser implements ParserInterface, PostParserInterface 'actualType' => DataTypes::COLLECTION, 'class' => $nestedType, 'primitive' => false, + 'inline' => false, ); } @@ -203,6 +213,7 @@ class JmsMetadataParser implements ParserInterface, PostParserInterface 'actualType' => $this->typeMap[$type], 'class' => null, 'primitive' => true, + 'inline' => false, ); } @@ -213,6 +224,7 @@ class JmsMetadataParser implements ParserInterface, PostParserInterface 'class' => $type, 'actualType' => DataTypes::MODEL, 'primitive' => false, + 'inline' => false, ); } @@ -224,6 +236,7 @@ class JmsMetadataParser implements ParserInterface, PostParserInterface 'class' => $type, 'actualType' => DataTypes::MODEL, 'primitive' => false, + 'inline' => $item->inline, ); } diff --git a/Tests/Fixtures/Model/JmsInline.php b/Tests/Fixtures/Model/JmsInline.php new file mode 100644 index 0000000..254a004 --- /dev/null +++ b/Tests/Fixtures/Model/JmsInline.php @@ -0,0 +1,18 @@ +getMock('Metadata\MetadataFactoryInterface'); + $docCommentExtractor = $this->getMockBuilder('Nelmio\ApiDocBundle\Util\DocCommentExtractor') + ->disableOriginalConstructor() + ->getMock(); + + $propertyMetadataFoo = new PropertyMetadata('Nelmio\ApiDocBundle\Tests\Fixtures\Model\JmsInline', 'foo'); + $propertyMetadataFoo->type = array('name' => 'string'); + + $propertyMetadataInline = new PropertyMetadata('Nelmio\ApiDocBundle\Tests\Fixtures\Model\JmsInline', 'inline'); + $propertyMetadataInline->type = array('name' => 'Nelmio\ApiDocBundle\Tests\Fixtures\Model\JmsTest'); + $propertyMetadataInline->inline = true; + + $input = 'Nelmio\ApiDocBundle\Tests\Fixtures\Model\JmsInline'; + + $metadata = new ClassMetadata($input); + $metadata->addPropertyMetadata($propertyMetadataFoo); + $metadata->addPropertyMetadata($propertyMetadataInline); + + $propertyMetadataBar = new PropertyMetadata('Nelmio\ApiDocBundle\Tests\Fixtures\Model\JmsTest', 'bar'); + $propertyMetadataBar->type = array('name' => 'string'); + + $subInput = 'Nelmio\ApiDocBundle\Tests\Fixtures\Model\JmsTest'; + + $subMetadata = new ClassMetadata($subInput); + $subMetadata->addPropertyMetadata($propertyMetadataBar); + + $metadataFactory->expects($this->at(0)) + ->method('getMetadataForClass') + ->with($input) + ->will($this->returnValue($metadata)); + + $metadataFactory->expects($this->at(1)) + ->method('getMetadataForClass') + ->with($subInput) + ->will($this->returnValue($subMetadata)); + + $metadataFactory->expects($this->at(2)) + ->method('getMetadataForClass') + ->with($subInput) + ->will($this->returnValue($subMetadata)); + + $propertyNamingStrategy = new CamelCaseNamingStrategy(); + + $jmsMetadataParser = new JmsMetadataParser($metadataFactory, $propertyNamingStrategy, $docCommentExtractor); + + $output = $jmsMetadataParser->parse( + array( + 'class' => $input, + 'groups' => array(), + ) + ); + + $this->assertEquals( + array( + 'foo' => array( + 'dataType' => 'string', + 'actualType' => DataTypes::STRING, + 'subType' => null, + 'default' => null, + 'required' => false, + 'description' => null, + 'readonly' => false, + 'sinceVersion' => null, + 'untilVersion' => null, + ), + 'bar' => array( + 'dataType' => 'string', + 'actualType' => DataTypes::STRING, + 'subType' => null, + 'default' => null, + 'required' => false, + 'description' => null, + 'readonly' => false, + 'sinceVersion' => null, + 'untilVersion' => null, + ), + ), + $output + ); + } + public function dataTestParserWithNestedType() { return array(