Skip to content

Commit b6555c8

Browse files
fix(laravel): ignore invalid range filter values (#8632)
1 parent 56c0be2 commit b6555c8

2 files changed

Lines changed: 31 additions & 0 deletions

File tree

‎src/Laravel/Eloquent/Filter/RangeFilter.php‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@
1313

1414
namespace ApiPlatform\Laravel\Eloquent\Filter;
1515

16+
use ApiPlatform\Metadata\Exception\InvalidArgumentException;
1617
use ApiPlatform\Metadata\JsonSchemaFilterInterface;
1718
use ApiPlatform\Metadata\OpenApiParameterFilterInterface;
1819
use ApiPlatform\Metadata\Parameter;
1920
use ApiPlatform\Metadata\QueryParameter;
2021
use ApiPlatform\OpenApi\Model\Parameter as OpenApiParameter;
2122
use Illuminate\Database\Eloquent\Builder;
2223
use Illuminate\Database\Eloquent\Model;
24+
use Psr\Log\LoggerInterface;
2325

2426
final class RangeFilter implements FilterInterface, JsonSchemaFilterInterface, OpenApiParameterFilterInterface
2527
{
@@ -32,12 +34,25 @@ final class RangeFilter implements FilterInterface, JsonSchemaFilterInterface, O
3234
'gte' => '>=',
3335
];
3436

37+
public function __construct(private readonly ?LoggerInterface $logger = null)
38+
{
39+
}
40+
3541
/**
3642
* @param Builder<Model> $builder
3743
* @param array<string, mixed> $context
3844
*/
3945
public function apply(Builder $builder, mixed $values, Parameter $parameter, array $context = []): Builder
4046
{
47+
$values = \is_array($values) ? array_intersect_key($values, self::OPERATOR_VALUE) : [];
48+
if (!$values) {
49+
$this->logger?->notice('Invalid filter ignored', [
50+
'exception' => new InvalidArgumentException(\sprintf('At least one valid operator ("%s") is required for "%s" parameter', implode('", "', array_keys(self::OPERATOR_VALUE)), $parameter->getKey())),
51+
]);
52+
53+
return $builder;
54+
}
55+
4156
$queryProperty = $this->getQueryProperty($parameter);
4257

4358
foreach ($values as $key => $value) {

‎src/Laravel/Tests/EloquentTest.php‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use ApiPlatform\Laravel\Test\ApiTestAssertionsTrait;
1717
use ApiPlatform\Laravel\workbench\app\Enums\BookStatus;
1818
use Illuminate\Foundation\Testing\RefreshDatabase;
19+
use Illuminate\Support\Facades\Log;
1920
use Illuminate\Support\Str;
2021
use Orchestra\Testbench\Concerns\WithWorkbench;
2122
use Orchestra\Testbench\TestCase;
@@ -427,6 +428,21 @@ public function testRangeGreaterThanEqualFilter(): void
427428
$this->assertSame($json['totalItems'], 2);
428429
}
429430

431+
public function testRangeFilterWithInvalidValue(): void
432+
{
433+
BookFactory::new()->has(AuthorFactory::new())->count(10)->create();
434+
$log = Log::spy();
435+
436+
$res = $this->get('/api/books?isbn_range=5', ['Accept' => ['application/ld+json']]);
437+
$res->assertOk();
438+
$this->assertSame(10, $res->json()['totalItems']);
439+
440+
$res = $this->get('/api/books?isbn_range[unknown]=5', ['Accept' => ['application/ld+json']]);
441+
$res->assertOk();
442+
$this->assertSame(10, $res->json()['totalItems']);
443+
$log->shouldHaveReceived('notice')->twice();
444+
}
445+
430446
public function testWrongOrderFilter(): void
431447
{
432448
BookFactory::new()->has(AuthorFactory::new())->count(10)->create();

0 commit comments

Comments
 (0)