Skip to content

Pagination is broken when mapping is set #7590

@Spomky

Description

@Spomky

API Platform version(s) affected: 4.2.8

Description
I have the following Api Resource

<?php

declare(strict_types=1);

namespace Api\ApiResource;

use Api\Entity\Customer as CustomerEntity;
use ApiPlatform\Doctrine\Orm\Filter\PartialSearchFilter;
use ApiPlatform\Doctrine\Orm\State\CollectionProvider;
use ApiPlatform\Doctrine\Orm\State\ItemProvider;
use ApiPlatform\Doctrine\Orm\State\Options;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\QueryParameter;

#[ApiResource(
    shortName: 'Customer',
    operations: [
        new Get(
            uriTemplate: '/customers/{id}',
            uriVariables: ['id'],
            provider: ItemProvider::class,
        ),
        new GetCollection(
            uriTemplate: '/customers',
            provider: CollectionProvider::class,
            parameters: [
                'name' => new QueryParameter(
                    filter: new PartialSearchFilter(),
                    property: 'name',
                ),
            ],
        ),
    ],
    stateOptions: new Options(CustomerEntity::class),
)]
class Customer
{
    public string $id;
    public string $name;
}

The collection enpoint returns the correct pagination information:

{
  "@context": "/contexts/Customer",
  "@id": "/customers",
  "@type": "Collection",
  "totalItems": 36,
  "member": [
    //Removed for brievety
  ],
  "view": {
    "@id": "/customers?page=1",
    "@type": "PartialCollectionView",
    "first": "/customers?page=1",
    "last": "/customers?page=2",
    "next": "/customers?page=2"
  },
  "search": {
    "@type": "IriTemplate",
    "template": "/customers{?name}",
    "variableRepresentation": "BasicRepresentation",
    "mapping": [
      {
        "@type": "IriTemplateMapping",
        "variable": "name",
        "property": "name",
        "required": false
      }
    ]
  }
}

When I add the #[Map(CustomerEntity::class)] attribute, the pagination is gone and the total number of items is wrong:

{
  "@context": "/contexts/Customer",
  "@id": "/customers",
  "@type": "Collection",
  "totalItems": 30,
  "member": [
    //Removed for brievety
  ],
  "search": {
    "@type": "IriTemplate",
    "template": "/customers{?name}",
    "variableRepresentation": "BasicRepresentation",
    "mapping": [
      {
        "@type": "IriTemplateMapping",
        "variable": "name",
        "property": "name",
        "required": false
      }
    ]
  }
}

How to reproduce
Use the provider above and attribute.

Possible Solution
It looks like the MappedObjectProvider is called and returns an ArrayPaginator and total items or next page are missing.

Additional Context

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