From ba2790221c95521fecf3441b7df3546bd476db2a Mon Sep 17 00:00:00 2001 From: Torsten Dittmann Date: Mon, 17 Jan 2022 17:25:20 +0100 Subject: [PATCH] fix: hide internal id from documents --- app/controllers/api/database.php | 2 +- src/Appwrite/Utopia/Response.php | 7 ++++--- src/Appwrite/Utopia/Response/Model/Document.php | 8 ++++++++ .../Utopia/Response/Model/DocumentList.php | 17 +++++++++++++++++ tests/e2e/Services/Database/DatabaseBase.php | 4 ++++ 5 files changed, 34 insertions(+), 4 deletions(-) create mode 100644 src/Appwrite/Utopia/Response/Model/DocumentList.php diff --git a/app/controllers/api/database.php b/app/controllers/api/database.php index c6fb14970..c24a8e68d 100644 --- a/app/controllers/api/database.php +++ b/app/controllers/api/database.php @@ -1832,7 +1832,7 @@ App::get('/v1/database/collections/:collectionId/documents/:documentId') $usage ->setParam('database.documents.read', 1) ->setParam('collectionId', $collectionId) - ; + ; $response->dynamic($document, Response::MODEL_DOCUMENT); }); diff --git a/src/Appwrite/Utopia/Response.php b/src/Appwrite/Utopia/Response.php index 047ccb965..e2769057b 100644 --- a/src/Appwrite/Utopia/Response.php +++ b/src/Appwrite/Utopia/Response.php @@ -26,6 +26,7 @@ use Appwrite\Utopia\Response\Model\Continent; use Appwrite\Utopia\Response\Model\Country; use Appwrite\Utopia\Response\Model\Currency; use Appwrite\Utopia\Response\Model\Document as ModelDocument; +use Appwrite\Utopia\Response\Model\DocumentList; use Appwrite\Utopia\Response\Model\Domain; use Appwrite\Utopia\Response\Model\Error; use Appwrite\Utopia\Response\Model\ErrorDev; @@ -207,7 +208,6 @@ class Response extends SwooleResponse // Lists ->setModel(new BaseList('Collections List', self::MODEL_COLLECTION_LIST, 'collections', self::MODEL_COLLECTION)) ->setModel(new BaseList('Indexes List', self::MODEL_INDEX_LIST, 'indexes', self::MODEL_INDEX)) - ->setModel(new BaseList('Documents List', self::MODEL_DOCUMENT_LIST, 'documents', self::MODEL_DOCUMENT)) ->setModel(new BaseList('Users List', self::MODEL_USER_LIST, 'users', self::MODEL_USER)) ->setModel(new BaseList('Sessions List', self::MODEL_SESSION_LIST, 'sessions', self::MODEL_SESSION)) ->setModel(new BaseList('Logs List', self::MODEL_LOG_LIST, 'logs', self::MODEL_LOG)) @@ -229,6 +229,7 @@ class Response extends SwooleResponse ->setModel(new BaseList('Currencies List', self::MODEL_CURRENCY_LIST, 'currencies', self::MODEL_CURRENCY)) ->setModel(new BaseList('Phones List', self::MODEL_PHONE_LIST, 'phones', self::MODEL_PHONE)) ->setModel(new BaseList('Metric List', self::MODEL_METRIC_LIST, 'metrics', self::MODEL_METRIC, true, false)) + ->setModel(new DocumentList('Documents List', self::MODEL_DOCUMENT_LIST, 'documents', self::MODEL_DOCUMENT)) // Entities ->setModel(new Collection()) ->setModel(new Attribute()) @@ -370,13 +371,13 @@ class Response extends SwooleResponse $model = $this->getModel($model); $output = []; + $document = $model->filter($document); + if ($model->isAny()) { $this->payload = $document->getArrayCopy(); return $this->payload; } - $document = $model->filter($document); - foreach ($model->getRules() as $key => $rule) { if (!$document->isSet($key) && $rule['require']) { // do not set attribute in response if not required if (!is_null($rule['default'])) { diff --git a/src/Appwrite/Utopia/Response/Model/Document.php b/src/Appwrite/Utopia/Response/Model/Document.php index f911f9ab7..3b3629965 100644 --- a/src/Appwrite/Utopia/Response/Model/Document.php +++ b/src/Appwrite/Utopia/Response/Model/Document.php @@ -3,6 +3,7 @@ namespace Appwrite\Utopia\Response\Model; use Appwrite\Utopia\Response; +use Utopia\Database\Document as DatabaseDocument; class Document extends Any { @@ -57,4 +58,11 @@ class Document extends Any ]) ; } + + public function filter(DatabaseDocument $document): DatabaseDocument + { + $document->removeAttribute('$internalId'); + + return $document; + } } diff --git a/src/Appwrite/Utopia/Response/Model/DocumentList.php b/src/Appwrite/Utopia/Response/Model/DocumentList.php new file mode 100644 index 000000000..270f2ccbe --- /dev/null +++ b/src/Appwrite/Utopia/Response/Model/DocumentList.php @@ -0,0 +1,17 @@ +getAttribute('documents', []) as $node) { + $node->removeAttribute('$internalId'); + } + + return $document; + } +} diff --git a/tests/e2e/Services/Database/DatabaseBase.php b/tests/e2e/Services/Database/DatabaseBase.php index 0a7780b6f..f961d6ff4 100644 --- a/tests/e2e/Services/Database/DatabaseBase.php +++ b/tests/e2e/Services/Database/DatabaseBase.php @@ -825,6 +825,9 @@ trait DatabaseBase $this->assertEquals(1944, $documents['body']['documents'][0]['releaseYear']); $this->assertEquals(2017, $documents['body']['documents'][1]['releaseYear']); $this->assertEquals(2019, $documents['body']['documents'][2]['releaseYear']); + $this->assertEmpty($documents['body']['documents'][0]['$internalId']); + $this->assertEmpty($documents['body']['documents'][1]['$internalId']); + $this->assertEmpty($documents['body']['documents'][2]['$internalId']); $this->assertCount(3, $documents['body']['documents']); foreach ($documents['body']['documents'] as $document) { @@ -866,6 +869,7 @@ trait DatabaseBase $this->assertEquals($response['body']['releaseYear'], $document['releaseYear']); $this->assertEquals($response['body']['$read'], $document['$read']); $this->assertEquals($response['body']['$write'], $document['$write']); + $this->assertEmpty($response['body']['$internalId']); } }