From c0efc232ad2b625711fbd9154633f42d25443493 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Sun, 14 Jul 2024 11:31:02 +0545 Subject: [PATCH 1/5] Fix warning when optional attribute doesn't exist When an attribute required is set to `false` and the attribute doesn't exist in the response, we get warning `undefined array key "attribute"` in the console, this update prevents that error --- src/Appwrite/Utopia/Response.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Appwrite/Utopia/Response.php b/src/Appwrite/Utopia/Response.php index 6601a36075..5b7ac04db2 100644 --- a/src/Appwrite/Utopia/Response.php +++ b/src/Appwrite/Utopia/Response.php @@ -590,6 +590,10 @@ class Response extends SwooleResponse } } + if(!$data->isSet($key) && !$rule['required']) { // skip attribute in response if not required and values does not exist + continue; + } + if ($rule['array']) { if (!is_array($data[$key])) { throw new Exception($key . ' must be an array of type ' . $rule['type']); From b3aaca317c0eef8aa6db03e835f0103a63656bd7 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Sun, 14 Jul 2024 06:05:51 +0000 Subject: [PATCH 2/5] format and update test --- src/Appwrite/Utopia/Response.php | 2 +- tests/unit/Utopia/ResponseTest.php | 19 +++++++++++++++++++ tests/unit/Utopia/Single.php | 5 +++++ 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/Appwrite/Utopia/Response.php b/src/Appwrite/Utopia/Response.php index 5b7ac04db2..afbee30f58 100644 --- a/src/Appwrite/Utopia/Response.php +++ b/src/Appwrite/Utopia/Response.php @@ -593,7 +593,7 @@ class Response extends SwooleResponse if(!$data->isSet($key) && !$rule['required']) { // skip attribute in response if not required and values does not exist continue; } - + if ($rule['array']) { if (!is_array($data[$key])) { throw new Exception($key . ' must be an array of type ' . $rule['type']); diff --git a/tests/unit/Utopia/ResponseTest.php b/tests/unit/Utopia/ResponseTest.php index cd111ec22c..4a2202f06c 100644 --- a/tests/unit/Utopia/ResponseTest.php +++ b/tests/unit/Utopia/ResponseTest.php @@ -55,12 +55,31 @@ class ResponseTest extends TestCase 'integer' => 123, 'boolean' => true, 'hidden' => 'secret', + 'array' => [ + 'string 1', + 'string 2' + ], ]), 'single'); $this->assertArrayHasKey('string', $output); $this->assertArrayHasKey('integer', $output); $this->assertArrayHasKey('boolean', $output); $this->assertArrayNotHasKey('hidden', $output); + $this->assertIsArray($output['array']); + + // test optional array + $output = $this->response->output(new Document([ + 'string' => 'lorem ipsum', + 'integer' => 123, + 'boolean' => true, + 'hidden' => 'secret', + ]), 'single'); + $this->assertArrayHasKey('string', $output); + $this->assertArrayHasKey('integer', $output); + $this->assertArrayHasKey('boolean', $output); + $this->assertArrayNotHasKey('hidden', $output); + $this->assertArrayNotHasKey('array', $output); + } public function testResponseModelRequired(): void diff --git a/tests/unit/Utopia/Single.php b/tests/unit/Utopia/Single.php index 3bd09ef6da..b7f36d10a8 100644 --- a/tests/unit/Utopia/Single.php +++ b/tests/unit/Utopia/Single.php @@ -28,6 +28,11 @@ class Single extends Model 'type' => self::TYPE_STRING, 'default' => 'default', 'required' => true + ]) + ->addRule('array', [ + 'type' => self::TYPE_STRING, + 'required' => false, + 'array' => true, ]); } From 03c48a5ef3bcd23baf97c4eb7e79bc75fa215712 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Mon, 15 Jul 2024 05:35:04 +0000 Subject: [PATCH 3/5] set null when not required and data key is not set --- src/Appwrite/Utopia/Response.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Appwrite/Utopia/Response.php b/src/Appwrite/Utopia/Response.php index afbee30f58..2cba368cf8 100644 --- a/src/Appwrite/Utopia/Response.php +++ b/src/Appwrite/Utopia/Response.php @@ -590,7 +590,8 @@ class Response extends SwooleResponse } } - if(!$data->isSet($key) && !$rule['required']) { // skip attribute in response if not required and values does not exist + if(!$data->isSet($key) && !$rule['required']) { // set output key null if data key is not set and required is false + $output[$key] = null; continue; } From 1c7e6d7d811be9eb04f794e1285f90b3bca4f573 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Mon, 15 Jul 2024 07:08:22 +0000 Subject: [PATCH 4/5] fix test --- tests/unit/Utopia/ResponseTest.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/unit/Utopia/ResponseTest.php b/tests/unit/Utopia/ResponseTest.php index 4a2202f06c..452119fafb 100644 --- a/tests/unit/Utopia/ResponseTest.php +++ b/tests/unit/Utopia/ResponseTest.php @@ -78,7 +78,8 @@ class ResponseTest extends TestCase $this->assertArrayHasKey('integer', $output); $this->assertArrayHasKey('boolean', $output); $this->assertArrayNotHasKey('hidden', $output); - $this->assertArrayNotHasKey('array', $output); + $this->assertArrayHasKey('array', $output); + $this->assertNull($output['array']); } From 9b3f1a2c4aa2ba0da810339fe7167e1c4891e3af Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Sun, 22 Sep 2024 02:43:29 +0000 Subject: [PATCH 5/5] fix linter --- src/Appwrite/Utopia/Response.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Appwrite/Utopia/Response.php b/src/Appwrite/Utopia/Response.php index 954d8af638..6cc2639f51 100644 --- a/src/Appwrite/Utopia/Response.php +++ b/src/Appwrite/Utopia/Response.php @@ -625,7 +625,7 @@ class Response extends SwooleResponse } } - if(!$data->isSet($key) && !$rule['required']) { // set output key null if data key is not set and required is false + if (!$data->isSet($key) && !$rule['required']) { // set output key null if data key is not set and required is false $output[$key] = null; continue; }