Skip to content

Commit 1a259fd

Browse files
committed
Fix arguments transformer when data is not set
When data for an input doesn't exist (ie. The property is not set in the data), use the default value of the input property or null if is doesn't have one.
1 parent e2fc804 commit 1a259fd

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

src/Transformer/ArgumentsTransformer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ private function populateObject(Type $type, $data, bool $multiple, ResolveInfo $
102102
$fields = $type->getFields();
103103

104104
foreach ($fields as $name => $field) {
105-
$fieldData = $this->accessor->getValue($data, sprintf('[%s]', $name));
105+
$fieldData = \array_key_exists($name, $data) ? $this->accessor->getValue($data, sprintf('[%s]', $name)) : $this->accessor->getValue($instance, $name);
106106
$fieldType = $field->getType();
107107

108108
if ($fieldType instanceof NonNull) {

tests/Transformer/ArgumentsTransformerTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,9 @@ public function testPopulating(): void
125125
$this->assertEquals($res->field1, $data['field1']);
126126
$this->assertEquals($res->field2, $data['field2']);
127127
$this->assertEquals($res->field3, $data['field3']);
128+
$this->assertEquals($res->field4, "default_value_when_not_set_in_data");
129+
$this->assertEquals($res->field5, []);
130+
$this->assertEquals($res->field6, null);
128131

129132
$data = [
130133
'field1' => [

tests/Transformer/InputType1.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,16 @@ final class InputType1
2020
* @var mixed
2121
*/
2222
public $field3;
23+
24+
/**
25+
* @var mixed
26+
*/
27+
public $field4 = "default_value_when_not_set_in_data";
28+
29+
/**
30+
* @var array
31+
*/
32+
public $field5 = [];
33+
34+
public $field6;
2335
}

0 commit comments

Comments
 (0)