Skip to content

Commit

Permalink
Catch possible null value error in ModelFilter
Browse files Browse the repository at this point in the history
  • Loading branch information
core23 authored and VincentLanglet committed Apr 10, 2024
1 parent 011b83f commit 447231d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/Filter/ModelFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,9 @@ protected function handleMultiple(ProxyQueryInterface $query, string $alias, Fil
);
}

$this->applyWhere($query, $inExpression);
if ($inExpression->count() > 0) {
$this->applyWhere($query, $inExpression);
}
}

protected function association(ProxyQueryInterface $query, FilterData $data): array
Expand Down Expand Up @@ -147,6 +149,10 @@ private function buildInExpression(ProxyQueryInterface $query, string $alias, Fi
$orX = $queryBuilder->expr()->orX();

foreach ($data->getValue() as $value) {
if (!\is_object($value)) {
continue;
}

$andX = $queryBuilder->expr()->andX();

foreach ($metadata->getIdentifierValues($value) as $fieldName => $identifierValue) {
Expand Down
19 changes: 19 additions & 0 deletions tests/Filter/ModelFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,25 @@ public function testFilterArray(): void
static::assertTrue($filter->isActive());
}

public function testFilterNonObject(): void
{
$filter = new ModelFilter();
$filter->initialize('field_name', ['field_options' => ['class' => 'FooBar']]);

$proxyQuery = new ProxyQuery($this->createQueryBuilderStub());

$objects = [new \stdClass(), new \stdClass()];
$filter->filter($proxyQuery, 'alias', 'field', FilterData::fromArray([
'type' => EqualOperatorType::TYPE_EQUAL,
'value' => null,
]));

// the alias is now computer by the entityJoin method
self::assertSameQuery([], $proxyQuery);
self::assertSameQueryParameters([], $proxyQuery);
static::assertFalse($filter->isActive());
}

public function testFilterArrayTypeIsNotEqual(): void
{
$filter = new ModelFilter();
Expand Down

0 comments on commit 447231d

Please sign in to comment.