From 42d6a7b00dc79685afc49b67fe32029beb71472c Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Tue, 19 Aug 2025 20:08:13 +1200 Subject: [PATCH] Fix hardcoded collectionId --- .../Collections/Documents/Action.php | 3 +- .../Databases/Legacy/DatabasesBase.php | 49 +++++++++++++++++++ .../Databases/TablesDB/DatabasesBase.php | 49 +++++++++++++++++++ 3 files changed, 99 insertions(+), 2 deletions(-) diff --git a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Action.php b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Action.php index 4bbe71ac9f..fa5326c859 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Action.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Action.php @@ -29,7 +29,6 @@ abstract class Action extends AppwriteAction } else { $this->removableAttributes = ['$databaseId', '$collectionId']; } - return parent::setHttpPath($path); } @@ -232,7 +231,7 @@ abstract class Action extends AppwriteAction $collectionId = $collection->getId(); $document->removeAttribute('$collection'); $document->setAttribute('$databaseId', $database->getId()); - $document->setAttribute('$collectionId', $collectionId); + $document->setAttribute('$' . $this->getCollectionsEventsContext() . 'Id', $collectionId); $relationships = $collectionsCache[$collectionId] ??= \array_filter( $collection->getAttribute('attributes', []), diff --git a/tests/e2e/Services/Databases/Legacy/DatabasesBase.php b/tests/e2e/Services/Databases/Legacy/DatabasesBase.php index 718d69197e..8b415127ba 100644 --- a/tests/e2e/Services/Databases/Legacy/DatabasesBase.php +++ b/tests/e2e/Services/Databases/Legacy/DatabasesBase.php @@ -5245,6 +5245,55 @@ trait DatabasesBase $this->assertArrayNotHasKey('libraries', $response['body']); } + /** + * @depends testOneToManyRelationship + */ + public function testCollectionIdAttributeConsistency(array $data): void + { + // Test that when NO select query is used, $collectionId is present (not $tableId) + $response = $this->client->call(Client::METHOD_GET, '/databases/' . $data['databaseId'] . '/collections/' . $data['personCollection'] . '/documents', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), [ + 'queries' => [ + Query::equal('fullName', ['Stevie Wonder'])->toString(), + ], + ]); + + $this->assertEquals(200, $response['headers']['status-code']); + $this->assertCount(1, $response['body']['documents']); + + $document = $response['body']['documents'][0]; + // Verify correct Collections API attributes are present + $this->assertArrayHasKey('$collectionId', $document, 'Collections API should set $collectionId, not $tableId'); + $this->assertArrayHasKey('$databaseId', $document); + $this->assertArrayNotHasKey('$tableId', $document, 'Collections API should not have $tableId'); + + // Test that when $collectionId is explicitly requested in select, it's present + $response = $this->client->call(Client::METHOD_GET, '/databases/' . $data['databaseId'] . '/collections/' . $data['personCollection'] . '/documents', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), [ + 'queries' => [ + Query::equal('fullName', ['Stevie Wonder'])->toString(), + Query::select(['fullName', '$collectionId', '$databaseId'])->toString(), + ], + ]); + + $this->assertEquals(200, $response['headers']['status-code']); + $this->assertCount(1, $response['body']['documents']); + + $document = $response['body']['documents'][0]; + $this->assertArrayHasKey('$collectionId', $document, 'Explicitly requested $collectionId should be present'); + $this->assertArrayHasKey('$databaseId', $document, 'Explicitly requested $databaseId should be present'); + $this->assertArrayHasKey('fullName', $document); + $this->assertArrayNotHasKey('$tableId', $document, 'Collections API should never have $tableId'); + + // Verify the correct value is set + $this->assertEquals($data['personCollection'], $document['$collectionId'], '$collectionId should contain the collection ID'); + $this->assertEquals($data['databaseId'], $document['$databaseId'], '$databaseId should contain the database ID'); + } + /** * @throws \Utopia\Database\Exception * @throws \Utopia\Database\Exception\Query diff --git a/tests/e2e/Services/Databases/TablesDB/DatabasesBase.php b/tests/e2e/Services/Databases/TablesDB/DatabasesBase.php index 8cd9e30708..b1b49d7aa9 100644 --- a/tests/e2e/Services/Databases/TablesDB/DatabasesBase.php +++ b/tests/e2e/Services/Databases/TablesDB/DatabasesBase.php @@ -5233,6 +5233,55 @@ trait DatabasesBase $this->assertArrayNotHasKey('libraries', $response['body']); } + /** + * @depends testOneToManyRelationship + */ + public function testTableIdAttributeConsistency(array $data): void + { + // Test that when NO select query is used, $tableId is present (not $collectionId) + $response = $this->client->call(Client::METHOD_GET, '/tablesdb/' . $data['databaseId'] . '/tables/' . $data['personCollection'] . '/rows', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), [ + 'queries' => [ + Query::equal('fullName', ['Stevie Wonder'])->toString(), + ], + ]); + + $this->assertEquals(200, $response['headers']['status-code']); + $this->assertCount(1, $response['body']['rows']); + + $row = $response['body']['rows'][0]; + // Verify correct TablesDB attributes are present + $this->assertArrayHasKey('$tableId', $row, 'TablesDB API should set $tableId, not $collectionId'); + $this->assertArrayHasKey('$databaseId', $row); + $this->assertArrayNotHasKey('$collectionId', $row, 'TablesDB API should not have $collectionId'); + + // Test that when $tableId is explicitly requested in select, it's present + $response = $this->client->call(Client::METHOD_GET, '/tablesdb/' . $data['databaseId'] . '/tables/' . $data['personCollection'] . '/rows', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), [ + 'queries' => [ + Query::equal('fullName', ['Stevie Wonder'])->toString(), + Query::select(['fullName', '$tableId', '$databaseId'])->toString(), + ], + ]); + + $this->assertEquals(200, $response['headers']['status-code']); + $this->assertCount(1, $response['body']['rows']); + + $row = $response['body']['rows'][0]; + $this->assertArrayHasKey('$tableId', $row, 'Explicitly requested $tableId should be present'); + $this->assertArrayHasKey('$databaseId', $row, 'Explicitly requested $databaseId should be present'); + $this->assertArrayHasKey('fullName', $row); + $this->assertArrayNotHasKey('$collectionId', $row, 'TablesDB should never have $collectionId'); + + // Verify the correct value is set + $this->assertEquals($data['personCollection'], $row['$tableId'], '$tableId should contain the table ID'); + $this->assertEquals($data['databaseId'], $row['$databaseId'], '$databaseId should contain the database ID'); + } + /** * @throws \Utopia\Database\Exception * @throws \Utopia\Database\Exception\Query