Skip to content

Commit f09cfa7

Browse files
l-youvyacheslavko
andauthored
Fixed null for union types (#500)
* unit test for union type with null * Nullable union test * Nullable union bug fix * Fix test for old php version Co-authored-by: madness <ss@gmail.com>
1 parent 167e26e commit f09cfa7

File tree

3 files changed

+37
-3
lines changed

3 files changed

+37
-3
lines changed

src/Mappers/Root/CompoundTypeMapper.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
use phpDocumentor\Reflection\Type;
1616
use phpDocumentor\Reflection\Types\Compound;
1717
use phpDocumentor\Reflection\Types\Iterable_;
18+
use phpDocumentor\Reflection\Types\Null_;
19+
use phpDocumentor\Reflection\Types\Nullable;
1820
use ReflectionMethod;
1921
use ReflectionProperty;
2022
use RuntimeException;
@@ -76,6 +78,9 @@ public function toGraphQLOutputType(Type $type, ?OutputType $subType, $reflector
7678
$mustBeIterable = true;
7779
continue;
7880
}
81+
if ($singleDocBlockType instanceof Nullable && $singleDocBlockType->getActualType() instanceof Null_) {
82+
continue;
83+
}
7984
$unionTypes[] = $this->topRootTypeMapper->toGraphQLOutputType($singleDocBlockType, null, $reflector, $docBlockObj);
8085
}
8186

tests/Fixtures80/UnionOutputType.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,7 @@ class UnionOutputType
1212
public function objectUnion(): TestObject|TestObject2 {
1313
return new TestObject((''));
1414
}
15+
public function nullableObjectUnion(): TestObject|TestObject2|null {
16+
return new TestObject((''));
17+
}
1518
}

tests/Mappers/Parameters/TypeMapperTest.php

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,29 @@ public function testMapObjectUnionWorks(): void
5959
$this->assertEquals('TestObject', $unionTypes[0]->name);
6060
$this->assertEquals('TestObject2', $unionTypes[1]->name);
6161
}
62+
/**
63+
* @requires PHP >= 8.0
64+
*/
65+
public function testMapObjectNullableUnionWorks(): void
66+
{
67+
$typeMapper = new TypeHandler($this->getArgumentResolver(), $this->getRootTypeMapper(), $this->getTypeResolver());
68+
69+
$cachedDocBlockFactory = new CachedDocBlockFactory(new Psr16Cache(new ArrayAdapter()));
70+
71+
$refMethod = new ReflectionMethod(UnionOutputType::class, 'nullableObjectUnion');
72+
$docBlockObj = $cachedDocBlockFactory->getDocBlock($refMethod);
73+
74+
$gqType = $typeMapper->mapReturnType($refMethod, $docBlockObj);
75+
$this->assertNotInstanceOf(NonNull::class, $gqType);
76+
assert(!($gqType instanceof NonNull));
77+
$this->assertInstanceOf(UnionType::class, $gqType);
78+
assert($gqType instanceof UnionType);
79+
$unionTypes = $gqType->getTypes();
80+
$this->assertEquals(2, count($unionTypes));
81+
$this->assertEquals('TestObject', $unionTypes[0]->name);
82+
$this->assertEquals('TestObject2', $unionTypes[1]->name);
83+
84+
}
6285

6386
public function testHideParameter(): void
6487
{
@@ -99,21 +122,24 @@ public function testHideParameterException(): void
99122
/**
100123
* @return int|string
101124
*/
102-
private function dummy() {
125+
private function dummy()
126+
{
103127

104128
}
105129

106130
/**
107131
* @HideParameter(for="$foo")
108132
*/
109-
private function withDefaultValue($foo = 24) {
133+
private function withDefaultValue($foo = 24)
134+
{
110135

111136
}
112137

113138
/**
114139
* @HideParameter(for="$foo")
115140
*/
116-
private function withoutDefaultValue($foo) {
141+
private function withoutDefaultValue($foo)
142+
{
117143

118144
}
119145
}

0 commit comments

Comments
 (0)