-
-
Notifications
You must be signed in to change notification settings - Fork 934
Add a generic ItemNormalizer (relation support for raw JSON, XML, CSV, YAML) #624
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (c) Kévin Dunglas <dunglas@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace ApiPlatform\Core\Serializer; | ||
|
||
/** | ||
* Generic item normalizer. | ||
* | ||
* @author Kévin Dunglas <dunglas@gmail.com> | ||
*/ | ||
class ItemNormalizer extends AbstractItemNormalizer | ||
{ | ||
public function normalize($object, $format = null, array $context = []) | ||
{ | ||
$rawData = parent::normalize($object, $format, $context); | ||
if (!is_array($rawData)) { | ||
return $rawData; | ||
} | ||
|
||
if (!isset($data['id'])) { | ||
$data['id'] = $this->iriConverter->getIriFromItem($object); | ||
} | ||
|
||
return array_merge($data, $rawData); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no better way than an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I want the ID as first key. Do you know a better way to that? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. how does the array_merge guarantees that? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Where does |
||
} | ||
|
||
public function denormalize($data, $class, $format = null, array $context = []) | ||
{ | ||
// Avoid issues with proxies if we populated the object | ||
if (isset($data['id']) && !isset($context['object_to_populate'])) { | ||
$context['object_to_populate'] = $this->iriConverter->getItemFromIri($data['id'], true); | ||
} | ||
|
||
return parent::denormalize($data, $class, $format, $context); // TODO: Change the autogenerated stub | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TODO to be removed There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (c) Kévin Dunglas <dunglas@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace ApiPlatform\Core\Tests\Serializer; | ||
|
||
use ApiPlatform\Core\Api\IriConverterInterface; | ||
use ApiPlatform\Core\Api\ResourceClassResolverInterface; | ||
use ApiPlatform\Core\Exception\InvalidArgumentException; | ||
use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface; | ||
use ApiPlatform\Core\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface; | ||
use ApiPlatform\Core\Metadata\Property\PropertyMetadata; | ||
use ApiPlatform\Core\Metadata\Property\PropertyNameCollection; | ||
use ApiPlatform\Core\Serializer\ItemNormalizer; | ||
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Dummy; | ||
use Prophecy\Argument; | ||
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; | ||
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; | ||
use Symfony\Component\Serializer\SerializerInterface; | ||
|
||
/** | ||
* @author Kévin Dunglas <dunglas@gmail.com> | ||
*/ | ||
class ItemNormalizerTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function testSupportNormalization() | ||
{ | ||
$std = new \stdClass(); | ||
$dummy = new Dummy(); | ||
$dummy->setDescription('hello'); | ||
|
||
$propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class); | ||
$propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class); | ||
$iriConverterProphecy = $this->prophesize(IriConverterInterface::class); | ||
|
||
$resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class); | ||
$resourceClassResolverProphecy->getResourceClass($dummy)->willReturn(Dummy::class)->shouldBeCalled(); | ||
$resourceClassResolverProphecy->getResourceClass($std)->willThrow(new InvalidArgumentException())->shouldBeCalled(); | ||
$resourceClassResolverProphecy->isResourceClass(Dummy::class)->willReturn(true)->shouldBeCalled(); | ||
$resourceClassResolverProphecy->isResourceClass(\stdClass::class)->willReturn(false)->shouldBeCalled(); | ||
|
||
$normalizer = new ItemNormalizer( | ||
$propertyNameCollectionFactoryProphecy->reveal(), | ||
$propertyMetadataFactoryProphecy->reveal(), | ||
$iriConverterProphecy->reveal(), | ||
$resourceClassResolverProphecy->reveal() | ||
); | ||
|
||
$this->assertTrue($normalizer->supportsNormalization($dummy)); | ||
$this->assertTrue($normalizer->supportsNormalization($dummy)); | ||
$this->assertFalse($normalizer->supportsNormalization($std)); | ||
|
||
$this->assertTrue($normalizer->supportsDenormalization($dummy, Dummy::class)); | ||
$this->assertTrue($normalizer->supportsDenormalization($dummy, Dummy::class)); | ||
$this->assertFalse($normalizer->supportsDenormalization($std, \stdClass::class)); | ||
} | ||
|
||
public function testNormalize() | ||
{ | ||
$dummy = new Dummy(); | ||
$dummy->setName('hello'); | ||
|
||
$propertyNameCollection = new PropertyNameCollection(['name']); | ||
$propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class); | ||
$propertyNameCollectionFactoryProphecy->create(Dummy::class, [])->willReturn($propertyNameCollection)->shouldBeCalled(); | ||
|
||
$propertyMetadataFactory = new PropertyMetadata(null, null, true); | ||
$propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class); | ||
$propertyMetadataFactoryProphecy->create(Dummy::class, 'name', [])->willReturn($propertyMetadataFactory)->shouldBeCalled(); | ||
|
||
$iriConverterProphecy = $this->prophesize(IriConverterInterface::class); | ||
$iriConverterProphecy->getIriFromItem($dummy)->willReturn('/dummies/12')->shouldBeCalled(); | ||
|
||
$resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class); | ||
$resourceClassResolverProphecy->getResourceClass($dummy, null, true)->willReturn(Dummy::class)->shouldBeCalled(); | ||
|
||
$serializerProphecy = $this->prophesize(SerializerInterface::class); | ||
$serializerProphecy->willImplement(NormalizerInterface::class); | ||
$serializerProphecy->normalize('hello', null, Argument::type('array'))->willReturn('hello')->shouldBeCalled(); | ||
|
||
$normalizer = new ItemNormalizer( | ||
$propertyNameCollectionFactoryProphecy->reveal(), | ||
$propertyMetadataFactoryProphecy->reveal(), | ||
$iriConverterProphecy->reveal(), | ||
$resourceClassResolverProphecy->reveal() | ||
); | ||
$normalizer->setSerializer($serializerProphecy->reveal()); | ||
|
||
$this->assertEquals(['id' => '/dummies/12', 'name' => 'hello'], $normalizer->normalize($dummy)); | ||
} | ||
|
||
public function testDenormalize() | ||
{ | ||
$context = ['resource_class' => Dummy::class]; | ||
|
||
$propertyNameCollection = new PropertyNameCollection(['name']); | ||
$propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class); | ||
$propertyNameCollectionFactoryProphecy->create(Dummy::class, [])->willReturn($propertyNameCollection)->shouldBeCalled(); | ||
|
||
$propertyMetadataFactory = new PropertyMetadata(null, null, true); | ||
$propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class); | ||
$propertyMetadataFactoryProphecy->create(Dummy::class, 'name', [])->willReturn($propertyMetadataFactory)->shouldBeCalled(); | ||
|
||
$iriConverterProphecy = $this->prophesize(IriConverterInterface::class); | ||
|
||
$resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class); | ||
|
||
$serializerProphecy = $this->prophesize(SerializerInterface::class); | ||
$serializerProphecy->willImplement(DenormalizerInterface::class); | ||
|
||
$normalizer = new ItemNormalizer( | ||
$propertyNameCollectionFactoryProphecy->reveal(), | ||
$propertyMetadataFactoryProphecy->reveal(), | ||
$iriConverterProphecy->reveal(), | ||
$resourceClassResolverProphecy->reveal() | ||
); | ||
$normalizer->setSerializer($serializerProphecy->reveal()); | ||
|
||
$this->assertInstanceOf(Dummy::class, $normalizer->denormalize(['id' => '/dummies/12', 'name' => 'hello'], Dummy::class, null, $context)); | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is ID an IRI in all cases (i.e. all formats)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO it's better to always use an IRI as identifier but I'm open to discussion :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's alright, just wanted to make sure
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've seen a discussion about this #612
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Simperfit I don't think #612 is related.