Skip to content

Commit 8d1e1d2

Browse files
authored
fix(upgrade): use Link for uri variables definition (#4683)
1 parent 609aab2 commit 8d1e1d2

File tree

2 files changed

+32
-29
lines changed

2 files changed

+32
-29
lines changed

src/Core/Upgrade/UpgradeApiSubresourceVisitor.php

+31-28
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use ApiPlatform\Metadata\ApiResource;
1919
use ApiPlatform\Metadata\Get;
2020
use ApiPlatform\Metadata\GetCollection;
21+
use ApiPlatform\Metadata\Link;
2122
use PhpParser\Node;
2223
use PhpParser\NodeVisitorAbstract;
2324
use ReflectionClass;
@@ -89,6 +90,13 @@ public function enterNode(Node $node)
8990
ApiResource::class
9091
)
9192
),
93+
]),
94+
new Node\Stmt\Use_([
95+
new Node\Stmt\UseUse(
96+
new Node\Name(
97+
Link::class
98+
)
99+
),
92100
])
93101
);
94102
}
@@ -99,62 +107,44 @@ public function enterNode(Node $node)
99107

100108
foreach ($this->subresourceMetadata['uri_variables'] as $identifier => $resource) {
101109
$identifierNodes = [
102-
new Node\Expr\ArrayItem(
103-
new Node\Expr\ClassConstFetch(
110+
'fromClass' => new Node\Expr\ClassConstFetch(
104111
new Node\Name(
105112
($resource['from_class'] === $this->subresourceMetadata['resource_class']) ? 'self' : '\\'.$resource['from_class']
106113
),
107114
'class'
108115
),
109-
new Node\Scalar\String_('from_class')
110-
),
111-
new Node\Expr\ArrayItem(
112-
new Node\Expr\Array_(
116+
'identifiers' => new Node\Expr\Array_(
113117
isset($resource['identifiers'][0]) ? [
114118
new Node\Expr\ArrayItem(new Node\Scalar\String_($resource['identifiers'][0])),
115119
] : [],
116-
['kind' => Node\Expr\Array_::KIND_SHORT]),
117-
new Node\Scalar\String_('identifiers')
118-
),
120+
['kind' => Node\Expr\Array_::KIND_SHORT]
121+
),
119122
];
120123

121124
if (isset($resource['expanded_value'])) {
122-
$identifierNodes[] = new Node\Expr\ArrayItem(
123-
new Node\Scalar\String_('expanded_value'),
124-
new Node\Scalar\String_($resource['expanded_value'])
125-
);
125+
$identifierNodes['expandedValue'] = new Node\Scalar\String_($resource['expanded_value']);
126126
}
127127

128128
if (isset($resource['from_property']) || isset($resource['to_property'])) {
129-
$identifierNodes[] = new Node\Expr\ArrayItem(
130-
new Node\Scalar\String_($resource['to_property'] ?? $resource['from_property']),
131-
new Node\Scalar\String_(isset($resource['to_property']) ? 'to_property' : 'from_property')
132-
);
129+
$identifierNodes[isset($resource['to_property']) ? 'toProperty' : 'fromProperty'] = new Node\Scalar\String_($resource['to_property'] ?? $resource['from_property']);
133130
}
134131

135-
$identifiersNodeItems[] = new Node\Expr\ArrayItem(
136-
new Node\Expr\Array_(
137-
$identifierNodes,
138-
[
139-
'kind' => Node\Expr\Array_::KIND_SHORT,
140-
]
141-
),
132+
$identifierNodeItems[] = new Node\Expr\ArrayItem(
133+
new Node\Expr\New_(new Node\Name('Link'), $this->arrayToArguments($identifierNodes)),
142134
new Node\Scalar\String_($identifier)
143135
);
144136
}
145137

146-
$identifiersNode = new Node\Expr\Array_($identifiersNodeItems, ['kind' => Node\Expr\Array_::KIND_SHORT]);
147-
148138
$arguments = [
149139
new Node\Arg(
150-
new Node\Scalar\String_(str_replace('.{_format}', '', $this->subresourceMetadata['path'])),
140+
new Node\Scalar\String_($this->subresourceMetadata['path']),
151141
false,
152142
false,
153143
[],
154144
new Node\Identifier('uriTemplate')
155145
),
156146
new Node\Arg(
157-
$identifiersNode,
147+
new Node\Expr\Array_($identifierNodeItems, ['kind' => Node\Expr\Array_::KIND_SHORT]),
158148
false,
159149
false,
160150
[],
@@ -280,4 +270,17 @@ public function enterNode(Node $node)
280270
$node->attrGroups[] = $apiResourceAttribute;
281271
}
282272
}
273+
274+
/**
275+
* @return Node\Arg[]
276+
*/
277+
private function arrayToArguments(array $arguments)
278+
{
279+
$args = [];
280+
foreach ($arguments as $key => $value) {
281+
$args[] = new Node\Arg($value, false, false, [], new Node\Identifier($key));
282+
}
283+
284+
return $args;
285+
}
283286
}

tests/Core/Bridge/Symfony/Bundle/Command/UpgradeApiResourceCommandTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public function testDebugResource()
9292
'+use ApiPlatform\\Metadata\\ApiResource',
9393
'+use ApiPlatform\\Metadata\\Get',
9494
"+#[ApiResource(graphQlOperations: [new Query(name: 'item_query'), new Mutation(name: 'update', normalizationContext: ['groups' => ['chicago', 'fakemanytomany']], denormalizationContext: ['groups' => ['friends']])], types: ['https://schema.org/Product'], normalizationContext: ['groups' => ['friends']], filters: ['related_dummy.friends', 'related_dummy.complex_sub_query'])]",
95-
"+#[ApiResource(uriTemplate: '/related_dummies/{id}/id', uriVariables: ['id' => ['from_class' => self::class, 'identifiers' => ['id']]], status: 200, types: ['https://schema.org/Product'], filters: ['related_dummy.friends', 'related_dummy.complex_sub_query'], normalizationContext: ['groups' => ['friends']], operations: [new Get()])]",
95+
"#[ApiResource(uriTemplate: '/related_dummies/{id}/id.{_format}', uriVariables: ['id' => new Link(fromClass: self::class, identifiers: ['id'])], status: 200, types: ['https://schema.org/Product'], filters: ['related_dummy.friends', 'related_dummy.complex_sub_query'], normalizationContext: ['groups' => ['friends']], operations: [new Get()])]",
9696
];
9797

9898
$display = $commandTester->getDisplay();

0 commit comments

Comments
 (0)