Skip to content

Commit 6e482c0

Browse files
committed
test: #7061
1 parent dbb7cd5 commit 6e482c0

File tree

5 files changed

+59
-4
lines changed

5 files changed

+59
-4
lines changed

src/Metadata/Resource/Factory/UriTemplateResourceMetadataCollectionFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ private function normalizeUriVariables(ApiResource|HttpOperation $operation): Ap
235235
}
236236

237237
$normalizedUriVariable = $normalizedUriVariable->withParameterName($normalizedParameterName);
238-
$normalizedUriVariables[$normalizedParameterName] = $normalizedUriVariable;
238+
$normalizedUriVariables[$normalizedParameterName] = $normalizedUriVariable->withKey($normalizedParameterName);
239239
}
240240

241241
return $operation->withUriVariables($normalizedUriVariables);

src/State/ParameterProvider/ReadLinkParameterProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public function provide(Parameter $parameter, array $parameters = [], array $con
6969

7070
foreach ($value as $v) {
7171
try {
72-
$r = $relation[] = $this->locator->provide($linkOperation, $this->getUriVariables($v, $parameter, $linkOperation), $context);
72+
$relation[] = $this->locator->provide($linkOperation, $this->getUriVariables($v, $parameter, $linkOperation), $context);
7373
} catch (ProviderNotFoundException) {
7474
}
7575
}

tests/Fixtures/TestBundle/Entity/Company.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,17 @@
1717
use ApiPlatform\Metadata\Get;
1818
use ApiPlatform\Metadata\GetCollection;
1919
use ApiPlatform\Metadata\Link;
20+
use ApiPlatform\Metadata\NotExposed;
21+
use ApiPlatform\Metadata\Operation;
2022
use ApiPlatform\Metadata\Post;
2123
use Doctrine\ORM\Mapping as ORM;
2224
use Symfony\Component\Serializer\Annotation\Groups;
2325

26+
#[NotExposed(
27+
uriTemplate: '/company-by-name/{name}',
28+
provider: [self::class, 'provideCompanyByName'],
29+
uriVariables: ['name']
30+
)]
2431
#[ApiResource]
2532
#[GetCollection]
2633
#[Get]
@@ -34,6 +41,14 @@
3441
#[ORM\Entity]
3542
class Company
3643
{
44+
public static function provideCompanyByName(Operation $operation, array $uriVariables): object
45+
{
46+
$c = new self();
47+
$c->setName($uriVariables['name']);
48+
49+
return $c;
50+
}
51+
3752
/**
3853
* @var int|null The id
3954
*/

tests/Fixtures/TestBundle/Entity/Employee.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use ApiPlatform\Metadata\ApiResource;
1717
use ApiPlatform\Metadata\Get;
1818
use ApiPlatform\Metadata\GetCollection;
19+
use ApiPlatform\Metadata\Link;
1920
use ApiPlatform\Metadata\Post;
2021
use Doctrine\ORM\Mapping as ORM;
2122
use Symfony\Component\Serializer\Annotation\Groups;
@@ -38,6 +39,18 @@
3839
normalizationContext: ['groups' => ['company_employees_read']]
3940
)]
4041
#[GetCollection]
42+
#[GetCollection(
43+
uriTemplate: '/companies-by-name/{name}/employees',
44+
uriVariables: [
45+
'name' => new Link(
46+
identifiers: ['name'],
47+
fromClass: Company::class,
48+
toProperty: 'company',
49+
security: 'company.name == "Test"',
50+
extraProperties: ['uri_template' => '/company-by-name/{name}']
51+
),
52+
],
53+
)]
4154
#[ORM\Entity]
4255
class Employee
4356
{

tests/Functional/Parameters/LinkProviderParameterTest.php

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515

1616
use ApiPlatform\Symfony\Bundle\Test\ApiTestCase;
1717
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\WithParameter;
18+
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Company;
1819
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Dummy;
20+
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Employee;
1921
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\RelatedDummy;
2022
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\RelatedOwnedDummy;
2123
use ApiPlatform\Tests\RecreateSchemaTrait;
@@ -33,15 +35,15 @@ final class LinkProviderParameterTest extends ApiTestCase
3335
*/
3436
public static function getResources(): array
3537
{
36-
return [WithParameter::class, Dummy::class];
38+
return [WithParameter::class, Dummy::class, Employee::class, Company::class];
3739
}
3840

3941
/**
4042
* @throws \Throwable
4143
*/
4244
protected function setUp(): void
4345
{
44-
$this->recreateSchema([Dummy::class, RelatedOwnedDummy::class, RelatedDummy::class]);
46+
$this->recreateSchema([Dummy::class, RelatedOwnedDummy::class, RelatedDummy::class, Employee::class, Company::class]);
4547
}
4648

4749
public function testReadDummyProviderFromQueryParameter(): void
@@ -107,4 +109,29 @@ public function testReadDummyProviderFromQueryParameterNoNotFound(): void
107109
$response = self::createClient()->request('GET', '/with_parameters_links_no_not_found?dummy=1');
108110
self::assertEquals(200, $response->getStatusCode());
109111
}
112+
113+
/**
114+
* See https://github.com/api-platform/core/issues/7061.
115+
*/
116+
public function testLinkSecurityWithSlug(): void
117+
{
118+
$manager = $this->getManager();
119+
$employee = new Employee();
120+
$employee->setName('me');
121+
$dummy = new Company();
122+
$dummy->setName('Test');
123+
$employee->setCompany($dummy);
124+
$manager->persist($employee);
125+
$manager->persist($dummy);
126+
$manager->flush();
127+
128+
$container = static::getContainer();
129+
if ('mongodb' === $container->getParameter('kernel.environment')) {
130+
$this->markTestSkipped();
131+
}
132+
133+
$response = self::createClient()->request('GET', '/companies-by-name/Test/employees');
134+
dd($response->toArray(false));
135+
self::assertEquals(200, $response->getStatusCode());
136+
}
110137
}

0 commit comments

Comments
 (0)