From ba79dda78815529241a0d9efb36dd633e961ccf1 Mon Sep 17 00:00:00 2001 From: fogelito Date: Sun, 24 Aug 2025 17:39:29 +0300 Subject: [PATCH 1/3] Check no change --- .../Collections/Documents/Upsert.php | 13 ++++ .../Databases/Legacy/DatabasesBase.php | 64 +++++++++++++++++++ .../Databases/TablesDB/DatabasesBase.php | 29 +++++++++ 3 files changed, 106 insertions(+) diff --git a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Upsert.php b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Upsert.php index b5a2bf9b74..770925e89b 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Upsert.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Upsert.php @@ -97,6 +97,13 @@ class Upsert extends Action throw new Exception($this->getMissingPayloadException()); } +// if (!empty($data) && \array_is_list($data)) { +// throw new Exception('shmuel'); +// } + + var_dump(array_is_list($data)); + var_dump($data); + $isAPIKey = Auth::isAppUser(Authorization::getRoles()); $isPrivilegedUser = Auth::isPrivilegedUser(Authorization::getRoles()); @@ -265,7 +272,13 @@ class Upsert extends Action } $collectionsCache = []; + + if (empty($upserted[0])) { + $upserted[0] = $dbForProject->getDocument('database_' . $database->getSequence() . '_collection_' . $collection->getSequence(), $documentId); + } + $document = $upserted[0]; + $this->processDocument( database: $database, collection: $collection, diff --git a/tests/e2e/Services/Databases/Legacy/DatabasesBase.php b/tests/e2e/Services/Databases/Legacy/DatabasesBase.php index 702ce6854d..1953532973 100644 --- a/tests/e2e/Services/Databases/Legacy/DatabasesBase.php +++ b/tests/e2e/Services/Databases/Legacy/DatabasesBase.php @@ -1728,6 +1728,70 @@ trait DatabasesBase $this->assertEquals('Thor: Ragnarok', $document['body']['title']); + /** + * Resubmit same document, nothing to update + */ + $document = $this->client->call(Client::METHOD_PUT, '/databases/' . $databaseId . '/collections/' . $data['moviesId'] . '/documents/' . $documentId, array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), [ + 'data' => [ + 'title' => 'Thor: Ragnarok', + 'releaseYear' => 2000, + 'integers' => [], + 'birthDay' => null, + 'duration' => null, + 'starringActors' => [], + 'actors' => [], + 'tagline' => '', + 'description' => '', + ], + 'permissions' => [ + Permission::read(Role::users()), + Permission::update(Role::users()), + Permission::delete(Role::users()), + ], + ]); + + $document = $this->client->call(Client::METHOD_PUT, '/databases/' . $databaseId . '/collections/' . $data['moviesId'] . '/documents/' . $documentId, array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), [ + 'data' => [ + [ + 'title' => 'Thor: Ragnarok1', + 'releaseYear' => 2000, + 'integers' => [], + 'birthDay' => null, + 'duration' => null, + 'starringActors' => [], + 'actors' => [], + 'tagline' => '', + 'description' => '', + ], + [ + 'title' => 'Thor: Ragnarok2', + 'releaseYear' => 2000, + 'integers' => [], + 'birthDay' => null, + 'duration' => null, + 'starringActors' => [], + 'actors' => [], + 'tagline' => '', + 'description' => '', + ] + ], + 'permissions' => [ + Permission::read(Role::users()), + Permission::update(Role::users()), + Permission::delete(Role::users()), + ], + ]); +var_dump($document); + $this->assertEquals(200, $document['headers']['status-code']); + $this->assertEquals('Thor: Ragnarok', $document['body']['title']); + $this->assertCount(3, $document['body']['$permissions']); + $document = $this->client->call(Client::METHOD_PUT, '/databases/' . $databaseId . '/collections/' . $data['moviesId'] . '/documents/' . $documentId, array_merge([ 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], diff --git a/tests/e2e/Services/Databases/TablesDB/DatabasesBase.php b/tests/e2e/Services/Databases/TablesDB/DatabasesBase.php index e36543fd55..ed43894ab9 100644 --- a/tests/e2e/Services/Databases/TablesDB/DatabasesBase.php +++ b/tests/e2e/Services/Databases/TablesDB/DatabasesBase.php @@ -1693,6 +1693,35 @@ trait DatabasesBase $this->assertEquals('Thor: Ragnarok', $row['body']['title']); + /** + * Resubmit same document, nothing to update + */ + $row = $this->client->call(Client::METHOD_PUT, '/tablesdb/' . $databaseId . '/tables/' . $data['moviesId'] . '/rows/' . $rowId, array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), [ + 'data' => [ + 'title' => 'Thor: Ragnarok', + 'releaseYear' => 2000, + 'integers' => [], + 'birthDay' => null, + 'duration' => null, + 'starringActors' => [], + 'actors' => [], + 'tagline' => '', + 'description' => '', + ], + 'permissions' => [ + Permission::read(Role::users()), + Permission::update(Role::users()), + Permission::delete(Role::users()), + ], + ]); + + $this->assertEquals(200, $row['headers']['status-code']); + $this->assertEquals('Thor: Ragnarok', $row['body']['title']); + $this->assertCount(3, $row['body']['$permissions']); + $row = $this->client->call(Client::METHOD_PUT, '/tablesdb/' . $databaseId . '/tables/' . $data['moviesId'] . '/rows/' . $rowId, array_merge([ 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], From 959db2ea0240a2e4389ebddc1c2c75de3f5feb0b Mon Sep 17 00:00:00 2001 From: fogelito Date: Sun, 24 Aug 2025 18:08:12 +0300 Subject: [PATCH 2/3] Restrict array list --- .../Collections/Documents/Upsert.php | 9 ++--- .../Databases/Legacy/DatabasesBase.php | 39 +------------------ .../Databases/TablesDB/DatabasesBase.php | 21 ++++++++++ 3 files changed, 26 insertions(+), 43 deletions(-) diff --git a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Upsert.php b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Upsert.php index 770925e89b..8427ee6cd2 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Upsert.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Upsert.php @@ -97,12 +97,9 @@ class Upsert extends Action throw new Exception($this->getMissingPayloadException()); } -// if (!empty($data) && \array_is_list($data)) { -// throw new Exception('shmuel'); -// } - - var_dump(array_is_list($data)); - var_dump($data); + if (\array_is_list($data) && \count($data) > 1) { // Allow 1 associated array + throw new Exception($this->getMissingPayloadException()); + } $isAPIKey = Auth::isAppUser(Authorization::getRoles()); $isPrivilegedUser = Auth::isPrivilegedUser(Authorization::getRoles()); diff --git a/tests/e2e/Services/Databases/Legacy/DatabasesBase.php b/tests/e2e/Services/Databases/Legacy/DatabasesBase.php index 1953532973..672d39bdeb 100644 --- a/tests/e2e/Services/Databases/Legacy/DatabasesBase.php +++ b/tests/e2e/Services/Databases/Legacy/DatabasesBase.php @@ -1735,7 +1735,7 @@ trait DatabasesBase 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], ], $this->getHeaders()), [ - 'data' => [ + 'data' => [[ 'title' => 'Thor: Ragnarok', 'releaseYear' => 2000, 'integers' => [], @@ -1745,7 +1745,7 @@ trait DatabasesBase 'actors' => [], 'tagline' => '', 'description' => '', - ], + ]], 'permissions' => [ Permission::read(Role::users()), Permission::update(Role::users()), @@ -1753,41 +1753,6 @@ trait DatabasesBase ], ]); - $document = $this->client->call(Client::METHOD_PUT, '/databases/' . $databaseId . '/collections/' . $data['moviesId'] . '/documents/' . $documentId, array_merge([ - 'content-type' => 'application/json', - 'x-appwrite-project' => $this->getProject()['$id'], - ], $this->getHeaders()), [ - 'data' => [ - [ - 'title' => 'Thor: Ragnarok1', - 'releaseYear' => 2000, - 'integers' => [], - 'birthDay' => null, - 'duration' => null, - 'starringActors' => [], - 'actors' => [], - 'tagline' => '', - 'description' => '', - ], - [ - 'title' => 'Thor: Ragnarok2', - 'releaseYear' => 2000, - 'integers' => [], - 'birthDay' => null, - 'duration' => null, - 'starringActors' => [], - 'actors' => [], - 'tagline' => '', - 'description' => '', - ] - ], - 'permissions' => [ - Permission::read(Role::users()), - Permission::update(Role::users()), - Permission::delete(Role::users()), - ], - ]); -var_dump($document); $this->assertEquals(200, $document['headers']['status-code']); $this->assertEquals('Thor: Ragnarok', $document['body']['title']); $this->assertCount(3, $document['body']['$permissions']); diff --git a/tests/e2e/Services/Databases/TablesDB/DatabasesBase.php b/tests/e2e/Services/Databases/TablesDB/DatabasesBase.php index ed43894ab9..0b92208c95 100644 --- a/tests/e2e/Services/Databases/TablesDB/DatabasesBase.php +++ b/tests/e2e/Services/Databases/TablesDB/DatabasesBase.php @@ -1722,6 +1722,27 @@ trait DatabasesBase $this->assertEquals('Thor: Ragnarok', $row['body']['title']); $this->assertCount(3, $row['body']['$permissions']); + /** + * Do not allow array list + */ + $row = $this->client->call(Client::METHOD_PUT, '/tablesdb/' . $databaseId . '/tables/' . $data['moviesId'] . '/rows/' . $rowId, array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), [ + 'data' => [ + [ + 'title' => 'Thor: Ragnarok 1', + ], + [ + 'title' => 'Thor: Ragnarok 2', + ] + ], + 'permissions' => [ + Permission::read(Role::users()), + ], + ]); + $this->assertEquals(400, $row['headers']['status-code']); + $row = $this->client->call(Client::METHOD_PUT, '/tablesdb/' . $databaseId . '/tables/' . $data['moviesId'] . '/rows/' . $rowId, array_merge([ 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], From ad96379623c2fed34536e458ef2fd81f954cd9cb Mon Sep 17 00:00:00 2001 From: fogelito Date: Sun, 24 Aug 2025 18:09:38 +0300 Subject: [PATCH 3/3] Restrict array list --- tests/e2e/Services/Databases/Legacy/DatabasesBase.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/e2e/Services/Databases/Legacy/DatabasesBase.php b/tests/e2e/Services/Databases/Legacy/DatabasesBase.php index 672d39bdeb..5fd8066584 100644 --- a/tests/e2e/Services/Databases/Legacy/DatabasesBase.php +++ b/tests/e2e/Services/Databases/Legacy/DatabasesBase.php @@ -1735,7 +1735,7 @@ trait DatabasesBase 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], ], $this->getHeaders()), [ - 'data' => [[ + 'data' => [ 'title' => 'Thor: Ragnarok', 'releaseYear' => 2000, 'integers' => [], @@ -1745,7 +1745,7 @@ trait DatabasesBase 'actors' => [], 'tagline' => '', 'description' => '', - ]], + ], 'permissions' => [ Permission::read(Role::users()), Permission::update(Role::users()),