Skip to content
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
15 changes: 15 additions & 0 deletions src/Laravel/Eloquent/Filter/RangeFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@

namespace ApiPlatform\Laravel\Eloquent\Filter;

use ApiPlatform\Metadata\Exception\InvalidArgumentException;
use ApiPlatform\Metadata\JsonSchemaFilterInterface;
use ApiPlatform\Metadata\OpenApiParameterFilterInterface;
use ApiPlatform\Metadata\Parameter;
use ApiPlatform\Metadata\QueryParameter;
use ApiPlatform\OpenApi\Model\Parameter as OpenApiParameter;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Psr\Log\LoggerInterface;

final class RangeFilter implements FilterInterface, JsonSchemaFilterInterface, OpenApiParameterFilterInterface
{
Expand All @@ -32,12 +34,25 @@ final class RangeFilter implements FilterInterface, JsonSchemaFilterInterface, O
'gte' => '>=',
];

public function __construct(private readonly ?LoggerInterface $logger = null)
{
}

/**
* @param Builder<Model> $builder
* @param array<string, mixed> $context
*/
public function apply(Builder $builder, mixed $values, Parameter $parameter, array $context = []): Builder
{
$values = \is_array($values) ? array_intersect_key($values, self::OPERATOR_VALUE) : [];
if (!$values) {
$this->logger?->notice('Invalid filter ignored', [
'exception' => new InvalidArgumentException(\sprintf('At least one valid operator ("%s") is required for "%s" parameter', implode('", "', array_keys(self::OPERATOR_VALUE)), $parameter->getKey())),
]);

return $builder;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we add a log like we do in other filters so that a user can debug this ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cheers @soyuka, done. I'd only compared it with the other Laravel filters, and none of those log, so I never looked at how the Doctrine range filter handles it.

}

$queryProperty = $this->getQueryProperty($parameter);

foreach ($values as $key => $value) {
Expand Down
16 changes: 16 additions & 0 deletions src/Laravel/Tests/EloquentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use ApiPlatform\Laravel\Test\ApiTestAssertionsTrait;
use ApiPlatform\Laravel\workbench\app\Enums\BookStatus;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str;
use Orchestra\Testbench\Concerns\WithWorkbench;
use Orchestra\Testbench\TestCase;
Expand Down Expand Up @@ -427,6 +428,21 @@ public function testRangeGreaterThanEqualFilter(): void
$this->assertSame($json['totalItems'], 2);
}

public function testRangeFilterWithInvalidValue(): void
{
BookFactory::new()->has(AuthorFactory::new())->count(10)->create();
$log = Log::spy();

$res = $this->get('/api/books?isbn_range=5', ['Accept' => ['application/ld+json']]);
$res->assertOk();
$this->assertSame(10, $res->json()['totalItems']);

$res = $this->get('/api/books?isbn_range[unknown]=5', ['Accept' => ['application/ld+json']]);
$res->assertOk();
$this->assertSame(10, $res->json()['totalItems']);
$log->shouldHaveReceived('notice')->twice();
}

public function testWrongOrderFilter(): void
{
BookFactory::new()->has(AuthorFactory::new())->count(10)->create();
Expand Down
Loading