Skip to content
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

feat: add support for Uuid and Ulid in SearchFilter #6131

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/Doctrine/Odm/Filter/SearchFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
use Symfony\Component\Uid\Ulid;
use Symfony\Component\Uid\Uuid;

/**
* The search filter allows to filter a collection by given properties.
Expand Down Expand Up @@ -221,7 +223,14 @@
return;
}

$values = array_map($this->getIdFromValue(...), $values);
$values = array_map(function ($value) {
$id = $this->getIdFromValue($value);
if ($id instanceof Uuid || $id instanceof Ulid) {
return $id->toBinary();

Check warning on line 229 in src/Doctrine/Odm/Filter/SearchFilter.php

View check run for this annotation

Codecov / codecov/patch

src/Doctrine/Odm/Filter/SearchFilter.php#L226-L229

Added lines #L226 - L229 were not covered by tests
}

return $id;
}, $values);

Check warning on line 233 in src/Doctrine/Odm/Filter/SearchFilter.php

View check run for this annotation

Codecov / codecov/patch

src/Doctrine/Odm/Filter/SearchFilter.php#L232-L233

Added lines #L232 - L233 were not covered by tests

$associationResourceClass = $metadata->getAssociationTargetClass($field);
$associationFieldIdentifier = $metadata->getIdentifierFieldNames()[0];
Expand Down
5 changes: 3 additions & 2 deletions src/Doctrine/Orm/Filter/SearchFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
use Symfony\Component\Uid\Ulid;
use Symfony\Component\Uid\Uuid;

/**
* The search filter allows to filter a collection by given properties.
Expand Down Expand Up @@ -196,7 +198,6 @@
}

$metadata = $this->getNestedMetadata($resourceClass, $associations);

if ($metadata->hasField($field)) {
if ('id' === $field) {
$values = array_map($this->getIdFromValue(...), $values);
Expand Down Expand Up @@ -255,6 +256,7 @@

$expected = \count($values);
$values = array_filter($values, static fn ($value) => null !== $value);
$values = array_map(static fn ($value) => $value instanceof Uuid || $value instanceof Ulid ? $value->toBinary() : $value, $values);

Check warning on line 259 in src/Doctrine/Orm/Filter/SearchFilter.php

View check run for this annotation

Codecov / codecov/patch

src/Doctrine/Orm/Filter/SearchFilter.php#L259

Added line #L259 was not covered by tests
if ($expected > \count($values)) {
/*
* Shouldn't this actually fail harder?
Expand All @@ -272,7 +274,6 @@
$associationAlias = QueryBuilderHelper::addJoinOnce($queryBuilder, $queryNameGenerator, $alias, $associationField);
$associationField = $associationFieldIdentifier;
}

$this->addWhereByStrategy($strategy, $queryBuilder, $queryNameGenerator, $associationAlias, $associationField, $values, $caseSensitive);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ class JsonSchemaContextDummy
* @var int The id
*
* @ApiProperty(identifier=true)
*
* @ODM\Id(strategy="INCREMENT", type="int")
*/
private $id;
Expand Down
Loading