Skip to content

Commit 29f4cfe

Browse files
updated the semantics for not equal case
1 parent fad8570 commit 29f4cfe

3 files changed

Lines changed: 25 additions & 12 deletions

File tree

src/Database/Query.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,11 @@ public static function equal(string $attribute, array $values): self
463463
*/
464464
public static function notEqual(string $attribute, string|int|float|bool|array $value): self
465465
{
466-
return new self(self::TYPE_NOT_EQUAL, $attribute, is_array($value) ? $value : [$value]);
466+
// maps or not an array
467+
if ((is_array($value) && !array_is_list($value)) || !is_array($value)) {
468+
$value = [$value];
469+
}
470+
return new self(self::TYPE_NOT_EQUAL, $attribute, $value);
467471
}
468472

469473
/**

src/Database/Validator/Query/Filter.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -163,12 +163,8 @@ protected function isValidAttributeAndValues(string $attribute, array $values, s
163163
break;
164164

165165
case Database::VAR_OBJECT:
166-
// For JSONB/object queries, value must be an array
167-
if (!is_array($value)) {
168-
$this->message = 'Query value for object type must be an array';
169-
return false;
170-
}
171-
// No further validation needed - JSONB accepts any valid array structure
166+
// value for object can be of any type as its a hashmap
167+
// eg; ['key'=>value']
172168
continue 2;
173169

174170
case Database::VAR_POINT:

tests/e2e/Adapter/Scopes/ObjectAttributeTests.php

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
namespace Tests\E2E\Adapter\Scopes;
44

5+
use Exception;
56
use Utopia\Database\Database;
67
use Utopia\Database\Document;
78
use Utopia\Database\Exception\Index as IndexException;
9+
use Utopia\Database\Exception\Query as QueryException;
810
use Utopia\Database\Exception\Structure as StructureException;
911
use Utopia\Database\Helpers\ID;
1012
use Utopia\Database\Helpers\Permission;
@@ -152,21 +154,31 @@ public function testObjectAttribute(): void
152154

153155
// Test 11d: notEqual on scalar inside object should exclude doc1
154156
$results = $database->find($collectionId, [
155-
Query::notEqual('meta', [['age' => 26]])
157+
Query::notEqual('meta', ['age' => 26])
156158
]);
157159
// Should return doc2 only
158160
$this->assertCount(1, $results);
159161
$this->assertEquals('doc2', $results[0]->getId());
160162

163+
try {
164+
// test -> not equal allows one value only
165+
$results = $database->find($collectionId, [
166+
Query::notEqual('meta', [['age' => 26],['age' => 27]])
167+
]);
168+
$this->fail('No query thrown');
169+
} catch (Exception $e) {
170+
$this->assertInstanceOf(QueryException::class, $e);
171+
}
172+
161173
// Test 11e: notEqual on nested object should exclude doc1
162174
$results = $database->find($collectionId, [
163-
Query::notEqual('meta', [[
175+
Query::notEqual('meta', [
164176
'user' => [
165177
'info' => [
166178
'country' => 'CA'
167179
]
168180
]
169-
]])
181+
])
170182
]);
171183
// Should return doc2 only
172184
$this->assertCount(1, $results);
@@ -588,9 +600,10 @@ public function testObjectAttributeGinIndex(): void
588600

589601
// Test 3: Query with equal on indexed JSONB column
590602
$results = $database->find($collectionId, [
591-
Query::equal('data', [['env' => 'production']])
603+
Query::equal('data', [['config' => ['env' => 'production']]])
592604
]);
593-
$this->assertCount(0, $results); // Note: Object index doesn't make equal queries work differently
605+
$this->assertCount(1, $results);
606+
$this->assertEquals('gin1', $results[0]->getId());
594607

595608
// Test 4: Query with contains on indexed JSONB column
596609
$results = $database->find($collectionId, [

0 commit comments

Comments
 (0)