Skip to content

Commit ce20a40

Browse files
authored
Merge pull request #4695 from magento-honey-badgers/hb-pr-branch
[honey] MC-18945 Reading deprecated annotation in schema
2 parents 9cb846b + c785556 commit ce20a40

File tree

20 files changed

+454
-38
lines changed

20 files changed

+454
-38
lines changed

app/code/Magento/DownloadableGraphQl/etc/schema.graphqls

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ type DownloadableProduct implements ProductInterface, CustomizableProductInterfa
5454
}
5555

5656
enum DownloadableFileTypeEnum @deprecated(reason: "`sample_url` serves to get the downloadable sample") {
57-
FILE
58-
URL
57+
FILE @deprecated(reason: "`sample_url` serves to get the downloadable sample")
58+
URL @deprecated(reason: "`sample_url` serves to get the downloadable sample")
5959
}
6060

6161
type DownloadableProductLinks @doc(description: "DownloadableProductLinks defines characteristics of a downloadable product") {

dev/tests/api-functional/testsuite/Magento/GraphQl/IntrospectionQueryTest.php

Lines changed: 129 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,135 @@ public function testIntrospectionQuery()
5353
defaultValue
5454
}
5555
QUERY;
56-
5756
$this->assertArrayHasKey('__schema', $this->graphQlQuery($query));
5857
}
58+
59+
/**
60+
* Tests that Introspection Query with deprecated annotations on enum values, fields are read.
61+
*/
62+
public function testIntrospectionQueryWithDeprecatedAnnotationOnEnumAndFieldValues()
63+
{
64+
$query
65+
= <<<QUERY
66+
query IntrospectionQuery {
67+
__schema {
68+
queryType { name }
69+
mutationType { name }
70+
types {
71+
...FullType
72+
}
73+
}
74+
}
75+
fragment FullType on __Type {
76+
kind
77+
name
78+
description
79+
fields(includeDeprecated: true) {
80+
name
81+
description
82+
isDeprecated
83+
deprecationReason
84+
}
85+
enumValues(includeDeprecated: true) {
86+
name
87+
description
88+
isDeprecated
89+
deprecationReason
90+
}
91+
}
92+
93+
QUERY;
94+
$response = $this->graphQlQuery($query);
95+
$this->assertArrayHasKey('__schema', $response);
96+
$schemaResponseFields = $response['__schema']['types'];
97+
$enumValueReasonArray = $this->getEnumValueDeprecatedReason($schemaResponseFields);
98+
$fieldsValueReasonArray = $this->getFieldsValueDeprecatedReason($schemaResponseFields);
99+
$expectedOutput = require __DIR__ . '/_files/schema_response_sdl_deprecated_annotation.php';
100+
101+
// checking field values deprecated reason
102+
$fieldDeprecatedReason = [];
103+
$fieldsArray = $expectedOutput[0]['fields'];
104+
foreach ($fieldsArray as $field) {
105+
if ($field['isDeprecated'] === true) {
106+
$fieldDeprecatedReason [] = $field['deprecationReason'];
107+
}
108+
}
109+
$this->assertNotEmpty($fieldDeprecatedReason);
110+
$this->assertContains(
111+
'Symbol was missed. Use `default_display_currency_code`.',
112+
$fieldDeprecatedReason
113+
);
114+
115+
$this->assertContains(
116+
'Symbol was missed. Use `default_display_currency_code`.',
117+
$fieldsValueReasonArray
118+
);
119+
120+
$this->assertNotEmpty(
121+
array_intersect($fieldDeprecatedReason, $fieldsValueReasonArray)
122+
);
123+
124+
// checking enum values deprecated reason
125+
$enumValueDeprecatedReason = [];
126+
$enumValuesArray = $expectedOutput[1]['enumValues'];
127+
foreach ($enumValuesArray as $enumValue) {
128+
if ($enumValue['isDeprecated'] === true) {
129+
$enumValueDeprecatedReason [] = $enumValue['deprecationReason'];
130+
}
131+
}
132+
$this->assertNotEmpty($enumValueDeprecatedReason);
133+
$this->assertContains(
134+
'`sample_url` serves to get the downloadable sample',
135+
$enumValueDeprecatedReason
136+
);
137+
$this->assertContains(
138+
'`sample_url` serves to get the downloadable sample',
139+
$enumValueReasonArray
140+
);
141+
$this->assertNotEmpty(
142+
array_intersect($enumValueDeprecatedReason, $enumValueReasonArray)
143+
);
144+
}
145+
146+
/**
147+
* Get the enum values deprecated reasons from the schema
148+
*
149+
* @param array $schemaResponseFields
150+
* @return array
151+
*/
152+
private function getEnumValueDeprecatedReason($schemaResponseFields): array
153+
{
154+
$enumValueReasonArray = [];
155+
foreach ($schemaResponseFields as $schemaResponseField) {
156+
if (!empty($schemaResponseField['enumValues'])) {
157+
foreach ($schemaResponseField['enumValues'] as $enumValueDeprecationReasonArray) {
158+
if (!empty($enumValueDeprecationReasonArray['deprecationReason'])) {
159+
$enumValueReasonArray[] = $enumValueDeprecationReasonArray['deprecationReason'];
160+
}
161+
}
162+
}
163+
}
164+
return $enumValueReasonArray;
165+
}
166+
167+
/**
168+
* Get the fields values deprecated reasons from the schema
169+
*
170+
* @param array $schemaResponseFields
171+
* @return array
172+
*/
173+
private function getFieldsValueDeprecatedReason($schemaResponseFields): array
174+
{
175+
$fieldsValueReasonArray = [];
176+
foreach ($schemaResponseFields as $schemaResponseField) {
177+
if (!empty($schemaResponseField['fields'])) {
178+
foreach ($schemaResponseField['fields'] as $fieldsValueDeprecatedReasonArray) {
179+
if (!empty($fieldsValueDeprecatedReasonArray['deprecationReason'])) {
180+
$fieldsValueReasonArray[] = $fieldsValueDeprecatedReasonArray['deprecationReason'];
181+
}
182+
}
183+
}
184+
}
185+
return $fieldsValueReasonArray;
186+
}
59187
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
return [
9+
[
10+
'kind'=> 'OBJECT',
11+
'name'=> 'Currency',
12+
'description'=> '',
13+
'fields'=> [
14+
[
15+
'name'=> 'available_currency_codes',
16+
'description'=> null,
17+
'isDeprecated'=> false,
18+
'deprecationReason'=> null
19+
],
20+
[
21+
'name'=> 'base_currency_code',
22+
'description'=> null,
23+
'isDeprecated'=> false,
24+
'deprecationReason'=> null
25+
],
26+
27+
[
28+
'name'=> 'base_currency_symbol',
29+
'description'=> null,
30+
'isDeprecated'=> false,
31+
'deprecationReason'=> null
32+
],
33+
[
34+
'name'=> 'default_display_currecy_code',
35+
'description'=> null,
36+
'isDeprecated'=> true,
37+
'deprecationReason'=> 'Symbol was missed. Use `default_display_currency_code`.'
38+
],
39+
[
40+
'name'=> 'default_display_currecy_symbol',
41+
'description'=> null,
42+
'isDeprecated'=> true,
43+
'deprecationReason'=> 'Symbol was missed. Use `default_display_currency_code`.'
44+
],
45+
[
46+
'name'=> 'default_display_currency_code',
47+
'description'=> null,
48+
'isDeprecated'=> false,
49+
'deprecationReason'=> null
50+
],
51+
[
52+
'name'=> 'default_display_currency_symbol',
53+
'description'=> null,
54+
'isDeprecated'=> false,
55+
'deprecationReason'=> null
56+
],
57+
[
58+
'name'=> 'exchange_rates',
59+
'description'=> null,
60+
'isDeprecated'=> false,
61+
'deprecationReason'=> null
62+
],
63+
64+
],
65+
'enumValues'=> null
66+
],
67+
[
68+
'kind' => 'ENUM',
69+
'name' => 'DownloadableFileTypeEnum',
70+
'description' => '',
71+
'fields' => null,
72+
'enumValues' => [
73+
[
74+
'name' => 'FILE',
75+
'description' => '',
76+
'isDeprecated' => true,
77+
'deprecationReason' => 'sample_url` serves to get the downloadable sample'
78+
],
79+
[
80+
'name' => 'URL',
81+
'description' => '',
82+
'isDeprecated' => true,
83+
'deprecationReason' => '`sample_url` serves to get the downloadable sample'
84+
]
85+
],
86+
'possibleTypes' => null
87+
],
88+
];

dev/tests/integration/testsuite/Magento/Framework/GraphQl/Config/GraphQlReaderTest.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -200,11 +200,14 @@ enumValues(includeDeprecated: true) {
200200
$sortFields = ['inputFields', 'fields'];
201201
foreach ($sortFields as $sortField) {
202202
isset($searchTerm[$sortField]) && is_array($searchTerm[$sortField])
203-
? usort($searchTerm[$sortField], function ($a, $b) {
204-
$cmpField = 'name';
205-
return isset($a[$cmpField]) && isset($b[$cmpField])
203+
? usort(
204+
$searchTerm[$sortField],
205+
function ($a, $b) {
206+
$cmpField = 'name';
207+
return isset($a[$cmpField]) && isset($b[$cmpField])
206208
? strcmp($a[$cmpField], $b[$cmpField]) : 0;
207-
}) : null;
209+
}
210+
) : null;
208211
}
209212

210213
$this->assertTrue(

dev/tests/static/framework/Magento/CodeMessDetector/Rule/Design/CookieAndSessionMisuse.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
/**
1818
* Session and Cookies must be used only in HTML Presentation layer.
19+
*
20+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1921
*/
2022
class CookieAndSessionMisuse extends AbstractRule implements ClassAware
2123
{
@@ -67,6 +69,19 @@ private function isLayoutProcessor(\ReflectionClass $class): bool
6769
);
6870
}
6971

72+
/**
73+
* Is given class a View Model?
74+
*
75+
* @param \ReflectionClass $class
76+
* @return bool
77+
*/
78+
private function isViewModel(\ReflectionClass $class): bool
79+
{
80+
return $class->isSubclassOf(
81+
\Magento\Framework\View\Element\Block\ArgumentInterface::class
82+
);
83+
}
84+
7085
/**
7186
* Is given class an HTML UI Document?
7287
*
@@ -191,6 +206,7 @@ public function apply(AbstractNode $node)
191206
&& !$this->isControllerPlugin($class)
192207
&& !$this->isBlockPlugin($class)
193208
&& !$this->isLayoutProcessor($class)
209+
&& !$this->isViewModel($class)
194210
) {
195211
$this->addViolation($node, [$node->getFullQualifiedName()]);
196212
}

lib/internal/Magento/Framework/GraphQl/Config/Element/Argument.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ class Argument implements FieldInterface
5454
*/
5555
private $defaultValue;
5656

57+
/**
58+
* @var array
59+
*/
60+
private $deprecated;
61+
5762
/**
5863
* @param string $name
5964
* @param string $type
@@ -64,6 +69,8 @@ class Argument implements FieldInterface
6469
* @param string $itemType
6570
* @param bool $itemsRequired
6671
* @param string $defaultValue
72+
* @param array $deprecated
73+
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
6774
*/
6875
public function __construct(
6976
string $name,
@@ -74,7 +81,8 @@ public function __construct(
7481
bool $isList,
7582
string $itemType = '',
7683
bool $itemsRequired = false,
77-
string $defaultValue = null
84+
string $defaultValue = null,
85+
array $deprecated = []
7886
) {
7987
$this->name = $name;
8088
$this->type = $isList ? $itemType : $type;
@@ -84,6 +92,7 @@ public function __construct(
8492
$this->isList = $isList;
8593
$this->itemsRequired = $itemsRequired;
8694
$this->defaultValue = $defaultValue;
95+
$this->deprecated = $deprecated;
8796
}
8897

8998
/**
@@ -175,4 +184,14 @@ public function hasDefaultValue() : bool
175184
{
176185
return $this->defaultValue ? true: false;
177186
}
187+
188+
/**
189+
* Return the deprecated
190+
*
191+
* @return array
192+
*/
193+
public function getDeprecated() : array
194+
{
195+
return $this->deprecated;
196+
}
178197
}

lib/internal/Magento/Framework/GraphQl/Config/Element/ArgumentFactory.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
use Magento\Framework\ObjectManagerInterface;
1111

1212
/**
13-
* {@inheritdoc}
13+
* @inheritdoc
1414
*/
1515
class ArgumentFactory
1616
{
@@ -51,7 +51,8 @@ public function createFromConfigData(
5151
'isList' => isset($argumentData['itemType']),
5252
'itemType' => isset($argumentData['itemType']) ? $argumentData['itemType'] : '',
5353
'itemsRequired' => isset($argumentData['itemsRequired']) ? $argumentData['itemsRequired'] : false,
54-
'defaultValue' => isset($argumentData['defaultValue']) ? $argumentData['defaultValue'] : null
54+
'defaultValue' => isset($argumentData['defaultValue']) ? $argumentData['defaultValue'] : null,
55+
'deprecated' => isset($argumentData['deprecated']) ? $argumentData['deprecated'] : [],
5556
]
5657
);
5758
}

lib/internal/Magento/Framework/GraphQl/Config/Element/EnumFactory.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
use Magento\Framework\ObjectManagerInterface;
1313

1414
/**
15-
* {@inheritdoc}
15+
* @inheritdoc
1616
*/
1717
class EnumFactory implements ConfigElementFactoryInterface
1818
{
@@ -71,7 +71,8 @@ public function createFromConfigData(array $data): ConfigElementInterface
7171
$values[$item['_value']] = $this->enumValueFactory->create(
7272
$item['name'],
7373
$item['_value'],
74-
isset($item['description']) ? $item['description'] : ''
74+
isset($item['description']) ? $item['description'] : '',
75+
isset($item['deprecationReason']) ? $item['deprecationReason'] : ''
7576
);
7677
}
7778
return $this->create(

0 commit comments

Comments
 (0)