Skip to content

SearchFilter on an association with a Ulid primary key causes an exception #4656

Closed
@gnito-org

Description

@gnito-org

API Platform version(s) affected: 2.6.8

Description

Article has many Tags. Tag has a primary key id that's defined as a Symfony Ulid type.

A search filter on Article to find articles with a specific Tag causes an exception An exception occurred while executing a query: SQLSTATE[22P02]: Invalid text representation: 7 ERROR: invalid input syntax for type uuid: \"01FVQ6SJ6GPCE97X8JPZ6VV5ZX\. This is on PostgreSQL.

The exception is not thrown when the id is a Symfony Uuid type.

How to reproduce

See the reproducer at https://github.com/gnito-org/api-platform-ulid-demo.

Create a Tag, associate it with an Article, then do a collection get on Article with the Tag Ulid in the tags filter field.

Same exception is thrown when valid Ulid values are entered in the tags[] filter fields.

Possible Causes

Api-platform does not supply the third parameter of the QueryBuilder::setParameter() method for Ulid fields, as decribed by the Symfony docs.

Possible Solution

The easy solution is to insert something like the following into SearchFilter:

    if ('ulid' === $doctrineTypeField){
        foreach ($values as $key => $uLidValue){
            $uLid = new Ulid($uLidValue);
            $values[$key] = $uLid->toRfc4122();
        }
    }

That works... however, that makes API Platform dependent on symfony/uid.

Will it break compatibility with Symfony 4.4?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions