Skip to content

Commit 1acad91

Browse files
committed
Optimise serialization if output dto defined as same class
Previous push reverted a change which is beneficial to performance. Instead of re-looping over the serializer/normalizers, if the DTO output class is defined as the same as the resource, we can detect this in the 1st round of serialization
1 parent 27e545b commit 1acad91

File tree

4 files changed

+8
-13
lines changed

4 files changed

+8
-13
lines changed

src/JsonLd/Serializer/ItemNormalizer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@ public function supportsNormalization($data, $format = null): bool
6565
*/
6666
public function normalize($object, $format = null, array $context = [])
6767
{
68-
6968
$objectClass = $this->getObjectClass($object);
7069
$outputClass = $this->getOutputClass($objectClass, $context);
70+
7171
if (null !== $outputClass && !isset($context[self::IS_TRANSFORMED_TO_SAME_CLASS_CONTEXT_KEY])) {
7272
return parent::normalize($object, $format, $context);
7373
}

src/Serializer/AbstractItemNormalizer.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public function __construct(PropertyNameCollectionFactoryInterface $propertyName
9292
*/
9393
public function supportsNormalization($data, $format = null)
9494
{
95-
if (!\is_object($data)) {
95+
if (!\is_object($data) || $data instanceof \Traversable) {
9696
return false;
9797
}
9898

@@ -114,13 +114,12 @@ public function hasCacheableSupportsMethod(): bool
114114
*/
115115
public function normalize($object, $format = null, array $context = [])
116116
{
117-
118117
if (!($isTransformed = isset($context[self::IS_TRANSFORMED_TO_SAME_CLASS_CONTEXT_KEY])) && $outputClass = $this->getOutputClass($this->getObjectClass($object), $context)) {
119118
if (!$this->serializer instanceof NormalizerInterface) {
120119
throw new LogicException('Cannot normalize the output because the injected serializer is not a normalizer');
121120
}
122121

123-
if ($object !== $transformed = $this->transformOutput($object, $context, $outputClass)) {
122+
if ($object !== $transformed = $this->transformOutput($object, $outputClass, $context)) {
124123
$context['api_normalize'] = true;
125124
$context['api_resource'] = $object;
126125
unset($context['output'], $context['resource_class']);
@@ -646,13 +645,8 @@ protected function getDataTransformer($data, string $to, array $context = []): ?
646645
* For a given resource, it returns an output representation if any
647646
* If not, the resource is returned.
648647
*/
649-
650-
protected function transformOutput($object, array $context = [], string $outputClass = null)
648+
protected function transformOutput($object, string $outputClass, array $context = [])
651649
{
652-
if (null === $outputClass) {
653-
$outputClass = $this->getOutputClass($this->getObjectClass($object), $context);
654-
}
655-
656650
if (null !== $outputClass && null !== $dataTransformer = $this->getDataTransformer($object, $outputClass, $context)) {
657651
return $dataTransformer->transform($object, $outputClass, $context);
658652
}

tests/Fixtures/TestBundle/DataTransformer/OutputDtoUnmodifiedDataTransformer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyDtoOutputFallbackToSameClass;
1919

2020
/**
21-
* OutputDtoUnmodifiedDataTransformer
21+
* OutputDtoUnmodifiedDataTransformer.
2222
*
2323
* @author Daniel West <[email protected]>
2424
*/

tests/Fixtures/TestBundle/Dto/OutputDtoDummy.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@
1414
namespace ApiPlatform\Core\Tests\Fixtures\TestBundle\Dto;
1515

1616
/**
17-
* OutputDtoDummy
17+
* OutputDtoDummy.
1818
*
1919
* @author Daniel West <[email protected]>
2020
*/
2121
class OutputDtoDummy
22-
{}
22+
{
23+
}

0 commit comments

Comments
 (0)