Skip to content

Commit f1defd6

Browse files
committed
fix(mongodb): start fixing filters when relations
1 parent f75bb0b commit f1defd6

File tree

3 files changed

+33
-4
lines changed

3 files changed

+33
-4
lines changed

src/Doctrine/Odm/Filter/ExactFilter.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,13 @@ final class ExactFilter implements FilterInterface, OpenApiParameterFilterInterf
3030
public function apply(Builder $aggregationBuilder, string $resourceClass, ?Operation $operation = null, array &$context = []): void
3131
{
3232
$parameter = $context['parameter'];
33-
$value = $parameter->getValue();
3433
$property = $parameter->getProperty();
3534

35+
$value = $parameter->getValue();
36+
if (is_numeric($value)) {
37+
$property .= '.id';
38+
}
39+
3640
$aggregationBuilder
3741
->match()
3842
->field($property)

src/Doctrine/Odm/Filter/IriFilter.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,18 @@ public function apply(Builder $aggregationBuilder, string $resourceClass, ?Opera
3333
{
3434
$parameter = $context['parameter'];
3535
$value = $parameter->getValue();
36-
$property = $parameter->getProperty();
36+
37+
$isIterable = is_iterable($value);
38+
if ($isIterable) {
39+
$ids = array_map(static fn (object $object) => $object->getId(), iterator_to_array($value));
40+
} else {
41+
$ids = \is_object($value) ? $value->getId() : $value;
42+
}
3743

3844
$aggregationBuilder
3945
->match()
40-
->field($property)
41-
->{(is_iterable($value)) ? 'in' : 'equals'}($value);
46+
->field('id')
47+
->{$isIterable ? 'in' : 'equals'}($ids);
4248
}
4349

4450
public static function getParameterProvider(): string

tests/Functional/Parameters/IriFilterTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@
2121
use ApiPlatform\Tests\RecreateSchemaTrait;
2222
use ApiPlatform\Tests\SetupClassResourcesTrait;
2323
use Doctrine\ODM\MongoDB\MongoDBException;
24+
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
25+
use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface;
26+
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
27+
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
28+
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
2429

2530
final class IriFilterTest extends ApiTestCase
2631
{
@@ -37,6 +42,13 @@ public static function getResources(): array
3742
return [ChickenCoop::class, Chicken::class];
3843
}
3944

45+
/**
46+
* @throws TransportExceptionInterface
47+
* @throws ServerExceptionInterface
48+
* @throws RedirectionExceptionInterface
49+
* @throws DecodingExceptionInterface
50+
* @throws ClientExceptionInterface
51+
*/
4052
public function testIriFilter(): void
4153
{
4254
$client = $this->createClient();
@@ -45,6 +57,13 @@ public function testIriFilter(): void
4557
$this->assertEquals(['/chickens/2'], $res['member'][0]['chickens']);
4658
}
4759

60+
/**
61+
* @throws TransportExceptionInterface
62+
* @throws ServerExceptionInterface
63+
* @throws RedirectionExceptionInterface
64+
* @throws DecodingExceptionInterface
65+
* @throws ClientExceptionInterface
66+
*/
4867
public function testIriFilterMultiple(): void
4968
{
5069
$client = $this->createClient();

0 commit comments

Comments
 (0)