Skip to content
Closed
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
4 changes: 4 additions & 0 deletions src/Core/Hydra/Serializer/CollectionNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use ApiPlatform\Core\JsonLd\ContextBuilderInterface;
use ApiPlatform\Core\JsonLd\Serializer\JsonLdContextTrait;
use ApiPlatform\Core\Serializer\ContextTrait;
use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
Expand Down Expand Up @@ -78,6 +79,9 @@ public function supportsNormalization($data, $format = null)
public function normalize($object, $format = null, array $context = [])
{
if (!isset($context['resource_class']) || isset($context['api_sub_level'])) {
if (($context[AbstractObjectNormalizer::PRESERVE_EMPTY_OBJECTS] ?? false) && $object instanceof \Countable && 0 === $object->count()) {
return $object;
}
return $this->normalizeRawCollection($object, $format, $context);
}

Expand Down
4 changes: 4 additions & 0 deletions src/Core/Serializer/AbstractCollectionNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use ApiPlatform\Core\DataProvider\PartialPaginatorInterface;
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
Expand Down Expand Up @@ -79,6 +80,9 @@ public function hasCacheableSupportsMethod(): bool
public function normalize($object, $format = null, array $context = [])
{
if (!isset($context['resource_class']) || isset($context['api_sub_level'])) {
if (($context[AbstractObjectNormalizer::PRESERVE_EMPTY_OBJECTS] ?? false) && $object instanceof \Countable && 0 === $object->count()) {
return $object;
}
return $this->normalizeRawCollection($object, $format, $context);
}

Expand Down
22 changes: 22 additions & 0 deletions tests/Core/Hydra/Serializer/CollectionNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use ApiPlatform\Tests\Fixtures\NotAResource;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Serializer\SerializerInterface;

Expand Down Expand Up @@ -256,6 +257,27 @@ public function testNormalizeSubLevelNonResourceCollection()
], $actual);
}

public function testPreserveEmptyObject()
{
$data = new \ArrayObject();

$contextBuilderProphecy = $this->prophesize(ContextBuilderInterface::class);
$resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
$iriConverterProphecy = $this->prophesize(IriConverterInterface::class);

$normalizer = new CollectionNormalizer($contextBuilderProphecy->reveal(), $resourceClassResolverProphecy->reveal(), $iriConverterProphecy->reveal());

$actual = $normalizer->normalize($data, CollectionNormalizer::FORMAT, [
AbstractObjectNormalizer::PRESERVE_EMPTY_OBJECTS => true
]);

$this->assertSame($data, $actual);

$actual = $normalizer->normalize($data, CollectionNormalizer::FORMAT, []);

$this->assertSame([], $actual);
}

public function testNormalizePaginator()
{
$this->assertEquals(
Expand Down
21 changes: 21 additions & 0 deletions tests/Core/JsonApi/Serializer/CollectionNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use ApiPlatform\Core\Tests\ProphecyTrait;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;

/**
Expand Down Expand Up @@ -338,4 +339,24 @@ public function testNormalizeWithoutDataKey()
'resource_class' => 'Foo',
]);
}

public function testPreserveEmptyObject()
{
$data = new \ArrayObject();

$resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
$resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataFactoryInterface::class);

$normalizer = new CollectionNormalizer($resourceClassResolverProphecy->reveal(), 'page', $resourceMetadataFactoryProphecy->reveal());

$actual = $normalizer->normalize($data, CollectionNormalizer::FORMAT, [
AbstractObjectNormalizer::PRESERVE_EMPTY_OBJECTS => true
]);

$this->assertSame($data, $actual);

$actual = $normalizer->normalize($data, CollectionNormalizer::FORMAT, []);

$this->assertSame([], $actual);
}
}