Skip to content

Commit ab5b950

Browse files
Oliboy50Oliver Thébault
andauthored
print @deprecated directive when deprecationReason is empty string (#631)
* test(deprecated): add @deprecated directive tests for SchemaPrinter * fix(deprecated): put @deprecated directive when deprecationReason is empty string Co-authored-by: Oliver Thébault <othebault.externe@m6web.fr>
1 parent 5a9d2da commit ab5b950

4 files changed

Lines changed: 45 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
+ Cannot return null for non-nullable field "parentType.fieldName".
1818
```
1919
- Simplified Deferred implementation
20+
- Having an empty string in `deprecationReason` will now print the `@deprecated` directive (only a `null` `deprecationReason` won't print the `@deprecated` directive).
2021

2122
#### v0.13.5
2223
- Fix coroutine executor when using with promise (#486)

phpstan-baseline.neon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -902,7 +902,7 @@ parameters:
902902

903903
-
904904
message: "#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#"
905-
count: 3
905+
count: 2
906906
path: src/Utils/SchemaPrinter.php
907907

908908
-

src/Utils/SchemaPrinter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ static function ($f, $i) use ($options) {
393393
private static function printDeprecated($fieldOrEnumVal) : string
394394
{
395395
$reason = $fieldOrEnumVal->deprecationReason;
396-
if (empty($reason)) {
396+
if ($reason === null) {
397397
return '';
398398
}
399399
if ($reason === '' || $reason === Directive::DEFAULT_DEPRECATION_REASON) {

tests/Utils/SchemaPrinterTest.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace GraphQL\Tests\Utils;
66

7+
use Generator;
78
use GraphQL\Language\DirectiveLocation;
89
use GraphQL\Type\Definition\CustomScalarType;
910
use GraphQL\Type\Definition\Directive;
@@ -148,6 +149,47 @@ public function testPrintNonNullArrayNonNullStringField() : void
148149
);
149150
}
150151

152+
/**
153+
* @see it('Prints Field With "@deprecated" Directive')
154+
*
155+
* @dataProvider deprecationReasonDataProvider
156+
*/
157+
public function testPrintDeprecatedField(?string $deprecationReason, string $expectedDeprecationDirective) : void
158+
{
159+
$output = $this->printSingleFieldSchema([
160+
'type' => Type::int(),
161+
'deprecationReason' => $deprecationReason,
162+
]);
163+
self::assertSame(
164+
'
165+
type Query {
166+
singleField: Int' . $expectedDeprecationDirective . '
167+
}
168+
',
169+
$output
170+
);
171+
}
172+
173+
public function deprecationReasonDataProvider() : Generator
174+
{
175+
yield 'when deprecationReason is null' => [
176+
null,
177+
'',
178+
];
179+
yield 'when deprecationReason is empty string' => [
180+
'',
181+
' @deprecated',
182+
];
183+
yield 'when deprecationReason is the default deprecation reason' => [
184+
Directive::DEFAULT_DEPRECATION_REASON,
185+
' @deprecated',
186+
];
187+
yield 'when deprecationReason is not empty string' => [
188+
'this is deprecated',
189+
' @deprecated(reason: "this is deprecated")',
190+
];
191+
}
192+
151193
/**
152194
* @see it('Print Object Field')
153195
*/

0 commit comments

Comments
 (0)