Skip to content

Commit a1fe018

Browse files
committed
fix(metadata): keep configured uri variables
fixes #5184
1 parent 84a7e56 commit a1fe018

File tree

3 files changed

+50
-12
lines changed

3 files changed

+50
-12
lines changed

features/main/attribute_resource.feature

+10-12
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1+
@php8
2+
@!mysql
3+
@!mongodb
14
Feature: Resource attributes
25
In order to use the Resource attribute
36
As a developer
47
I should be able to fetch data from a state provider
58

6-
@php8
7-
@!mysql
8-
@!mongodb
99
Scenario: Retrieve a Resource collection
1010
When I add "Content-Type" header equal to "application/ld+json"
1111
And I send a "GET" request to "/attribute_resources"
@@ -35,9 +35,6 @@ Feature: Resource attributes
3535
}
3636
"""
3737

38-
@php8
39-
@!mysql
40-
@!mongodb
4138
Scenario: Retrieve the first resource
4239
When I add "Content-Type" header equal to "application/ld+json"
4340
And I send a "GET" request to "/attribute_resources/1"
@@ -55,9 +52,6 @@ Feature: Resource attributes
5552
}
5653
"""
5754

58-
@php8
59-
@!mysql
60-
@!mongodb
6155
Scenario: Retrieve the aliased resource
6256
When I add "Content-Type" header equal to "application/ld+json"
6357
And I send a "GET" request to "/dummy/1/attribute_resources/2"
@@ -77,9 +71,6 @@ Feature: Resource attributes
7771
}
7872
"""
7973

80-
@php8
81-
@!mysql
82-
@!mongodb
8374
Scenario: Patch the aliased resource
8475
When I add "Content-Type" header equal to "application/merge-patch+json"
8576
And I send a "PATCH" request to "/dummy/1/attribute_resources/2" with body:
@@ -101,3 +92,10 @@ Feature: Resource attributes
10192
"name": "Patched"
10293
}
10394
"""
95+
96+
Scenario: Uri variables should be configured properly
97+
When I send a "GET" request to "/photos/1/resize/300/100"
98+
Then the response status code should be 400
99+
And the response should be in JSON
100+
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
101+
And the JSON node "hydra:description" should be equal to 'Unable to generate an IRI for the item of type "ApiPlatform\Tests\Fixtures\TestBundle\Entity\UriVariableConfigured"'

src/Metadata/Resource/Factory/UriTemplateResourceMetadataCollectionFactory.php

+6
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,9 @@ private function configureUriVariables(ApiResource|HttpOperation $operation): Ap
137137
return $this->normalizeUriVariables($operation);
138138
}
139139

140+
$hasUserConfiguredUriVariables = true;
140141
if (!$operation->getUriVariables()) {
142+
$hasUserConfiguredUriVariables = false;
141143
$operation = $operation->withUriVariables($this->transformLinksToUriVariables($this->linkFactory->createLinksFromIdentifiers($operation)));
142144
}
143145

@@ -164,6 +166,10 @@ private function configureUriVariables(ApiResource|HttpOperation $operation): Ap
164166
$variables = $route->getPathVariables();
165167

166168
if (\count($variables) !== \count($uriVariables)) {
169+
if ($hasUserConfiguredUriVariables) {
170+
return $operation;
171+
}
172+
167173
$newUriVariables = [];
168174
foreach ($variables as $variable) {
169175
if (isset($uriVariables[$variable])) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Tests\Fixtures\TestBundle\Entity;
15+
16+
use ApiPlatform\Metadata\ApiResource;
17+
use ApiPlatform\Metadata\Operation;
18+
19+
#[ApiResource(uriTemplate: '/photos/{id}/resize/{width}/{height}', uriVariables: ['id'], provider: [UriVariableConfigured::class, 'provide'])]
20+
final class IncompleteUriVariableConfigured
21+
{
22+
public function __construct(public string $id)
23+
{
24+
}
25+
26+
public static function provide(Operation $operation, array $uriVariables = [], array $context = []): self
27+
{
28+
if (isset($uriVariables['width'])) {
29+
throw new \LogicException('URI variable "width" should not exist');
30+
}
31+
32+
return new self($uriVariables['id']);
33+
}
34+
}

0 commit comments

Comments
 (0)