Merge pull request #432 from Prezent/jms-inline

Parse JSM\Inline, fixes #372
This commit is contained in:
William Durand 2014-07-09 10:29:17 +02:00
commit b9b453c857
3 changed files with 130 additions and 16 deletions

View File

@ -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,
);
}

View File

@ -0,0 +1,18 @@
<?php
namespace Nelmio\ApiDocBundle\Tests\Fixtures\Model;
use JMS\Serializer\Annotation as JMS;
class JmsInline
{
/**
* @JMS\Type("string");
*/
public $foo;
/**
* @JMS\Inline
*/
public $inline;
}

View File

@ -402,6 +402,89 @@ class JmsMetadataParserTest extends \PHPUnit_Framework_TestCase
);
}
public function testParserWithInline()
{
$metadataFactory = $this->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(