From 32cb7344326f5ae91c37052245c669245c574e31 Mon Sep 17 00:00:00 2001 From: Dmitry Hordinsky Date: Mon, 11 Jan 2021 16:11:56 +0200 Subject: [PATCH 1/2] Fix error for non-nullable array of non-nullable elements annotation --- src/Transformer/ArgumentsTransformer.php | 9 ++- .../Transformer/ArgumentsTransformerTest.php | 75 +++++++++++++++++++ 2 files changed, 82 insertions(+), 2 deletions(-) diff --git a/src/Transformer/ArgumentsTransformer.php b/src/Transformer/ArgumentsTransformer.php index 4d201e2c3..8102f42f7 100644 --- a/src/Transformer/ArgumentsTransformer.php +++ b/src/Transformer/ArgumentsTransformer.php @@ -142,8 +142,13 @@ public function getInstanceAndValidate(string $argType, $data, ResolveInfo $info { $isRequired = '!' === $argType[\strlen($argType) - 1]; $isMultiple = '[' === $argType[0]; - $endIndex = ($isRequired ? 1 : 0) + ($isMultiple ? 1 : 0); - $type = \substr($argType, $isMultiple ? 1 : 0, $endIndex > 0 ? -$endIndex : \strlen($argType)); + $isStrictMultiple = false; + if ($isMultiple) { + $isStrictMultiple = '!' === $argType[strpos($argType, ']') - 1]; + } + + $endIndex = ($isRequired ? 1 : 0) + ($isMultiple ? 1 : 0) + ($isStrictMultiple ? 1 : 0); + $type = \substr($argType, $isMultiple ? 1 : 0, $endIndex > 0 ? -$endIndex : strlen($argType)); $result = $this->populateObject($this->getType($type, $info), $data, $isMultiple, $info); $errors = []; diff --git a/tests/Transformer/ArgumentsTransformerTest.php b/tests/Transformer/ArgumentsTransformerTest.php index c20804fde..a94b47ad5 100644 --- a/tests/Transformer/ArgumentsTransformerTest.php +++ b/tests/Transformer/ArgumentsTransformerTest.php @@ -4,8 +4,12 @@ namespace Overblog\GraphQLBundle\Tests\Transformer; +use Exception; +use Generator; use GraphQL\Type\Definition\EnumType; use GraphQL\Type\Definition\InputObjectType; +use GraphQL\Type\Definition\ListOfType; +use GraphQL\Type\Definition\NonNull; use GraphQL\Type\Definition\ResolveInfo; use GraphQL\Type\Definition\Type; use GraphQL\Type\Schema; @@ -180,4 +184,75 @@ public function testRaisedErrors(): void $this->assertEquals($e->toState(), $expected); } } + + public function getWrappedInputObject(): Generator + { + $inputObject = new InputObjectType([ + 'name' => 'InputType1', + 'fields' => [ + 'field1' => Type::string(), + 'field2' => Type::int(), + 'field3' => Type::boolean(), + ], + ]); + yield [$inputObject, false]; + yield [new NonNull($inputObject), false]; + } + + /** @dataProvider getWrappedInputObject */ + public function testInputObjectWithWrappingType(Type $type): void + { + $transformer = $this->getTransformer([ + 'InputType1' => ['type' => 'input', 'class' => InputType1::class], + ], new ConstraintViolationList() + ); + $info = $this->getResolveInfo(self::getTypes()); + + $data = ['field1' => 'hello', 'field2' => 12, 'field3' => true]; + + $inputValue = $transformer->getInstanceAndValidate($type->toString(), $data, $info, 'input1'); + + /** @var InputType1 $inputValue */ + $this->assertInstanceOf(InputType1::class, $inputValue); + $this->assertEquals($inputValue->field1, $data['field1']); + $this->assertEquals($inputValue->field2, $data['field2']); + $this->assertEquals($inputValue->field3, $data['field3']); + } + + public function getWrappedInputObjectList(): Generator + { + $inputObject = new InputObjectType([ + 'name' => 'InputType1', + 'fields' => [ + 'field1' => Type::string(), + 'field2' => Type::int(), + 'field3' => Type::boolean(), + ], + ]); + yield [new ListOfType($inputObject)]; + yield [new ListOfType(new NonNull($inputObject))]; + yield [new NonNull(new ListOfType($inputObject))]; + yield [new NonNull(new ListOfType(new NonNull($inputObject)))]; + } + + /** @dataProvider getWrappedInputObjectList */ + public function testInputObjectWithWrappingTypeList(Type $type): void + { + $transformer = $this->getTransformer( + ['InputType1' => ['type' => 'input', 'class' => InputType1::class]], + new ConstraintViolationList() + ); + $info = $this->getResolveInfo(self::getTypes()); + + $data = ['field1' => 'hello', 'field2' => 12, 'field3' => true]; + + $inputValue = $transformer->getInstanceAndValidate($type->toString(), [$data], $info, 'input1'); + $inputValue = reset($inputValue); + + /** @var InputType1 $inputValue */ + $this->assertInstanceOf(InputType1::class, $inputValue); + $this->assertEquals($inputValue->field1, $data['field1']); + $this->assertEquals($inputValue->field2, $data['field2']); + $this->assertEquals($inputValue->field3, $data['field3']); + } } From f187a08b65a28dd9feb7678a621e53fc36e93a5e Mon Sep 17 00:00:00 2001 From: Jeremiah VALERIE Date: Wed, 13 Jan 2021 12:33:51 +0100 Subject: [PATCH 2/2] Fix CS --- src/Transformer/ArgumentsTransformer.php | 4 ++-- tests/Transformer/ArgumentsTransformerTest.php | 7 +++---- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/Transformer/ArgumentsTransformer.php b/src/Transformer/ArgumentsTransformer.php index 8102f42f7..1f49747ab 100644 --- a/src/Transformer/ArgumentsTransformer.php +++ b/src/Transformer/ArgumentsTransformer.php @@ -144,11 +144,11 @@ public function getInstanceAndValidate(string $argType, $data, ResolveInfo $info $isMultiple = '[' === $argType[0]; $isStrictMultiple = false; if ($isMultiple) { - $isStrictMultiple = '!' === $argType[strpos($argType, ']') - 1]; + $isStrictMultiple = '!' === $argType[\strpos($argType, ']') - 1]; } $endIndex = ($isRequired ? 1 : 0) + ($isMultiple ? 1 : 0) + ($isStrictMultiple ? 1 : 0); - $type = \substr($argType, $isMultiple ? 1 : 0, $endIndex > 0 ? -$endIndex : strlen($argType)); + $type = \substr($argType, $isMultiple ? 1 : 0, $endIndex > 0 ? -$endIndex : \strlen($argType)); $result = $this->populateObject($this->getType($type, $info), $data, $isMultiple, $info); $errors = []; diff --git a/tests/Transformer/ArgumentsTransformerTest.php b/tests/Transformer/ArgumentsTransformerTest.php index a94b47ad5..3cd2a592f 100644 --- a/tests/Transformer/ArgumentsTransformerTest.php +++ b/tests/Transformer/ArgumentsTransformerTest.php @@ -4,7 +4,6 @@ namespace Overblog\GraphQLBundle\Tests\Transformer; -use Exception; use Generator; use GraphQL\Type\Definition\EnumType; use GraphQL\Type\Definition\InputObjectType; @@ -212,7 +211,7 @@ public function testInputObjectWithWrappingType(Type $type): void $inputValue = $transformer->getInstanceAndValidate($type->toString(), $data, $info, 'input1'); - /** @var InputType1 $inputValue */ + /* @var InputType1 $inputValue */ $this->assertInstanceOf(InputType1::class, $inputValue); $this->assertEquals($inputValue->field1, $data['field1']); $this->assertEquals($inputValue->field2, $data['field2']); @@ -247,9 +246,9 @@ public function testInputObjectWithWrappingTypeList(Type $type): void $data = ['field1' => 'hello', 'field2' => 12, 'field3' => true]; $inputValue = $transformer->getInstanceAndValidate($type->toString(), [$data], $info, 'input1'); - $inputValue = reset($inputValue); + $inputValue = \reset($inputValue); - /** @var InputType1 $inputValue */ + /* @var InputType1 $inputValue */ $this->assertInstanceOf(InputType1::class, $inputValue); $this->assertEquals($inputValue->field1, $data['field1']); $this->assertEquals($inputValue->field2, $data['field2']);