Skip to content

Commit 837dc48

Browse files
authored
Fix #3979 identifier chain not breaking after denormalizing (#3985)
* Lower the priority of our identifier denormalizers * Break on supported denormalizer * add test
1 parent 21f4cf0 commit 837dc48

File tree

4 files changed

+27
-3
lines changed

4 files changed

+27
-3
lines changed

src/Bridge/Symfony/Bundle/Resources/config/api.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,11 +275,11 @@
275275
</service>
276276

277277
<service id="api_platform.identifier.integer" class="ApiPlatform\Core\Identifier\Normalizer\IntegerDenormalizer" public="false">
278-
<tag name="api_platform.identifier.denormalizer" />
278+
<tag name="api_platform.identifier.denormalizer" priority="-100" />
279279
</service>
280280

281281
<service id="api_platform.identifier.date_normalizer" class="ApiPlatform\Core\Identifier\Normalizer\DateTimeIdentifierDenormalizer" public="false">
282-
<tag name="api_platform.identifier.denormalizer" />
282+
<tag name="api_platform.identifier.denormalizer" priority="-100" />
283283
</service>
284284

285285
<!-- Subresources -->

src/Bridge/Symfony/Bundle/Resources/config/ramsey_uuid.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<services>
88
<service id="api_platform.identifier.uuid_normalizer" class="ApiPlatform\Core\Bridge\RamseyUuid\Identifier\Normalizer\UuidNormalizer" public="false">
9-
<tag name="api_platform.identifier.denormalizer" />
9+
<tag name="api_platform.identifier.denormalizer" priority="-100" />
1010
</service>
1111

1212
<service id="api_platform.serializer.uuid_denormalizer" class="ApiPlatform\Core\Bridge\RamseyUuid\Serializer\UuidDenormalizer" public="false">

src/Identifier/IdentifierConverter.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ public function convert($data, string $class, array $context = []): array
6969

7070
try {
7171
$identifiers[$identifier] = $identifierTransformer->denormalize($value, $type);
72+
break;
7273
} catch (InvalidIdentifierException $e) {
7374
throw new InvalidIdentifierException(sprintf('Identifier "%s" could not be denormalized.', $identifier), $e->getCode(), $e);
7475
}

tests/Identifier/IdentifierConverterTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use ApiPlatform\Core\Tests\ProphecyTrait;
2323
use PHPUnit\Framework\TestCase;
2424
use Symfony\Component\PropertyInfo\Type;
25+
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
2526

2627
/**
2728
* @author Antoine Bluchet <[email protected]>
@@ -98,4 +99,26 @@ public function testIntegerIdentifier()
9899

99100
$this->assertSame(['id' => 42], $identifierDenormalizer->convert($identifier, $class));
100101
}
102+
103+
public function testShouldBreakAfterTransforming()
104+
{
105+
$identifier = ['id' => '42'];
106+
$class = 'Dummy';
107+
108+
$integerIdentifierPropertyMetadata = (new PropertyMetadata())->withIdentifier(true)->withType(new Type(Type::BUILTIN_TYPE_INT));
109+
110+
$propertyMetadataFactory = $this->prophesize(PropertyMetadataFactoryInterface::class);
111+
$propertyMetadataFactory->create($class, 'id')->shouldBeCalled()->willReturn($integerIdentifierPropertyMetadata);
112+
113+
$identifiersExtractor = $this->prophesize(IdentifiersExtractorInterface::class);
114+
$identifiersExtractor->getIdentifiersFromResourceClass($class)->willReturn(['id']);
115+
116+
$shouldNotBeCalled = $this->prophesize(DenormalizerInterface::class);
117+
$shouldNotBeCalled->supportsDenormalization()->shouldNotBeCalled();
118+
119+
$identifierDenormalizers = [new IntegerDenormalizer(), $shouldNotBeCalled->reveal()];
120+
$identifierDenormalizer = new IdentifierConverter($identifiersExtractor->reveal(), $propertyMetadataFactory->reveal(), $identifierDenormalizers);
121+
122+
$this->assertSame(['id' => 42], $identifierDenormalizer->convert($identifier, $class));
123+
}
101124
}

0 commit comments

Comments
 (0)