Skip to content

Custom operation identifier has a different type from the entity's one but gets normalized with the entity's identifier type. #2116

Closed
@blesenechal

Description

@blesenechal

I have a GET operation that retrieves an item based on a unique parameter which is a string.
It works fine with 2.2.9, but not with 2.3.0.

It looks like the query parameter value is casted to an int somewhere.

==================================

Example with the api-platform/demo project, to get the books by ISBN

1 - I create a custom operation with "requirements" (as defined in https://api-platform.com/docs/core/operations) :

 * @ApiResource(
 *     itemOperations={
 *         "get"={"method"="GET", "path"="/books/{id}"},
 *         "get_by_isbn"={"method"="GET", "path"="/books/by_isbn/{id}", "requirements"={"id"=".+"}},
 *     }
 * )

2 - I create a data provider :

namespace App\DataProvider;

use ApiPlatform\Core\DataProvider\ItemDataProviderInterface;
use ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface;
use ApiPlatform\Core\Exception\ResourceClassNotSupportedException;
use App\Entity\Book;
use Doctrine\ORM\EntityManagerInterface;

final class BookIsbnItemDataProvider implements ItemDataProviderInterface, RestrictedDataProviderInterface
{
    private $entityManager;

    public function __construct(EntityManagerInterface $entityManager)
    {
        $this->entityManager = $entityManager;
    }

    public function supports(string $resourceClass, string $operationName = null, array $context = []): bool
    {
        return (Book::class === $resourceClass) && ('get_by_isbn' === $operationName);
    }

    public function getItem(string $resourceClass, $identifiers, string $operationName = null, array $context = []): ?BlogPost
    {
        return $this->entityManager->getRepository(Book::class)->findOneBy(['isbn' => $identifiers]);
    }
}

3 - When executing the request GET /books/by_isbn/plop :

  • With 2.2.9 : In getItem, $identifiers is 'plop' (string)
  • With 2.3.0 : In getItem, $identifiers is 0 (int)

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions