Home > Mobile >  How can I resolve issue on Symfony ApiPlatform SearchFilter import?
How can I resolve issue on Symfony ApiPlatform SearchFilter import?

Time:10-17

I made an API with Symfony / ApiPlatform. I followed the official doc of ApiPlatform concerning filters on queries but I just can't use the ApiFilter "SearchFilter".

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use ApiPlatform\Metadata\ApiFilter;
use ApiPlatform\Doctrine\Orm\Filter\SearchFilter;
use ApiPlatform\Doctrine\Orm\Filter\DateFilter;
use ApiPlatform\Core\Annotation\ApiResource;

#[ORM\Entity(repositoryClass: PostRepository::class)]
#[
    ApiResource(
        graphql: [
            //queries
            'item_query' => [
                'normalization_context' => [
                    'groups' => ['read:Post']
                ]
            ],
            'collection_query' => [
                'normalization_context' => [
                    'groups' => ['read:Posts'],
                ],
                'denormalizationContext' => [
                    'groups' => ['write:Post']
                ]
            ],
        ]
    ),
]
#[ApiFilter(DateFilter::class, properties: ['date'])]
#[ApiFilter(SearchFilter::class, properties: ['author' => 'exact'])]
Class Post {
//...
    #[ORM\ManyToOne(inversedBy: 'posts')]
    #[ORM\JoinColumn(nullable: false)]
    #[Groups(['read:Post', 'read:Posts'])]
    private ?User $author = null;
}

Api returning 500 on fetching schemas. Error below:

ApiPlatform\Doctrine\Orm\Filter\SearchFilter::__construct(): Argument #2 ($iriConverter) must be of type ApiPlatform\Api\IriConverterInterface, ApiPlatform\Core\Bridge\Symfony\Routing\IriConverter given, called in ...myway/var/cache/dev/ContainerE6FeJyy/getAnnotatedAppEntityPostApiPlatformDoctrineOrmFilterSearchFilterService.php on line 29 Doc ApiPlatform: https://api-platform.com/docs/core/filters/

I don't understand the issue concerning "SearchFilter". Also the "DateFilter" works fine. (This is not problem with gql support because with basic REST support, the error is identical on localhost/api) Any help is welcome! Thanks you so much!

CodePudding user response:

So, there were conflicts between my current version of ApiPlatform (the deprecated v2.6) and the new serializer annotations that appeared with v2.7/v3. So I started the migration to v2.7 of apiPlatform and corrected the import breaking changes, and that's it.

(Also, $ php bin/console api:upgrade-resource -f helped me to update my entity files.)

Everything works well. Thanks @HarveyDent, you were right it was concerning my version of api-platform.

  • Related