Skip to content

Commit 7893a72

Browse files
authored
Merge pull request #2665 from teohhanhui/fix/cacheable-noop-scalar-normalizer
Add a cacheable no-op scalar normalizer
2 parents 66ee1ba + 1010b95 commit 7893a72

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,11 @@
126126
<tag name="serializer.normalizer" priority="-925" />
127127
</service>
128128

129+
<service id="api_platform.serializer.normalizer.no_op_scalar" class="ApiPlatform\Core\Serializer\NoOpScalarNormalizer" public="false">
130+
<!-- Run before our non-cacheable normalizers -->
131+
<tag name="serializer.normalizer" priority="-900" />
132+
</service>
133+
129134
<!-- Resources Operations path resolver -->
130135

131136
<service id="api_platform.operation_path_resolver" alias="api_platform.operation_path_resolver.router" public="false" />
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
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\Core\Serializer;
15+
16+
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
17+
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
18+
19+
/**
20+
* A no-op normalizer that passes through scalar values.
21+
*
22+
* When there are non-cacheable normalizers in use, and you don't need to normalize
23+
* scalar values, register this normalizer with a higher priority than the non-cacheable
24+
* normalizers. This allows caching supportsNormalization calls for scalar values.
25+
*/
26+
final class NoOpScalarNormalizer implements NormalizerInterface, CacheableSupportsMethodInterface
27+
{
28+
/**
29+
* {@inheritdoc}
30+
*/
31+
public function supportsNormalization($data, $format = null): bool
32+
{
33+
return is_scalar($data);
34+
}
35+
36+
/**
37+
* {@inheritdoc}
38+
*/
39+
public function normalize($object, $format = null, array $context = [])
40+
{
41+
return $object;
42+
}
43+
44+
/**
45+
* {@inheritdoc}
46+
*/
47+
public function hasCacheableSupportsMethod(): bool
48+
{
49+
return true;
50+
}
51+
}

tests/Bridge/Symfony/Bundle/DependencyInjection/ApiPlatformExtensionTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -836,6 +836,7 @@ private function getPartialContainerBuilderProphecy()
836836
'api_platform.serializer.group_filter',
837837
'api_platform.serializer.normalizer.item',
838838
'api_platform.serializer.normalizer.item.non_resource',
839+
'api_platform.serializer.normalizer.no_op_scalar',
839840
'api_platform.serializer.property_filter',
840841
'api_platform.serializer_locator',
841842
'api_platform.subresource_data_provider',

0 commit comments

Comments
 (0)