From 627cbe285f046e8c705d9696b069a7dcdf11fc69 Mon Sep 17 00:00:00 2001 From: Bradley Schofield Date: Fri, 30 Aug 2024 19:18:45 +0900 Subject: [PATCH] Address Comments --- app/controllers/api/databases.php | 73 +++++++++---------- composer.lock | 12 +-- .../Databases/DatabasesCustomServerTest.php | 21 ++++++ 3 files changed, 60 insertions(+), 46 deletions(-) diff --git a/app/controllers/api/databases.php b/app/controllers/api/databases.php index d83a542c5a..ced96868b2 100644 --- a/app/controllers/api/databases.php +++ b/app/controllers/api/databases.php @@ -27,6 +27,7 @@ use Utopia\Database\Exception\Duplicate as DuplicateException; use Utopia\Database\Exception\Limit as LimitException; use Utopia\Database\Exception\Restricted as RestrictedException; use Utopia\Database\Exception\Structure as StructureException; +use Utopia\Database\Exception\Truncate as TruncateException; use Utopia\Database\Helpers\ID; use Utopia\Database\Helpers\Permission; use Utopia\Database\Helpers\Role; @@ -364,14 +365,18 @@ function updateAttribute( $dbForProject->purgeCachedDocument('database_' . $db->getInternalId(), $relatedCollection->getId()); } } else { - $dbForProject->updateAttribute( - collection: $collectionId, - id: $key, - required: $required, - default: $default, - formatOptions: $options ?? null, - size: $size ?? null, - ); + try { + $dbForProject->updateAttribute( + collection: $collectionId, + id: $key, + required: $required, + default: $default, + formatOptions: $options ?? null, + size: $size ?? null, + ); + } catch (TruncateException $e) { + throw new Exception(Exception::ATTRIBUTE_INVALID_RESIZE); + } } $attribute = $dbForProject->updateDocument('attributes', $db->getInternalId() . '_' . $collection->getInternalId() . '_' . $key, $attribute); @@ -1149,19 +1154,15 @@ App::post('/v1/databases/:databaseId/collections/:collectionId/attributes/string $filters[] = 'encrypt'; } - try { - $attribute = createAttribute($databaseId, $collectionId, new Document([ - 'key' => $key, - 'type' => Database::VAR_STRING, - 'size' => $size, - 'required' => $required, - 'default' => $default, - 'array' => $array, - 'filters' => $filters, - ]), $response, $dbForProject, $queueForDatabase, $queueForEvents); - } catch (DatabaseException $e) { - var_dump($e); - } + $attribute = createAttribute($databaseId, $collectionId, new Document([ + 'key' => $key, + 'type' => Database::VAR_STRING, + 'size' => $size, + 'required' => $required, + 'default' => $default, + 'array' => $array, + 'filters' => $filters, + ]), $response, $dbForProject, $queueForDatabase, $queueForEvents); $response @@ -1877,25 +1878,17 @@ App::patch('/v1/databases/:databaseId/collections/:collectionId/attributes/strin ->inject('queueForEvents') ->action(function (string $databaseId, string $collectionId, string $key, ?bool $required, ?string $default, ?int $size, Response $response, Database $dbForProject, Event $queueForEvents) { - try { - $attribute = updateAttribute( - databaseId: $databaseId, - collectionId: $collectionId, - key: $key, - dbForProject: $dbForProject, - queueForEvents: $queueForEvents, - type: Database::VAR_STRING, - default: $default, - required: $required, - size: $size - ); - } catch (DatabaseException $e) { - if ($e->getMessage() === "Resize would result in data truncation") { - throw new Exception(Exception::ATTRIBUTE_INVALID_RESIZE); - } else { - throw $e; - } - } + $attribute = updateAttribute( + databaseId: $databaseId, + collectionId: $collectionId, + key: $key, + dbForProject: $dbForProject, + queueForEvents: $queueForEvents, + type: Database::VAR_STRING, + default: $default, + required: $required, + size: $size + ); $response ->setStatusCode(Response::STATUS_CODE_OK) diff --git a/composer.lock b/composer.lock index 66076e71c9..962ee5a478 100644 --- a/composer.lock +++ b/composer.lock @@ -1723,16 +1723,16 @@ }, { "name": "utopia-php/database", - "version": "0.53.1", + "version": "0.53.2", "source": { "type": "git", "url": "https://github.com/utopia-php/database.git", - "reference": "3f17072a33649a3451122bfeb630d57330c0a6df" + "reference": "87fa42f15e9ff43bcb6523f5f8be4b53ad4bbb78" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/database/zipball/3f17072a33649a3451122bfeb630d57330c0a6df", - "reference": "3f17072a33649a3451122bfeb630d57330c0a6df", + "url": "https://api.github.com/repos/utopia-php/database/zipball/87fa42f15e9ff43bcb6523f5f8be4b53ad4bbb78", + "reference": "87fa42f15e9ff43bcb6523f5f8be4b53ad4bbb78", "shasum": "" }, "require": { @@ -1773,9 +1773,9 @@ ], "support": { "issues": "https://github.com/utopia-php/database/issues", - "source": "https://github.com/utopia-php/database/tree/0.53.1" + "source": "https://github.com/utopia-php/database/tree/0.53.2" }, - "time": "2024-08-30T09:02:45+00:00" + "time": "2024-08-30T10:14:51+00:00" }, { "name": "utopia-php/domains", diff --git a/tests/e2e/Services/Databases/DatabasesCustomServerTest.php b/tests/e2e/Services/Databases/DatabasesCustomServerTest.php index 4258db8972..86313caa94 100644 --- a/tests/e2e/Services/Databases/DatabasesCustomServerTest.php +++ b/tests/e2e/Services/Databases/DatabasesCustomServerTest.php @@ -3402,6 +3402,27 @@ class DatabasesCustomServerTest extends Scope $this->assertEquals(200, $document['headers']['status-code']); $this->assertEquals(10, strlen($document['body']['string'])); + + // Try create document with string that is too large + $newDoc = $this->client->call( + Client::METHOD_POST, + '/databases/' . $databaseId . '/collections/' . $collectionId . '/documents', + array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'] + ]), + [ + 'documentId' => 'unique()', + 'data' => [ + 'string' => str_repeat('a', 11) + ], + "permissions" => ["read(\"any\")"] + ] + ); + + $this->assertEquals(400, $newDoc['headers']['status-code']); + $this->assertEquals(AppwriteException::DOCUMENT_INVALID_STRUCTURE, $newDoc['body']['type']); } /**