mirror of
https://github.com/appwrite/appwrite
synced 2026-05-24 09:28:40 +00:00
Merge pull request #8391 from appwrite/lohanidamodar-patch-3
Fix: throwing exception when optional array attribute does not exist
This commit is contained in:
commit
8e4ceebaef
3 changed files with 30 additions and 0 deletions
|
|
@ -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 ($rule['array']) {
|
||||||
if (!is_array($data[$key])) {
|
if (!is_array($data[$key])) {
|
||||||
throw new Exception($key . ' must be an array of type ' . $rule['type']);
|
throw new Exception($key . ' must be an array of type ' . $rule['type']);
|
||||||
|
|
|
||||||
|
|
@ -55,12 +55,32 @@ class ResponseTest extends TestCase
|
||||||
'integer' => 123,
|
'integer' => 123,
|
||||||
'boolean' => true,
|
'boolean' => true,
|
||||||
'hidden' => 'secret',
|
'hidden' => 'secret',
|
||||||
|
'array' => [
|
||||||
|
'string 1',
|
||||||
|
'string 2'
|
||||||
|
],
|
||||||
]), 'single');
|
]), 'single');
|
||||||
|
|
||||||
$this->assertArrayHasKey('string', $output);
|
$this->assertArrayHasKey('string', $output);
|
||||||
$this->assertArrayHasKey('integer', $output);
|
$this->assertArrayHasKey('integer', $output);
|
||||||
$this->assertArrayHasKey('boolean', $output);
|
$this->assertArrayHasKey('boolean', $output);
|
||||||
$this->assertArrayNotHasKey('hidden', $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
|
public function testResponseModelRequired(): void
|
||||||
|
|
|
||||||
|
|
@ -28,6 +28,11 @@ class Single extends Model
|
||||||
'type' => self::TYPE_STRING,
|
'type' => self::TYPE_STRING,
|
||||||
'default' => 'default',
|
'default' => 'default',
|
||||||
'required' => true
|
'required' => true
|
||||||
|
])
|
||||||
|
->addRule('array', [
|
||||||
|
'type' => self::TYPE_STRING,
|
||||||
|
'required' => false,
|
||||||
|
'array' => true,
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue