mirror of
https://github.com/retailcrm/NelmioApiDocBundle.git
synced 2025-02-09 02:59:27 +03:00
Merge remote-tracking branch 'stewe/jms_serlializer_1_0_upgrade'
This commit is contained in:
commit
efb4bb29dd
@ -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
|
||||
@ -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,13 @@ class JmsMetadataParser implements ParserInterface
|
||||
* Figure out a normalized data type (for documentation), and get a
|
||||
* nested class name, if available.
|
||||
*
|
||||
* @param string $type
|
||||
* @param PropertyMetadata $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
|
||||
);
|
||||
}
|
||||
|
||||
//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 +128,16 @@ class JmsMetadataParser implements ParserInterface
|
||||
);
|
||||
}
|
||||
|
||||
$type = $item->type['name'];
|
||||
|
||||
//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 PropertyMetadata $item
|
||||
* @return string|null
|
||||
*/
|
||||
protected function getNestedTypeInArray($type)
|
||||
protected function getNestedTypeInArray(PropertyMetadata $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')) // We have to support ArrayCollection as well
|
||||
&& 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(14, $data);
|
||||
|
@ -1,7 +1,7 @@
|
||||
<?php
|
||||
namespace Nelmio\ApiDocBundle\Tests\Fixtures\Model;
|
||||
|
||||
use JMS\SerializerBundle\Annotation as JMS;
|
||||
use JMS\Serializer\Annotation as JMS;
|
||||
|
||||
class JmsNested
|
||||
{
|
||||
|
@ -2,7 +2,7 @@
|
||||
|
||||
namespace Nelmio\ApiDocBundle\Tests\Fixtures\Model;
|
||||
|
||||
use JMS\SerializerBundle\Annotation as JMS;
|
||||
use JMS\Serializer\Annotation as JMS;
|
||||
|
||||
class JmsTest
|
||||
{
|
||||
|
@ -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(
|
||||
|
76
Tests/Parser/JmsMetadataParserTest.php
Normal file
76
Tests/Parser/JmsMetadataParserTest.php
Normal file
@ -0,0 +1,76 @@
|
||||
<?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 = array(
|
||||
'name' => '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);
|
||||
}
|
||||
}
|
@ -26,7 +26,7 @@
|
||||
"symfony/validator": ">=2.1,<2.3-dev",
|
||||
"symfony/yaml": ">=2.1,<2.3-dev",
|
||||
"friendsofsymfony/rest-bundle": "dev-master",
|
||||
"jms/serializer-bundle": "0.9.*"
|
||||
"jms/serializer-bundle": "1.0.*"
|
||||
},
|
||||
"minimum-stability": "dev",
|
||||
"autoload": {
|
||||
|
Loading…
x
Reference in New Issue
Block a user