Skip to content
Merged
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
3 changes: 3 additions & 0 deletions src/Doctrine/Odm/Filter/ComparisonFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ final class ComparisonFilter implements FilterInterface, OpenApiParameterFilterI
'gte' => 'gte',
'lt' => 'lt',
'lte' => 'lte',
'ne' => 'notEqual',
];

public function __construct(private readonly FilterInterface $filter)
Expand Down Expand Up @@ -91,6 +92,7 @@ public function getOpenApiParameters(Parameter $parameter): array
new OpenApiParameter(name: "{$key}[gte]", in: $in),
new OpenApiParameter(name: "{$key}[lt]", in: $in),
new OpenApiParameter(name: "{$key}[lte]", in: $in),
new OpenApiParameter(name: "{$key}[ne]", in: $in),
];
}

Expand All @@ -108,6 +110,7 @@ public function getSchema(Parameter $parameter): array
'gte' => $innerSchema,
'lt' => $innerSchema,
'lte' => $innerSchema,
'ne' => $innerSchema,
],
];
}
Expand Down
3 changes: 3 additions & 0 deletions src/Doctrine/Orm/Filter/ComparisonFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ final class ComparisonFilter implements FilterInterface, OpenApiParameterFilterI
'gte' => '>=',
'lt' => '<',
'lte' => '<=',
'ne' => '!=',
];

public const ALLOWED_DQL_OPERATORS = ['=', '>', '>=', '<', '<=', '!=', '<>'];
Expand Down Expand Up @@ -91,6 +92,7 @@ public function getOpenApiParameters(Parameter $parameter): array
new OpenApiParameter(name: "{$key}[gte]", in: $in),
new OpenApiParameter(name: "{$key}[lt]", in: $in),
new OpenApiParameter(name: "{$key}[lte]", in: $in),
new OpenApiParameter(name: "{$key}[ne]", in: $in),
];
}

Expand All @@ -108,6 +110,7 @@ public function getSchema(Parameter $parameter): array
'gte' => $innerSchema,
'lt' => $innerSchema,
'lte' => $innerSchema,
'ne' => $innerSchema,
],
];
}
Expand Down
5 changes: 5 additions & 0 deletions tests/Fixtures/TestBundle/Entity/Uuid/SymfonyUuidDevice.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace ApiPlatform\Tests\Fixtures\TestBundle\Entity\Uuid;

use ApiPlatform\Doctrine\Orm\Filter\ComparisonFilter;
use ApiPlatform\Doctrine\Orm\Filter\UuidFilter;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Get;
Expand All @@ -29,6 +30,10 @@
'id' => new QueryParameter(
filter: new UuidFilter(),
),
'idComparison' => new QueryParameter(
filter: new ComparisonFilter(new UuidFilter()),
property: 'id',
),
]
),
new Post(),
Expand Down
20 changes: 19 additions & 1 deletion tests/Functional/Parameters/ComparisonFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,24 @@ public function testCombinedGtAndLt(): void
$this->assertSame(['Bravo', 'Charlie'], $names);
}

public function testNe(): void
{
// ne "Bravo": all names except "Bravo" → Alpha, Charlie, Delta
$response = self::createClient()->request('GET', '/chickens?nameComparison[ne]=Bravo');
$this->assertResponseIsSuccessful();
$names = array_map(static fn ($c) => $c['name'], $response->toArray()['member']);
sort($names);
$this->assertSame(['Alpha', 'Charlie', 'Delta'], $names);
}

public function testNeNoMatch(): void
{
// ne "Unknown": no chicken has that name, so all are returned
$response = self::createClient()->request('GET', '/chickens?nameComparison[ne]=Unknown&itemsPerPage=10');
$this->assertResponseIsSuccessful();
$this->assertCount(4, $response->toArray()['member']);
}

public function testGtNoResults(): void
{
// gt "ZZZZ": no name is alphabetically after "ZZZZ"
Expand Down Expand Up @@ -125,7 +143,7 @@ public function testOpenApiDocumentation(): void
$parameters = $openApiDoc['paths']['/chickens']['get']['parameters'];
$parameterNames = array_column($parameters, 'name');

foreach (['nameComparison[gt]', 'nameComparison[gte]', 'nameComparison[lt]', 'nameComparison[lte]'] as $expectedName) {
foreach (['nameComparison[gt]', 'nameComparison[gte]', 'nameComparison[lt]', 'nameComparison[lte]', 'nameComparison[ne]'] as $expectedName) {
$this->assertContains($expectedName, $parameterNames, \sprintf('Expected parameter "%s" in OpenAPI documentation', $expectedName));
}

Expand Down
187 changes: 187 additions & 0 deletions tests/Functional/Uuid/UuidComparisonFilterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Tests\Functional\Uuid;

use ApiPlatform\Symfony\Bundle\Test\ApiTestCase;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Uuid\SymfonyUuidDevice;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Uuid\SymfonyUuidDeviceEndpoint;
use ApiPlatform\Tests\RecreateSchemaTrait;
use ApiPlatform\Tests\SetupClassResourcesTrait;

final class UuidComparisonFilterTest extends ApiTestCase
{
use RecreateSchemaTrait;
use SetupClassResourcesTrait;

protected static ?bool $alwaysBootKernel = false;

/**
* @return class-string[]
*/
public static function getResources(): array
{
return [SymfonyUuidDevice::class, SymfonyUuidDeviceEndpoint::class];
}

protected function setUp(): void
{
parent::setUp();

if ($this->isMongoDB()) {
$this->markTestSkipped('UuidFilter is ORM only.');
}
}

public function testGtWithUuid(): void
{
$this->recreateSchema(static::getResources());

$manager = $this->getManager();
$manager->persist($device1 = new SymfonyUuidDevice());
usleep(1000);
$manager->persist($device2 = new SymfonyUuidDevice());
usleep(1000);
$manager->persist($device3 = new SymfonyUuidDevice());
$manager->flush();

$uuids = [(string) $device1->id, (string) $device2->id, (string) $device3->id];
sort($uuids);

$response = self::createClient()->request('GET', '/symfony_uuid_devices', [
'query' => [
'idComparison' => ['gt' => $uuids[1]],
],
]);

self::assertResponseIsSuccessful();
$json = $response->toArray();
self::assertSame(1, $json['hydra:totalItems']);
self::assertSame($uuids[2], $json['hydra:member'][0]['id']);
}

public function testGteWithUuid(): void
{
$this->recreateSchema(static::getResources());

$manager = $this->getManager();
$manager->persist($device1 = new SymfonyUuidDevice());
usleep(1000);
$manager->persist($device2 = new SymfonyUuidDevice());
usleep(1000);
$manager->persist($device3 = new SymfonyUuidDevice());
$manager->flush();

$uuids = [(string) $device1->id, (string) $device2->id, (string) $device3->id];
sort($uuids);

$response = self::createClient()->request('GET', '/symfony_uuid_devices', [
'query' => [
'idComparison' => ['gte' => $uuids[1]],
],
]);

self::assertResponseIsSuccessful();
$json = $response->toArray();
self::assertSame(2, $json['hydra:totalItems']);
}

public function testLtWithUuid(): void
{
$this->recreateSchema(static::getResources());

$manager = $this->getManager();
$manager->persist($device1 = new SymfonyUuidDevice());
usleep(1000);
$manager->persist($device2 = new SymfonyUuidDevice());
usleep(1000);
$manager->persist($device3 = new SymfonyUuidDevice());
$manager->flush();

$uuids = [(string) $device1->id, (string) $device2->id, (string) $device3->id];
sort($uuids);

$response = self::createClient()->request('GET', '/symfony_uuid_devices', [
'query' => [
'idComparison' => ['lt' => $uuids[1]],
],
]);

self::assertResponseIsSuccessful();
$json = $response->toArray();
self::assertSame(1, $json['hydra:totalItems']);
self::assertSame($uuids[0], $json['hydra:member'][0]['id']);
}

public function testCombinedGteAndLteWithUuid(): void
{
$this->recreateSchema(static::getResources());

$manager = $this->getManager();
$manager->persist($device1 = new SymfonyUuidDevice());
usleep(1000);
$manager->persist($device2 = new SymfonyUuidDevice());
usleep(1000);
$manager->persist($device3 = new SymfonyUuidDevice());
usleep(1000);
$manager->persist($device4 = new SymfonyUuidDevice());
$manager->flush();

$uuids = [(string) $device1->id, (string) $device2->id, (string) $device3->id, (string) $device4->id];
sort($uuids);

$response = self::createClient()->request('GET', '/symfony_uuid_devices', [
'query' => [
'idComparison' => ['gte' => $uuids[1], 'lte' => $uuids[2]],
],
]);

self::assertResponseIsSuccessful();
$json = $response->toArray();
self::assertSame(2, $json['hydra:totalItems']);
}

public function testNeWithUuid(): void
{
$this->recreateSchema(static::getResources());

$manager = $this->getManager();
$manager->persist($device1 = new SymfonyUuidDevice());
$manager->persist($device2 = new SymfonyUuidDevice());
$manager->persist($device3 = new SymfonyUuidDevice());
$manager->flush();

$response = self::createClient()->request('GET', '/symfony_uuid_devices', [
'query' => [
'idComparison' => ['ne' => (string) $device2->id],
],
]);

self::assertResponseIsSuccessful();
$json = $response->toArray();
self::assertSame(2, $json['hydra:totalItems']);

$returnedIds = array_map(static fn ($m) => $m['id'], $json['hydra:member']);
self::assertNotContains((string) $device2->id, $returnedIds);
}

protected function tearDown(): void
{
if ($this->isMongoDB()) {
return;
}

$this->recreateSchema(static::getResources());
parent::tearDown();
}
}
Loading