Skip to content

Forward operation name in the iri converter #1874

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 20, 2018

Conversation

soyuka
Copy link
Member

@soyuka soyuka commented Apr 18, 2018

Q A
Bug fix? yes
New feature? no
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets api-platform/api-platform#644 kinda
License MIT
Doc PR na

Let's say you declare a custom operation to identify a resource by something different then id:

resources:
    App\Product\Entity\Product:
        itemOperations:
            get:
                method: 'GET'
            put:
                method: 'PUT'
            get_by_code:
                method: 'GET'
                path: '/products/by_code/{id}'

Then, you just have to create a custom item data provider:

<?php

declare(strict_types=1);

namespace App\Product\DataProvider;

use ApiPlatform\Core\DataProvider\ItemDataProviderInterface;
use ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface;
use App\Product\Entity\Product;
use Doctrine\Common\Persistence\ManagerRegistry;
use Symfony\Component\HttpKernel\Exception\HttpException;

final class ProductByCodeItemDataProvider implements ItemDataProviderInterface, RestrictedDataProviderInterface
{
    private $repository;

    public function __construct(ManagerRegistry $managerRegistry)
    {
        $this->repository = $managerRegistry->getManagerForClass(Product::class)->getRepository(Product::class);
    }

    public function getItem(string $resourceClass, $id, string $operationName = null, array $context = [])
    {
        $product = $id['id'];

        if (!$product) {
            throw new HttpException(400, 'No "product" parameter.');
        }

        $queryBuilder = $this->repository->createQueryBuilder('p');

        $queryBuilder->select('p')
            ->where('p.code = :product')
            ->setParameter('product', $product);

        return $queryBuilder->getQuery()->getOneOrNullResult();
    }

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

This is really easy to pull up. Still it won't work when denormalizing a custom IRI like /api/products/by_code/12345. The only thing missing was the operationName that we have in the IRIConverter.
With this patch you can now use your custom IRIs in denormalization (denormalizeRelation in the normalizer for example).

ping @gries might fix your issue

@dunglas dunglas merged commit 2fe4544 into api-platform:2.2 Apr 20, 2018
@dunglas
Copy link
Member

dunglas commented Apr 20, 2018

Good catch! Thanks @soyuka

@soyuka soyuka deleted the fix-iri-converter branch April 20, 2018 09:01
@soyuka soyuka restored the fix-iri-converter branch May 2, 2018 16:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants