Skip to content

Commit 77d9964

Browse files
committed
deprecation path
1 parent da8fa9a commit 77d9964

7 files changed

Lines changed: 393 additions & 200 deletions

File tree

src/JsonApi/Serializer/ItemNormalizer.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ final class ItemNormalizer extends AbstractItemNormalizer
5454
use CacheKeyTrait;
5555
use ClassInfoTrait;
5656
use ContextTrait;
57-
use ItemNormalizerTrait;
57+
use ItemNormalizerTrait {
58+
denormalize as private doDenormalize;
59+
}
5860

5961
public const FORMAT = 'jsonapi';
6062

@@ -84,6 +86,13 @@ public function supportsNormalization(mixed $data, ?string $format = null, array
8486
return self::FORMAT === $format && parent::supportsNormalization($data, $format, $context) && !($data instanceof \Exception || $data instanceof FlattenException);
8587
}
8688

89+
public function denormalize(mixed $data, string $type, ?string $format = null, array $context = []): mixed
90+
{
91+
trigger_deprecation('api-platform/core', '4.4', 'Calling "denormalize()" on "%s" is deprecated, use "%s" instead.', self::class, ItemDenormalizer::class);
92+
93+
return $this->doDenormalize($data, $type, $format, $context);
94+
}
95+
8796
public function normalize(mixed $data, ?string $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null
8897
{
8998
$resourceClass = $this->getObjectClass($data);
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <dunglas@gmail.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\JsonApi\Tests\Serializer;
15+
16+
use ApiPlatform\JsonApi\Serializer\ItemDenormalizer;
17+
use ApiPlatform\JsonApi\Serializer\ItemNormalizer;
18+
use ApiPlatform\JsonApi\Tests\Fixtures\Dummy;
19+
use ApiPlatform\Metadata\IriConverterInterface;
20+
use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
21+
use ApiPlatform\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
22+
use ApiPlatform\Metadata\ResourceClassResolverInterface;
23+
use PHPUnit\Framework\Attributes\Group;
24+
use PHPUnit\Framework\TestCase;
25+
use Prophecy\PhpUnit\ProphecyTrait;
26+
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
27+
28+
class ItemDenormalizerTest extends TestCase
29+
{
30+
use ProphecyTrait;
31+
32+
public function testSupportsDenormalizationOnlyForJsonApiFormat(): void
33+
{
34+
$dummy = new Dummy();
35+
36+
$propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
37+
$propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
38+
$iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
39+
40+
$resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
41+
$resourceClassResolverProphecy->isResourceClass(Dummy::class)->willReturn(true);
42+
43+
$denormalizer = new ItemDenormalizer(
44+
$propertyNameCollectionFactoryProphecy->reveal(),
45+
$propertyMetadataFactoryProphecy->reveal(),
46+
$iriConverterProphecy->reveal(),
47+
$resourceClassResolverProphecy->reveal()
48+
);
49+
50+
$this->assertFalse($denormalizer->supportsNormalization($dummy, ItemNormalizer::FORMAT));
51+
$this->assertTrue($denormalizer->supportsDenormalization($dummy, Dummy::class, ItemNormalizer::FORMAT));
52+
$this->assertFalse($denormalizer->supportsDenormalization($dummy, Dummy::class, 'jsonld'));
53+
}
54+
55+
#[Group('legacy')]
56+
public function testDenormalizeOnLegacyItemNormalizerIsDeprecated(): void
57+
{
58+
$this->expectUserDeprecationMessage('Since api-platform/core 4.4: Calling "denormalize()" on "ApiPlatform\JsonApi\Serializer\ItemNormalizer" is deprecated, use "ApiPlatform\JsonApi\Serializer\ItemDenormalizer" instead.');
59+
$this->expectException(NotNormalizableValueException::class);
60+
61+
$propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
62+
$propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
63+
$iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
64+
$resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
65+
66+
$normalizer = new ItemNormalizer(
67+
$propertyNameCollectionFactoryProphecy->reveal(),
68+
$propertyMetadataFactoryProphecy->reveal(),
69+
$iriConverterProphecy->reveal(),
70+
$resourceClassResolverProphecy->reveal()
71+
);
72+
73+
$normalizer->denormalize(
74+
['data' => ['id' => '/dummies/1']],
75+
Dummy::class,
76+
ItemNormalizer::FORMAT,
77+
['api_allow_update' => false]
78+
);
79+
}
80+
}

src/JsonApi/Tests/Serializer/ItemNormalizerTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
use ApiPlatform\Metadata\ResourceClassResolverInterface;
3535
use ApiPlatform\Metadata\UrlGeneratorInterface;
3636
use Doctrine\Common\Collections\ArrayCollection;
37+
use PHPUnit\Framework\Attributes\IgnoreDeprecations;
3738
use PHPUnit\Framework\TestCase;
3839
use Prophecy\Argument;
3940
use Prophecy\PhpUnit\ProphecyTrait;
@@ -51,6 +52,7 @@
5152
/**
5253
* @author Amrouche Hamza <hamza.simperfit@gmail.com>
5354
*/
55+
#[IgnoreDeprecations]
5456
class ItemNormalizerTest extends TestCase
5557
{
5658
use ProphecyTrait;

src/JsonLd/Serializer/ItemNormalizer.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ final class ItemNormalizer extends AbstractItemNormalizer
4343
{
4444
use ClassInfoTrait;
4545
use ContextTrait;
46-
use ItemNormalizerTrait;
46+
use ItemNormalizerTrait {
47+
denormalize as private doDenormalize;
48+
}
4749
use JsonLdContextTrait;
4850

4951
public const FORMAT = 'jsonld';
@@ -171,4 +173,11 @@ public function supportsDenormalization(mixed $data, string $type, ?string $form
171173
{
172174
return self::FORMAT === $format && parent::supportsDenormalization($data, $type, $format, $context);
173175
}
176+
177+
public function denormalize(mixed $data, string $type, ?string $format = null, array $context = []): mixed
178+
{
179+
trigger_deprecation('api-platform/core', '4.4', 'Calling "denormalize()" on "%s" is deprecated, use "%s" instead.', self::class, ItemDenormalizer::class);
180+
181+
return $this->doDenormalize($data, $type, $format, $context);
182+
}
174183
}

src/Serializer/ItemNormalizer.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@
3434
*/
3535
class ItemNormalizer extends AbstractItemNormalizer
3636
{
37-
use ItemNormalizerTrait;
37+
use ItemNormalizerTrait {
38+
denormalize as private doDenormalize;
39+
}
3840

3941
private readonly LoggerInterface $logger;
4042

@@ -44,4 +46,11 @@ public function __construct(PropertyNameCollectionFactoryInterface $propertyName
4446

4547
$this->logger = $logger ?: new NullLogger();
4648
}
49+
50+
public function denormalize(mixed $data, string $type, ?string $format = null, array $context = []): mixed
51+
{
52+
trigger_deprecation('api-platform/core', '4.4', 'Calling "denormalize()" on "%s" is deprecated, use "%s" instead.', self::class, ItemDenormalizer::class);
53+
54+
return $this->doDenormalize($data, $type, $format, $context);
55+
}
4756
}

0 commit comments

Comments
 (0)