Skip to content

Commit dd90655

Browse files
committed
Fix encode
1 parent 9c24931 commit dd90655

2 files changed

Lines changed: 20 additions & 13 deletions

File tree

src/Database/Database.php

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6293,9 +6293,7 @@ public function encode(Document $collection, Document $document): Document
62936293
$attributes = $collection->getAttribute('attributes', []);
62946294

62956295
foreach ($this->getInternalAttributes() as $attribute) {
6296-
if ($attribute['$id'] !== '$permissions') { // Don't encode permissions into a JSON string
6297-
$attributes[] = new Document($attribute);
6298-
}
6296+
$attributes[] = $attribute;
62996297
}
63006298

63016299
foreach ($attributes as $attribute) {
@@ -6305,6 +6303,13 @@ public function encode(Document $collection, Document $document): Document
63056303
$filters = $attribute['filters'] ?? [];
63066304
$value = $document->getAttribute($key);
63076305

6306+
if ($attribute['$id'] === '$permissions') {
6307+
if (empty($value)){
6308+
$document->setAttribute('$permissions', []); // set default value
6309+
}
6310+
continue;
6311+
}
6312+
63086313
// Continue on optional param with no default
63096314
if (is_null($value) && is_null($default)) {
63106315
continue;
@@ -6370,12 +6375,13 @@ public function decode(Document $collection, Document $document, array $selectio
63706375
}
63716376

63726377
foreach ($this->getInternalAttributes() as $attribute) {
6373-
if ($attribute['$id'] !== '$permissions') {
6374-
$attributes[] = new Document($attribute);
6375-
}
6378+
$attributes[] = $attribute;
63766379
}
63776380

63786381
foreach ($attributes as $attribute) {
6382+
// if ($attribute['$id'] === '$permissions') {
6383+
// continue;
6384+
// }
63796385
$key = $attribute['$id'] ?? '';
63806386
$array = $attribute['array'] ?? false;
63816387
$filters = $attribute['filters'] ?? [];
@@ -6428,12 +6434,13 @@ public function casting(Document $collection, Document $document): Document
64286434
$attributes = $collection->getAttribute('attributes', []);
64296435

64306436
foreach ($this->getInternalAttributes() as $attribute) {
6431-
if ($attribute['$id'] !== '$permissions') {
6432-
$attributes[] = new Document($attribute);
6433-
}
6437+
$attributes[] = $attribute;
64346438
}
64356439

64366440
foreach ($attributes as $attribute) {
6441+
// if ($attribute['$id'] === '$permissions') {
6442+
// continue;
6443+
// }
64376444
$key = $attribute['$id'] ?? '';
64386445
$type = $attribute['type'] ?? '';
64396446
$array = $attribute['array'] ?? false;

tests/e2e/Adapter/Scopes/PermissionTests.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ public function testCreateDocumentsEmptyPermission(): void
2727

2828
$document = $database->createDocument(__FUNCTION__, new Document());
2929

30-
$this->assertArrayNotHasKey('$permissions', $document);
31-
$this->assertEquals(null, $document->getAttribute('$permissions'));
30+
$this->assertArrayHasKey('$permissions', $document);
31+
$this->assertEquals([], $document->getAttribute('$permissions'));
3232

3333
$documents = [];
3434

@@ -43,8 +43,8 @@ public function testCreateDocumentsEmptyPermission(): void
4343

4444
$this->assertEquals(2, $count);
4545
foreach ($results as $result) {
46-
$this->assertArrayNotHasKey('$permissions', $result);
47-
$this->assertEquals(null, $result->getAttribute('$permissions'));
46+
$this->assertArrayHasKey('$permissions', $result);
47+
$this->assertEquals([], $result->getAttribute('$permissions'));
4848
}
4949
}
5050

0 commit comments

Comments
 (0)