Skip to content

Commit

Permalink
feat: add support for uuid and ulid in SearchFilter
Browse files Browse the repository at this point in the history
  • Loading branch information
davy-beauzil committed Jan 30, 2024
1 parent 6bcc1be commit a13b36f
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
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 @@ protected function filterProperty(string $property, $value, Builder $aggregation
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 @@ protected function filterProperty(string $property, $value, QueryBuilder $queryB
}

$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 @@ protected function filterProperty(string $property, $value, QueryBuilder $queryB

$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 @@ protected function filterProperty(string $property, $value, QueryBuilder $queryB
$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

0 comments on commit a13b36f

Please sign in to comment.