Merge pull request #8391 from appwrite/lohanidamodar-patch-3

Fix: throwing exception when optional array attribute does not exist
This commit is contained in:
Damodar Lohani 2024-09-29 07:28:21 +05:45 committed by GitHub
commit 8e4ceebaef
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 30 additions and 0 deletions

View file

@ -625,6 +625,11 @@ class Response extends SwooleResponse
}
}
if (!$data->isSet($key) && !$rule['required']) { // set output key null if data key is not set and required is false
$output[$key] = null;
continue;
}
if ($rule['array']) {
if (!is_array($data[$key])) {
throw new Exception($key . ' must be an array of type ' . $rule['type']);

View file

@ -55,12 +55,32 @@ 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->assertArrayHasKey('array', $output);
$this->assertNull($output['array']);
}
public function testResponseModelRequired(): void

View file

@ -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,
]);
}