Skip to content

Handle binary UUID in SearchFilter - #3774

Merged
alanpoulain merged 5 commits into
api-platform:2.6from
odoucet:uuidbin-search
Feb 19, 2021
Merged

Handle binary UUID in SearchFilter#3774
alanpoulain merged 5 commits into
api-platform:2.6from
odoucet:uuidbin-search

Conversation

@odoucet

@odoucetodoucet commented Oct 20, 2020

Copy link
Copy Markdown
Contributor
QA
Bug fix?yes
New feature?no
BC breaks?no
Deprecations?no
Ticketsfixesapi-platform/api-platform#1139
LicenseMIT

When searching on a binary UUID field, current behaviour leads to search done on Doctrine with the string UUID and not the binary version of it.
This is because the field type must be provided to Doctrine each time we use QueryBuilder. Why ? Because Doctrine sees string on input, and does not know it needs conversion.

This PR reads $metadata->getTypeOfField() and force field type.

WARNING
Current PR passes the unit tests (so it does not break current behaviour), but does not implement two new needed tests (to make sure it fixes the issue mentioned) :

  • search on a single UUIDBinary
  • search with multiple UUIDBinary values

Test has been done on a local project so I know it works, but better to check this with a test :)

I need some help adding custom type to SearchFilterTestTrait as uuidBinary is not recognized.

@alanpoulain

Copy link
Copy Markdown
Member

Since it's a bugfix, it should target 2.5.

Comment threadsrc/Bridge/Doctrine/Orm/Filter/SearchFilter.php Outdated

@odoucetodoucet left a comment

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used a less generic parameter name ... Tell me if it's the right way to do.

Comment threadsrc/Bridge/Doctrine/Orm/Filter/SearchFilter.php Outdated
@odoucetodoucet changed the title WIP - Handle binary UUID in SearchFilterHandle binary UUID in SearchFilterOct 22, 2020
Comment threadsrc/Bridge/Doctrine/Orm/Filter/SearchFilter.php Outdated
Comment threadsrc/Bridge/Doctrine/Orm/Filter/SearchFilter.php Outdated
Comment threadsrc/Bridge/Doctrine/Orm/Filter/SearchFilter.php Outdated
@odoucet
odoucet requested a review from dunglasOctober 24, 2020 10:35
@soyuka

Copy link
Copy Markdown
Member

Nice patch, please target 2.5

@soyukasoyuka added the Ready label Nov 8, 2020
@odoucet

Copy link
Copy Markdown
ContributorAuthor

Updated to use QueryNameGenerator for variable names.

for ($i = 0; $i < $nbArgs; ++$i) {
$inQuery[] = ':filterproperty'.$i;
$parameters->add(new Parameter('filterproperty'.$i, $caseSensitive ? $values[$i] : strtolower($values[$i]), $type));
$args = [];

@alanpoulainalanpoulainNov 9, 2020

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not always using this conditional branch instead of having an if / else?
It will:

  • make the code easier to read (and maintain),
  • prevent bugs to arise only in one conditional branch.

WDYT @soyuka@dunglas?

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will require updating some tests expectations because when multiple values are given, they are set as one unique array parameter, and that will become several parameters. See https://github.com/api-platform/core/pull/3774/checks?check_run_id=1379433208#step:14:91

I'm currently blocked at adding UUID tests, I cant fully understand yet how tests are organized and how can I add custom types.

\Doctrine\DBAL\Types\Type::addType('uuid', \Ramsey\Uuid\Doctrine\UuidType::class);

is required to manipulate uuid types.

Comment threadsrc/Bridge/Doctrine/Orm/Filter/SearchFilter.php Outdated
@odoucet

Copy link
Copy Markdown
ContributorAuthor

I've pushed an attempt to add test on UUID field, but the current test failed because SearchFilterTest::testGetDescriptionDefaultFields() failed on $filter->getDescription : it seems the uuid type is never loaded, despite adding it in DoctrineOrmFilterTestCase:setUp().
I need help on this one... or I can update my branch with no additional test and it can be merged right away.

@soyuka

Copy link
Copy Markdown
Member

if you need more help @odoucet ping me on symfony's slack!

Base automatically changed from master to mainJanuary 23, 2021 21:59
@odoucet

Copy link
Copy Markdown
ContributorAuthor

I'll update my PR with latest master branch in a few days.
I need help on how to provide test code for this (I did not understand how to create custom doctrine types in unit tests).

@odoucet

Copy link
Copy Markdown
ContributorAuthor

Hello,
I've updated my pull request to be up to date with latest master.
This MR lacks specific unit tests because I was unable to add a binary doctrine type to tests. @soyuka if you can help, it would be great :)
This MR can also be merged as-is, I know it works because I use binary type in a private project.

@alanpoulain
alanpoulain changed the base branch from main to 2.6February 19, 2021 12:34
@alanpoulain
alanpoulainforce-pushed the uuidbin-search branch 2 times, most recently from 4ded0f9 to 16dd558CompareFebruary 19, 2021 12:39
@alanpoulain

Copy link
Copy Markdown
Member

Hello @odoucet,
Adding PHPUnit tests (they are integration tests, not really unit ones) is not possible since the conversion is done at the execution.
I've added Behat tests instead.

@alanpoulain
alanpoulain merged commit ff248ae into api-platform:2.6Feb 19, 2021
@alanpoulain

Copy link
Copy Markdown
Member

Thank you @odoucet.

@odoucet
odoucet deleted the uuidbin-search branch March 2, 2021 09:53
soyuka added a commit to soyuka/core that referenced this pull request Mar 13, 2021
soyuka added a commit to soyuka/core that referenced this pull request Mar 13, 2021
alanpoulain pushed a commit to soyuka/core that referenced this pull request Mar 16, 2021
alanpoulain added a commit that referenced this pull request Mar 16, 2021
* Revert "Handle binary UUID in SearchFilter (#3774)"
This reverts commit ff248ae.
Co-authored-by: Alan Poulain <contact@alanpoulain.eu>
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SearchFilter doesnt transform uuid parameter to binary, instead uses string represenation in sql

4 participants

@odoucet@alanpoulain@soyuka@dunglas