Skip to content
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

[Bug] Fix integer Identifiers denormalization #1899

Merged
merged 1 commit into from
Apr 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions features/bootstrap/FeatureContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -733,6 +733,7 @@ public function thereIsARelatedDummyWithFriends(int $nb)
$relatedDummy2->setName('RelatedDummy without friends');
$this->manager->persist($relatedDummy2);
$this->manager->flush();
$this->manager->clear();
}

/**
Expand Down
1 change: 1 addition & 0 deletions features/doctrine/search_filter.feature
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Feature: Search filter on collections
And I send a "GET" request to "/related_dummies?relatedToDummyFriend.dummyFriend=/dummy_friends/4"
Then the response status code should be 200
And the JSON node "_embedded.item" should have 1 element
And the JSON node "_embedded.item[0].id" should be equal to the number 1
And the JSON node "_embedded.item[0]._links.relatedToDummyFriend" should have 4 elements
And the JSON node "_embedded.item[0]._embedded.relatedToDummyFriend" should have 4 elements

Expand Down
2 changes: 1 addition & 1 deletion features/main/non_resource.feature
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Feature: Non-resources handling
"@context": "/contexts/ContainNonResource",
"@id": "/contain_non_resources/1",
"@type": "ContainNonResource",
"id": "1",
"id": 1,
"nested": {
"@id": "/contain_non_resources/1-nested",
"@type": "ContainNonResource",
Expand Down
4 changes: 4 additions & 0 deletions src/Bridge/Symfony/Bundle/Resources/config/api.xml
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,10 @@
<argument type="tagged" tag="api_platform.identifier.denormalizer" />
</service>

<service id="api_platform.identifier.integer" class="ApiPlatform\Core\Identifier\Normalizer\IntegerDenormalizer" public="false">
<tag name="api_platform.identifier.denormalizer" />
</service>

<service id="api_platform.identifier.date_normalizer" class="ApiPlatform\Core\Identifier\Normalizer\DateTimeIdentifierDenormalizer" public="false">
<tag name="api_platform.identifier.denormalizer" />
</service>
Expand Down
9 changes: 5 additions & 4 deletions src/Identifier/Normalizer/ChainIdentifierDenormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,8 @@ public function denormalize($data, $class, $format = null, array $context = [])
throw new InvalidIdentifierException(sprintf('Invalid identifier "%1$s", "%1$s" was not found.', $key));
}

$metadata = $this->getIdentifierMetadata($class, $key);
foreach ($this->identifierDenormalizers as $normalizer) {
$metadata = $this->getIdentifierMetadata($class, $key);

if (!$normalizer->supportsDenormalization($identifiers[$key], $metadata)) {
continue;
}
Expand All @@ -82,8 +81,10 @@ public function denormalize($data, $class, $format = null, array $context = [])

private function getIdentifierMetadata($class, $propertyName)
{
$type = $this->propertyMetadataFactory->create($class, $propertyName)->getType();
if (!$type = $this->propertyMetadataFactory->create($class, $propertyName)->getType()) {
return null;
}

return $type && Type::BUILTIN_TYPE_OBJECT === $type->getBuiltinType() ? $type->getClassName() : null;
return Type::BUILTIN_TYPE_OBJECT === ($builtInType = $type->getBuiltinType()) ? $type->getClassName() : $builtInType;
}
}
33 changes: 33 additions & 0 deletions src/Identifier/Normalizer/IntegerDenormalizer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Core\Identifier\Normalizer;

use Symfony\Component\PropertyInfo\Type;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;

final class IntegerDenormalizer implements DenormalizerInterface
{
public function denormalize($data, $class, $format = null, array $context = []): int
{
return (int) $data;
}

/**
* {@inheritdoc}
*/
public function supportsDenormalization($data, $type, $format = null): bool
{
return Type::BUILTIN_TYPE_INT === $type && \is_string($data);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,7 @@ private function getPartialContainerBuilderProphecy($test = false)
'api_platform.filters',
'api_platform.iri_converter',
'api_platform.identifier.denormalizer',
'api_platform.identifier.integer',
'api_platform.identifier.date_normalizer',
'api_platform.identifier.uuid_normalizer',
'api_platform.identifiers_extractor',
Expand Down
29 changes: 26 additions & 3 deletions tests/Identifier/Normalizer/ChainIdentifierDenormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use ApiPlatform\Core\Api\IdentifiersExtractorInterface;
use ApiPlatform\Core\Identifier\Normalizer\ChainIdentifierDenormalizer;
use ApiPlatform\Core\Identifier\Normalizer\DateTimeIdentifierDenormalizer;
use ApiPlatform\Core\Identifier\Normalizer\IntegerDenormalizer;
use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
use ApiPlatform\Core\Metadata\Property\PropertyMetadata;
use PHPUnit\Framework\TestCase;
Expand All @@ -31,22 +32,25 @@ public function testCompositeIdentifier()
$identifier = 'a=1;c=2;d=2015-04-05';
$class = 'Dummy';

$integerPropertyMetadata = (new PropertyMetadata())->withIdentifier(true)->withType(new Type(Type::BUILTIN_TYPE_INT));
$identifierPropertyMetadata = (new PropertyMetadata())->withIdentifier(true);
$dateIdentifierPropertyMetadata = (new PropertyMetadata())->withIdentifier(true)->withType(new Type(Type::BUILTIN_TYPE_OBJECT, false, \DateTime::class));

$propertyMetadataFactory = $this->prophesize(PropertyMetadataFactoryInterface::class);
$propertyMetadataFactory->create($class, 'a')->shouldBeCalled()->willReturn($identifierPropertyMetadata);
$propertyMetadataFactory->create($class, 'a')->shouldBeCalled()->willReturn($integerPropertyMetadata);
$propertyMetadataFactory->create($class, 'c')->shouldBeCalled()->willReturn($identifierPropertyMetadata);
$propertyMetadataFactory->create($class, 'd')->shouldBeCalled()->willReturn($dateIdentifierPropertyMetadata);

$identifiersExtractor = $this->prophesize(IdentifiersExtractorInterface::class);
$identifiersExtractor->getIdentifiersFromResourceClass($class)->willReturn(['a', 'c', 'd']);

$identifierDenormalizers = [new DateTimeIdentifierDenormalizer()];
$identifierDenormalizers = [new IntegerDenormalizer(), new DateTimeIdentifierDenormalizer()];

$identifierDenormalizer = new ChainIdentifierDenormalizer($identifiersExtractor->reveal(), $propertyMetadataFactory->reveal(), $identifierDenormalizers);

$this->assertEquals($identifierDenormalizer->denormalize($identifier, $class), ['a' => '1', 'c' => '2', 'd' => new \DateTime('2015-04-05')]);
$result = $identifierDenormalizer->denormalize($identifier, $class);
$this->assertEquals(['a' => 1, 'c' => '2', 'd' => new \DateTime('2015-04-05')], $result);
$this->assertSame(1, $result['a']);
}

public function testSingleDateIdentifier()
Expand All @@ -67,4 +71,23 @@ public function testSingleDateIdentifier()

$this->assertEquals($identifierDenormalizer->denormalize($identifier, $class), ['funkyid' => new \DateTime('2015-04-05')]);
}

public function testIntegerIdentifier()
{
$identifier = '42';
$class = 'Dummy';

$integerIdentifierPropertyMetadata = (new PropertyMetadata())->withIdentifier(true)->withType(new Type(Type::BUILTIN_TYPE_INT));

$propertyMetadataFactory = $this->prophesize(PropertyMetadataFactoryInterface::class);
$propertyMetadataFactory->create($class, 'id')->shouldBeCalled()->willReturn($integerIdentifierPropertyMetadata);

$identifiersExtractor = $this->prophesize(IdentifiersExtractorInterface::class);
$identifiersExtractor->getIdentifiersFromResourceClass($class)->willReturn(['id']);

$identifierDenormalizers = [new IntegerDenormalizer()];
$identifierDenormalizer = new ChainIdentifierDenormalizer($identifiersExtractor->reveal(), $propertyMetadataFactory->reveal(), $identifierDenormalizers);

$this->assertSame(['id' => 42], $identifierDenormalizer->denormalize($identifier, $class));
}
}