Skip to content

Commit f5bed7c

Browse files
authored
Fix handling of native union types containing null (#570)
* Fix handling of native union types containing null * Fix tabulation
1 parent 6991df9 commit f5bed7c

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

src/Mappers/Root/NullableTypeMapperAdapter.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ private function getNonNullable(Type $type): Type|null
126126
return null;
127127
}
128128
if ($type instanceof Nullable) {
129-
return $type->getActualType();
129+
return $this->getNonNullable($type->getActualType());
130130
}
131131
if ($type instanceof Compound) {
132132
$types = array_map([$this, 'getNonNullable'], iterator_to_array($type));

tests/Mappers/Root/NullableTypeMapperAdapterTest.php

+19-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace TheCodingMachine\GraphQLite\Mappers\Root;
44

5+
use Generator;
56
use GraphQL\Type\Definition\InputType;
67
use GraphQL\Type\Definition\NamedType;
78
use GraphQL\Type\Definition\NonNull;
@@ -10,6 +11,7 @@
1011
use GraphQL\Type\Definition\Type as GraphQLType;
1112
use phpDocumentor\Reflection\DocBlock;
1213
use phpDocumentor\Reflection\Type;
14+
use phpDocumentor\Reflection\Types\Nullable;
1315
use ReflectionMethod;
1416
use TheCodingMachine\GraphQLite\AbstractQueryProviderTest;
1517
use TheCodingMachine\GraphQLite\Fixtures\TestObject;
@@ -18,14 +20,28 @@
1820

1921
class NullableTypeMapperAdapterTest extends AbstractQueryProviderTest
2022
{
21-
public function testMultipleCompound(): void
23+
/**
24+
* @dataProvider nullableVariationsProvider
25+
*/
26+
public function testMultipleCompound(callable $type): void
2227
{
2328
$compoundTypeMapper = $this->getRootTypeMapper();
2429

25-
$result = $compoundTypeMapper->toGraphQLOutputType($this->resolveType(TestObject::class.'|'.TestObject2::class.'|null'), null, new ReflectionMethod(__CLASS__, 'testMultipleCompound'), new DocBlock());
30+
$result = $compoundTypeMapper->toGraphQLOutputType($type(), null, new ReflectionMethod(__CLASS__, 'testMultipleCompound'), new DocBlock());
2631
$this->assertNotInstanceOf(NonNull::class, $result);
2732
}
2833

34+
public function nullableVariationsProvider(): Generator
35+
{
36+
yield 'php documentor generated from phpdoc' => [
37+
fn () => $this->resolveType(TestObject::class . '|' . TestObject2::class . '|null'),
38+
];
39+
40+
yield 'type handler nullable wrapped native reflection union type' => [
41+
fn () => new Nullable($this->resolveType(TestObject::class . '|' . TestObject2::class . '|null')),
42+
];
43+
}
44+
2945
public function testOnlyNull(): void
3046
{
3147
$compoundTypeMapper = $this->getRootTypeMapper();
@@ -69,6 +85,6 @@ public function mapNameToType(string $typeName): NamedType&GraphQLType
6985

7086
$this->expectException(CannotMapTypeException::class);
7187
$this->expectExceptionMessage('a type mapper returned a GraphQL\\Type\\Definition\\NonNull instance.');
72-
$typeMapper->toGraphQLOutputType($this->resolveType(TestObject::class.'|'.TestObject2::class.'|null'), null, new ReflectionMethod(__CLASS__, 'testMultipleCompound'), new DocBlock());
88+
$typeMapper->toGraphQLOutputType($this->resolveType(TestObject::class . '|' . TestObject2::class . '|null'), null, new ReflectionMethod(__CLASS__, 'testMultipleCompound'), new DocBlock());
7389
}
7490
}

0 commit comments

Comments
 (0)