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
2 changes: 1 addition & 1 deletion src/Laravel/Eloquent/Filter/BooleanFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function apply(Builder $builder, mixed $values, Parameter $parameter, arr
return $builder;
}

return $builder->{$context['whereClause'] ?? 'where'}($this->getQueryProperty($parameter), $values);
return $builder->{$context['whereClause'] ?? 'where'}($this->getQueryProperty($parameter), self::BOOLEAN_VALUES[$values]);
}

public function getSchema(Parameter $parameter): array
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

final class ParameterValidationResourceMetadataCollectionFactory implements ResourceMetadataCollectionFactoryInterface
{
private const BOOLEAN_VALUES = ['true', 'false', '1', '0'];

public function __construct(
private readonly ?ResourceMetadataCollectionFactoryInterface $decorated = null,
private readonly ?ContainerInterface $filterLocator = null,
Expand Down Expand Up @@ -145,7 +147,7 @@ private function addSchemaValidation(Parameter $parameter): Parameter
}

if (isset($schema['type']) && 'boolean' === $schema['type']) {
$assertions[] = 'boolean';
$assertions[] = Rule::in(self::BOOLEAN_VALUES);
}

if (!$assertions) {
Expand Down
10 changes: 9 additions & 1 deletion src/Laravel/Tests/EloquentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -433,10 +433,18 @@ public function testWithAccessor(): void

public function testBooleanFilter(): void
{
BookFactory::new()->has(AuthorFactory::new())->count(10)->create();
$books = BookFactory::new()->has(AuthorFactory::new())->count(10)->create();
$res = $this->get('/api/books?published=notabool', ['Accept' => ['application/ld+json']]);
$this->assertEquals($res->getStatusCode(), 422);

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

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

$res = $this->get('/api/books?published=0', ['Accept' => ['application/ld+json']]);
$this->assertEquals($res->getStatusCode(), 200);
$this->assertEquals($res->json()['totalItems'], 0);
Expand Down
Loading