From 20dae22dd60f3ca20ef55b3aac9fc5633df2bdce Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Wed, 2 Dec 2026 11:53:23 +1300 Subject: [PATCH] fix(documents): set the collection on found rows before decoding them find() decoded each row before it set $collection, so a decode filter that asked $document->getCollection() got an empty string on every list read but the right id on getDocument(). Appwrite's subQueryVariables filter keys on it to pick function or site variables, and fell back to both, so listing sites returned the variables of a function with the same $sequence. Co-Authored-By: Claude Opus 5.5 --- src/Database/Trait/Documents.php | 7 +-- tests/unit/FilterCollectionTest.php | 92 +++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+), 4 deletions(-) create mode 100644 tests/unit/FilterCollectionTest.php diff --git a/src/Database/Trait/Documents.php b/src/Database/Trait/Documents.php index c7380496d..0e689d6c5 100644 --- a/src/Database/Trait/Documents.php +++ b/src/Database/Trait/Documents.php @@ -4444,6 +4444,9 @@ private function fetch(Collection $collection, array $queries, PermissionType $f foreach ($this->castAfter($collection, $results) as $index => $node) { $node = $this->casting($collection, $node); + if (! $node->isEmpty()) { + $node->setAttribute(Document::COLLECTION, $collectionId); + } $node = $this->decode($collection, $node, $selections); if ($joinedCollections !== []) { $node = $this->decodeJoins($node, $joinedCollections); @@ -4456,10 +4459,6 @@ private function fetch(Collection $collection, array $queries, PermissionType $f $node = $this->newDocument($collectionId, $node->getArrayCopy()); } - if (! $node->isEmpty()) { - $node->setAttribute(Document::COLLECTION, $collectionId); - } - $results[$index] = $node; } diff --git a/tests/unit/FilterCollectionTest.php b/tests/unit/FilterCollectionTest.php new file mode 100644 index 000000000..409c71de3 --- /dev/null +++ b/tests/unit/FilterCollectionTest.php @@ -0,0 +1,92 @@ + + */ + private static array $seen = []; + + /** + * @return array + */ + public static function adapters(): array + { + return [ + 'memory' => [static fn (): Adapter => new Memory()], + 'sqlite' => [static fn (): Adapter => new SQLite(new PDO('sqlite::memory:'))], + ]; + } + + /** + * @param \Closure(): Adapter $adapter + */ + #[DataProvider('adapters')] + public function testDecodeFilterSeesTheCollectionOnEveryRead(\Closure $adapter): void + { + $database = $this->database($adapter()); + $database->createDocument(self::COLLECTION, new Document([Document::ID => 'one', 'name' => 'one', 'derived' => ''])); + + self::$seen = []; + $database->getDocument(self::COLLECTION, 'one'); + $this->assertSame([self::COLLECTION], self::$seen, 'getDocument() decoded without the collection'); + + self::$seen = []; + $found = $database->find(self::COLLECTION); + $this->assertSame([self::COLLECTION], self::$seen, 'find() decoded without the collection'); + $this->assertSame(self::COLLECTION, $found[0]->getCollection()); + + self::$seen = []; + $selected = $database->find(self::COLLECTION, [Query::select(['name'])]); + $this->assertSame(self::COLLECTION, $selected[0]->getCollection(), 'find() with a selection lost the collection'); + } + + private function database(Adapter $adapter): Database + { + Database::addFilter( + self::FILTER, + static fn (mixed $value): mixed => $value, + static function (mixed $value, Document $document): mixed { + self::$seen[] = $document->getCollection(); + + return $value; + }, + ); + + $database = new Database($adapter, new Cache(new None())); + $database->setDatabase('filters')->setNamespace('collection_'.\uniqid()); + $database->create(); + $database->createCollection(Collection::create( + id: self::COLLECTION, + attributes: [ + Attribute::string(key: 'name', size: 64), + Attribute::string(key: 'derived', size: 64, filters: [self::FILTER]), + ], + permissions: [Permission::create(Role::any()), Permission::read(Role::any())], + )); + + return $database; + } +}