From 48d30d71d67d723a959383e271baff6690f590e3 Mon Sep 17 00:00:00 2001 From: Eldad Fux Date: Thu, 18 Jun 2020 23:13:03 +0300 Subject: [PATCH 1/2] Uncaught error with non JSON object creation of document Fixes #424 --- app/controllers/api/database.php | 7 ++++--- composer.json | 4 ++-- composer.lock | 24 ++++++++++++------------ 3 files changed, 18 insertions(+), 17 deletions(-) diff --git a/app/controllers/api/database.php b/app/controllers/api/database.php index 69d01e9dc7..b21f55544d 100644 --- a/app/controllers/api/database.php +++ b/app/controllers/api/database.php @@ -9,6 +9,7 @@ use Utopia\Validator\Range; use Utopia\Validator\WhiteList; use Utopia\Validator\Text; use Utopia\Validator\ArrayList; +use Utopia\Validator\JSON; use Utopia\Locale\Locale; use Utopia\Audit\Audit; use Utopia\Audit\Adapters\MySQL as AuditAdapter; @@ -351,7 +352,7 @@ $utopia->post('/v1/database/collections/:collectionId/documents') ->label('sdk.method', 'createDocument') ->label('sdk.description', '/docs/references/database/create-document.md') ->param('collectionId', null, function () { return new UID(); }, 'Collection unique ID. You can create a new collection with validation rules using the Database service [server integration](/docs/database?platform=server#createCollection).') - ->param('data', [], function () { return new \Utopia\Validator\Mock(); }, 'Document data as JSON object.') + ->param('data', [], function () { return new JSON(); }, 'Document data as JSON object.') ->param('read', [], function () { return new ArrayList(new Text(64)); }, 'An array of strings with read permissions. By default no user is granted with any read permissions. [learn more about permissions](/docs/permissions) and get a full list of available permissions.') ->param('write', [], function () { return new ArrayList(new Text(64)); }, 'An array of strings with write permissions. By default no user is granted with any write permissions. [learn more about permissions](/docs/permissions) and get a full list of available permissions.') ->param('parentDocument', '', function () { return new UID(); }, 'Parent document unique ID. Use when you want your new document to be a child of a parent document.', true) @@ -359,8 +360,8 @@ $utopia->post('/v1/database/collections/:collectionId/documents') ->param('parentPropertyType', Document::SET_TYPE_ASSIGN, function () { return new WhiteList([Document::SET_TYPE_ASSIGN, Document::SET_TYPE_APPEND, Document::SET_TYPE_PREPEND]); }, 'Parent document property connection type. You can set this value to **assign**, **append** or **prepend**, default value is assign. Use when you want your new document to be a child of a parent document.', true) ->action( function ($collectionId, $data, $read, $write, $parentDocument, $parentProperty, $parentPropertyType) use ($response, $projectDB, $webhook, $audit) { - $data = (is_string($data) && $result = json_decode($data, true)) ? $result : $data; // Cast to JSON array - + $data = (is_string($data)) ? json_decode($data, true) : $data; // Cast to JSON array + if (empty($data)) { throw new Exception('Missing payload', 400); } diff --git a/composer.json b/composer.json index a692b56baa..9b658475d9 100644 --- a/composer.json +++ b/composer.json @@ -32,11 +32,11 @@ "appwrite/php-clamav": "1.0.*", - "utopia-php/framework": "0.2.*", + "utopia-php/framework": "0.3.*", "utopia-php/abuse": "0.2.*", "utopia-php/audit": "0.3.*", "utopia-php/cache": "0.2.*", - "utopia-php/cli": "0.5.*", + "utopia-php/cli": "0.6.*", "utopia-php/config": "0.2.*", "utopia-php/locale": "0.2.*", "utopia-php/registry": "0.2.*", diff --git a/composer.lock b/composer.lock index ef3e3d104d..6cf7d8a53b 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "e93c84ab77f0085004222c942dcba6f4", + "content-hash": "001a9336198793f293f83f62df2b7567", "packages": [ { "name": "appwrite/php-clamav", @@ -1452,21 +1452,21 @@ }, { "name": "utopia-php/cli", - "version": "0.5.0", + "version": "0.6.0", "source": { "type": "git", "url": "https://github.com/utopia-php/cli.git", - "reference": "c66255fed87f00c5a8f7cc0c482cdb082260cd32" + "reference": "e636c61ac4d0cf7539be332adba9dd9889dc300e" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/cli/zipball/c66255fed87f00c5a8f7cc0c482cdb082260cd32", - "reference": "c66255fed87f00c5a8f7cc0c482cdb082260cd32", + "url": "https://api.github.com/repos/utopia-php/cli/zipball/e636c61ac4d0cf7539be332adba9dd9889dc300e", + "reference": "e636c61ac4d0cf7539be332adba9dd9889dc300e", "shasum": "" }, "require": { "php": ">=7.1", - "utopia-php/framework": "0.2.*" + "utopia-php/framework": "0.3.*" }, "require-dev": { "phpunit/phpunit": "^7.0" @@ -1496,7 +1496,7 @@ "upf", "utopia" ], - "time": "2020-05-05T09:02:10+00:00" + "time": "2020-06-18T19:28:40+00:00" }, { "name": "utopia-php/config", @@ -1596,16 +1596,16 @@ }, { "name": "utopia-php/framework", - "version": "0.2.0", + "version": "0.3.0", "source": { "type": "git", "url": "https://github.com/utopia-php/framework.git", - "reference": "e3d88b794badbd5ededcf62d708b31009111f860" + "reference": "9c3bb4fb69ab85d3d5ba76f334d682845dc4815c" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/framework/zipball/e3d88b794badbd5ededcf62d708b31009111f860", - "reference": "e3d88b794badbd5ededcf62d708b31009111f860", + "url": "https://api.github.com/repos/utopia-php/framework/zipball/9c3bb4fb69ab85d3d5ba76f334d682845dc4815c", + "reference": "9c3bb4fb69ab85d3d5ba76f334d682845dc4815c", "shasum": "" }, "require": { @@ -1636,7 +1636,7 @@ "php", "upf" ], - "time": "2020-03-08T15:42:21+00:00" + "time": "2020-06-18T19:23:47+00:00" }, { "name": "utopia-php/locale", From fea55375d8e74774e34c2e2e283d226468df1b22 Mon Sep 17 00:00:00 2001 From: Eldad Fux Date: Thu, 18 Jun 2020 23:52:59 +0300 Subject: [PATCH 2/2] Same fix for doc update --- app/controllers/api/database.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/controllers/api/database.php b/app/controllers/api/database.php index b21f55544d..5e908e5896 100644 --- a/app/controllers/api/database.php +++ b/app/controllers/api/database.php @@ -584,7 +584,7 @@ $utopia->patch('/v1/database/collections/:collectionId/documents/:documentId') ->label('sdk.description', '/docs/references/database/update-document.md') ->param('collectionId', null, function () { return new UID(); }, 'Collection unique ID. You can create a new collection with validation rules using the Database service [server integration](/docs/database?platform=server#createCollection).') ->param('documentId', null, function () { return new UID(); }, 'Document unique ID.') - ->param('data', [], function () { return new \Utopia\Validator\Mock(); }, 'Document data as JSON object.') + ->param('data', [], function () { return new JSON(); }, 'Document data as JSON object.') ->param('read', [], function () { return new ArrayList(new Text(64)); }, 'An array of strings with read permissions. By default no user is granted with any read permissions. [learn more about permissions](/docs/permissions) and get a full list of available permissions.') ->param('write', [], function () { return new ArrayList(new Text(64)); }, 'An array of strings with write permissions. By default no user is granted with any write permissions. [learn more about permissions](/docs/permissions) and get a full list of available permissions.') ->action( @@ -592,7 +592,7 @@ $utopia->patch('/v1/database/collections/:collectionId/documents/:documentId') $collection = $projectDB->getDocument($collectionId/*, $isDev*/); $document = $projectDB->getDocument($documentId, $isDev); - $data = (is_string($data) && $result = json_decode($data, true)) ? $result : $data; // Cast to JSON array + $data = (is_string($data)) ? json_decode($data, true) : $data; // Cast to JSON array if (!is_array($data)) { throw new Exception('Data param should be a valid JSON', 400);