8000 Since Symfony 5.1, the serialization of Paginator that contains no data no more result in an empty array, but is an empty object · Issue #1550 · api-platform/api-platform · GitHub
[go: up one dir, main page]

Skip to content
Since Symfony 5.1, the serialization of Paginator that contains no data no more result in an empty array, but is an empty object #1550
@Rebolon

Description

@Rebolon

API Platform version(s) affected: v2.5.6

Description
On a Collection Get resource with Pagination plugin, when the collection is empty, the expected body should be an empty array. Both json & ld+json was working before 5.1.x
On this version of Symfony, now it will no more normalize the Paginator, but will just return the object and the response will be an empty object like this "{}" instead of the empty array like this "[]"

The problem comes from the Symfony\Component\Serializer\Serializer::normalize.
Before 5.1.0 it does this:

        if (\is_array($data) || $data instanceof \Traversable) {
            $normalized = [];
            foreach ($data as $key => $val) {
                $normalized[$key] = $this->normalize($val, $format, $context);
            }

            return $normalized;
        }

Since Symfony 5.1.0 it also adds a check on data lenght like this:

if (\is_array($data) || $data instanceof \Traversable) {
            if ($data instanceof \Countable && 0 === $data->count()) {
                return $data;
            }

            $normalized = [];
            foreach ($data as $key => $val) {
                $normalized[$key] = $this->normalize($val, $format, $context);
            }

            return $normalized;
        }

How to reproduce
With a simple DataProvider that implements CollectionDataProviderInterface and which query returns nothing (add a where 1 = 0) :

public function getCollection(string $resourceClass, string $operationName = null, array $context = [])
    {
        $repository = $this->managerRegistry->getRepository(AnyEntity::class);
        $queryBuilder = $prescripterRepository->createQueryBuilder('pre');
        $queryBuilder->where("1 = 0");

        $data = null;
        foreach ($this->collectionExtensions as $extension) {
            $extension->applyToCollection($queryBuilder, new QueryNameGenerator(), $resourceClass, $operationName, $context);
            if ($extension instanceof QueryResultCollectionExtensionInterface
                && $extension->supportsResult($resourceClass, $operationName)
            ) {
                $data = $extension->getResult($queryBuilder);
            }
        }

        if (!$data) {
            $data = $queryBuilder->getQuery()->getResult();
        }

        return $data;
    }

Possible Solution
No solution for instance

Additional Context
Json response before 5.1 : []
Json response after 5.1 : {}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0