From e06e890279eae33ee2a44b8ae16238481f275f74 Mon Sep 17 00:00:00 2001 From: fogelito Date: Tue, 21 Mar 2023 14:32:05 +0200 Subject: [PATCH] query validators ignore nested --- app/controllers/api/databases.php | 7 ++++- .../Utopia/Database/Validator/Queries.php | 5 ++- .../e2e/Services/Databases/DatabasesBase.php | 31 +++++++++++++------ 3 files changed, 31 insertions(+), 12 deletions(-) diff --git a/app/controllers/api/databases.php b/app/controllers/api/databases.php index ee5e69bc76..005fba3b70 100644 --- a/app/controllers/api/databases.php +++ b/app/controllers/api/databases.php @@ -2680,7 +2680,12 @@ App::get('/v1/databases/:databaseId/collections/:collectionId/documents') } $filterQueries = Query::groupByType($queries)['filters']; - + // todo: temporary fix until Utopia will be ready !!!! + foreach ($filterQueries as $key => $query) { + if (\str_contains($query->getAttribute(), '.')) { + unset($filterQueries[$key]); + } + } if ($documentSecurity && !$valid) { $documents = $dbForProject->find('database_' . $database->getInternalId() . '_collection_' . $collection->getInternalId(), $queries); $total = $dbForProject->count('database_' . $database->getInternalId() . '_collection_' . $collection->getInternalId(), $filterQueries, APP_LIMIT_COUNT); diff --git a/src/Appwrite/Utopia/Database/Validator/Queries.php b/src/Appwrite/Utopia/Database/Validator/Queries.php index 1e9fe8f208..2629c08ce5 100644 --- a/src/Appwrite/Utopia/Database/Validator/Queries.php +++ b/src/Appwrite/Utopia/Database/Validator/Queries.php @@ -57,8 +57,11 @@ class Queries extends Validator if (!$query instanceof Query) { try { $query = Query::parse($query); + if (\str_contains($query->getAttribute(), '.')) { + return true; + } } catch (\Throwable $th) { - $this->message = 'Invalid query: ${query}'; + $this->message = "Invalid query: {$query}"; return false; } } diff --git a/tests/e2e/Services/Databases/DatabasesBase.php b/tests/e2e/Services/Databases/DatabasesBase.php index 75e7f375be..fe2023a6fa 100644 --- a/tests/e2e/Services/Databases/DatabasesBase.php +++ b/tests/e2e/Services/Databases/DatabasesBase.php @@ -321,7 +321,6 @@ trait DatabasesBase $this->assertEquals(false, $attribute['body']['twoWay']); $this->assertEquals('cascade', $attribute['body']['onUpdate']); $this->assertEquals('restrict', $attribute['body']['onDelete']); - //$this->assertEquals('libraryId', $attribute['body']['id']); // $person1 = $this->client->call(Client::METHOD_POST, '/databases/' . $databaseId . '/collections/' . $person['body']['$id'] . '/documents', array_merge([ 'content-type' => 'application/json', @@ -350,20 +349,24 @@ trait DatabasesBase 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], ], $this->getHeaders()), [ - 'queries' => ['equal("libraryId", "library1")'], + 'queries' => [ + 'equal("libraryId", "library1")', + //'select("libraryId")' + ], ]); $this->assertEquals(1, $documents['body']['total']); $this->assertEquals('Library 1', $documents['body']['documents'][0]['libraryId']['libraryName']); -// $documents = $this->client->call(Client::METHOD_GET, '/databases/' . $databaseId . '/collections/' . $person['body']['$id'] . '/documents', array_merge([ -// 'content-type' => 'application/json', -// 'x-appwrite-project' => $this->getProject()['$id'], -// ], $this->getHeaders()), [ -// 'queries' => ['equal("library.libraryName", "Library 1")'], -// ]); -// -// var_dump($documents); + $documents = $this->client->call(Client::METHOD_GET, '/databases/' . $databaseId . '/collections/' . $person['body']['$id'] . '/documents', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), [ + 'queries' => ['equal("library.libraryNameNotFound", "Library 1")'], + ]); + + $this->assertEquals(1, $documents['body']['total']); // we skip count for nested + $this->assertEquals('Library 1', $documents['body']['documents'][0]['libraryId']['libraryName']); $response = $this->client->call(Client::METHOD_DELETE, '/databases/' . $databaseId . '/collections/' . $person['body']['$id'] . '/attributes/libraryId', array_merge([ 'content-type' => 'application/json', @@ -371,7 +374,15 @@ trait DatabasesBase 'x-appwrite-key' => $this->getProject()['apiKey'] ])); + sleep(1); + $this->assertEquals(204, $response['headers']['status-code']); + $attribute = $this->client->call(Client::METHOD_GET, "/databases/{$databaseId}/collections/{$person['body']['$id']}/attributes/libraryId", array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'] + ])); + $this->assertEquals(404, $attribute['headers']['status-code']); die; return [];