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)
This commit is contained in:
Stefano Sala 2013-02-11 14:42:17 +01:00
parent befeeb3e3f
commit 07bb37ac76
7 changed files with 104 additions and 24 deletions

@ -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 <V>, or <K,V>
$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;

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

@ -16,7 +16,7 @@ use Symfony\Component\Validator\Constraints as Assert;
class Test
{
/**
* @Assert\MinLength("foo");
* @Assert\Length(min="foo");
* @Assert\NotBlank
*/
public $a;

@ -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: _

@ -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 = <<<MARKDOWN

@ -20,7 +20,9 @@ class SimpleFormatterTest 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.simple_formatter')->format($data);
$expected = array(

@ -0,0 +1,74 @@
<?php
namespace NelmioApiDocBundle\Tests\Parser;
use Nelmio\ApiDocBundle\Tests\Fixtures\Model\JmsNested;
use Nelmio\ApiDocBundle\Parser\JmsMetadataParser;
use JMS\Serializer\Metadata\ClassMetadata;
use JMS\Serializer\Metadata\PropertyMetadata;
class JmsMetadataParserTest extends \PHPUnit_Framework_TestCase
{
public function testParserWithNestedType()
{
$metadataFactory = $this->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);
}
}