Skip to content

Commit d0b9710

Browse files
authored
Merge pull request #807 from mcg-web/patch-806
[PATCH] Fix error for non-nullable array of non-nullable elements annotation
2 parents 89e3ca1 + f187a08 commit d0b9710

File tree

2 files changed

+80
-1
lines changed

2 files changed

+80
-1
lines changed

src/Transformer/ArgumentsTransformer.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,12 @@ public function getInstanceAndValidate(string $argType, $data, ResolveInfo $info
142142
{
143143
$isRequired = '!' === $argType[\strlen($argType) - 1];
144144
$isMultiple = '[' === $argType[0];
145-
$endIndex = ($isRequired ? 1 : 0) + ($isMultiple ? 1 : 0);
145+
$isStrictMultiple = false;
146+
if ($isMultiple) {
147+
$isStrictMultiple = '!' === $argType[\strpos($argType, ']') - 1];
148+
}
149+
150+
$endIndex = ($isRequired ? 1 : 0) + ($isMultiple ? 1 : 0) + ($isStrictMultiple ? 1 : 0);
146151
$type = \substr($argType, $isMultiple ? 1 : 0, $endIndex > 0 ? -$endIndex : \strlen($argType));
147152

148153
$result = $this->populateObject($this->getType($type, $info), $data, $isMultiple, $info);

tests/Transformer/ArgumentsTransformerTest.php

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@
44

55
namespace Overblog\GraphQLBundle\Tests\Transformer;
66

7+
use Generator;
78
use GraphQL\Type\Definition\EnumType;
89
use GraphQL\Type\Definition\InputObjectType;
10+
use GraphQL\Type\Definition\ListOfType;
11+
use GraphQL\Type\Definition\NonNull;
912
use GraphQL\Type\Definition\ResolveInfo;
1013
use GraphQL\Type\Definition\Type;
1114
use GraphQL\Type\Schema;
@@ -180,4 +183,75 @@ public function testRaisedErrors(): void
180183
$this->assertEquals($e->toState(), $expected);
181184
}
182185
}
186+
187+
public function getWrappedInputObject(): Generator
188+
{
189+
$inputObject = new InputObjectType([
190+
'name' => 'InputType1',
191+
'fields' => [
192+
'field1' => Type::string(),
193+
'field2' => Type::int(),
194+
'field3' => Type::boolean(),
195+
],
196+
]);
197+
yield [$inputObject, false];
198+
yield [new NonNull($inputObject), false];
199+
}
200+
201+
/** @dataProvider getWrappedInputObject */
202+
public function testInputObjectWithWrappingType(Type $type): void
203+
{
204+
$transformer = $this->getTransformer([
205+
'InputType1' => ['type' => 'input', 'class' => InputType1::class],
206+
], new ConstraintViolationList()
207+
);
208+
$info = $this->getResolveInfo(self::getTypes());
209+
210+
$data = ['field1' => 'hello', 'field2' => 12, 'field3' => true];
211+
212+
$inputValue = $transformer->getInstanceAndValidate($type->toString(), $data, $info, 'input1');
213+
214+
/* @var InputType1 $inputValue */
215+
$this->assertInstanceOf(InputType1::class, $inputValue);
216+
$this->assertEquals($inputValue->field1, $data['field1']);
217+
$this->assertEquals($inputValue->field2, $data['field2']);
218+
$this->assertEquals($inputValue->field3, $data['field3']);
219+
}
220+
221+
public function getWrappedInputObjectList(): Generator
222+
{
223+
$inputObject = new InputObjectType([
224+
'name' => 'InputType1',
225+
'fields' => [
226+
'field1' => Type::string(),
227+
'field2' => Type::int(),
228+
'field3' => Type::boolean(),
229+
],
230+
]);
231+
yield [new ListOfType($inputObject)];
232+
yield [new ListOfType(new NonNull($inputObject))];
233+
yield [new NonNull(new ListOfType($inputObject))];
234+
yield [new NonNull(new ListOfType(new NonNull($inputObject)))];
235+
}
236+
237+
/** @dataProvider getWrappedInputObjectList */
238+
public function testInputObjectWithWrappingTypeList(Type $type): void
239+
{
240+
$transformer = $this->getTransformer(
241+
['InputType1' => ['type' => 'input', 'class' => InputType1::class]],
242+
new ConstraintViolationList()
243+
);
244+
$info = $this->getResolveInfo(self::getTypes());
245+
246+
$data = ['field1' => 'hello', 'field2' => 12, 'field3' => true];
247+
248+
$inputValue = $transformer->getInstanceAndValidate($type->toString(), [$data], $info, 'input1');
249+
$inputValue = \reset($inputValue);
250+
251+
/* @var InputType1 $inputValue */
252+
$this->assertInstanceOf(InputType1::class, $inputValue);
253+
$this->assertEquals($inputValue->field1, $data['field1']);
254+
$this->assertEquals($inputValue->field2, $data['field2']);
255+
$this->assertEquals($inputValue->field3, $data['field3']);
256+
}
183257
}

0 commit comments

Comments
 (0)