Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/State/ParameterProvider/ReadLinkParameterProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ public function provide(Parameter $parameter, array $parameters = [], array $con

$context['request']?->attributes->set($securityObjectName, $relation);

if ($parameter instanceof Link) {
$uriVariables = $operation->getUriVariables();
$uriVariables[$parameter->getKey()] = $parameter;
$operation = $operation->withUriVariables($uriVariables);
}

return $operation;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Tests\Fixtures\TestBundle\ApiResource;

use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\HttpOperation;
use ApiPlatform\Metadata\Link;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\ParameterProvider\ReadLinkParameterProvider;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Dummy;

#[Get(
uriTemplate: '/link_parameter_provider_resources/{id}',
uriVariables: [
'id' => new Link(
provider: ReadLinkParameterProvider::class,
fromClass: Dummy::class
),
],
provider: [self::class, 'provide']
)]
class LinkParameterProviderResource
{
public string $id;
public Dummy $dummy;

/**
* @param HttpOperation $operation
*/
public static function provide(Operation $operation, array $uriVariables = [])
{
$d = new self();
$d->id = '1';
$d->dummy = $operation->getUriVariables()['id']->getValue();

return $d;
}
}
8 changes: 6 additions & 2 deletions tests/Fixtures/TestBundle/Entity/Employee.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
use ApiPlatform\Metadata\Post;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints\IdenticalTo;
use Symfony\Component\Validator\Constraints\Expression;

#[ApiResource]
#[Post]
Expand Down Expand Up @@ -49,7 +49,11 @@
toProperty: 'company',
security: 'company.name == "Test" or company.name == "NotTest"',
extraProperties: ['uri_template' => '/company-by-name/{name}'],
constraints: [new IdenticalTo('Test')]
constraints: [
new Expression(
'value.getName() == "Test"',
),
]
),
],
)]
Expand Down
22 changes: 21 additions & 1 deletion tests/Functional/Parameters/LinkProviderParameterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace ApiPlatform\Tests\Functional\Parameters;

use ApiPlatform\Symfony\Bundle\Test\ApiTestCase;
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\LinkParameterProviderResource;
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\WithParameter;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Company;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Dummy;
Expand All @@ -35,7 +36,7 @@
*/
public static function getResources(): array
{
return [WithParameter::class, Dummy::class, Employee::class, Company::class];
return [WithParameter::class, Dummy::class, Employee::class, Company::class, LinkParameterProviderResource::class];
}

/**
Expand Down Expand Up @@ -162,4 +163,23 @@
$response = self::createClient()->request('GET', '/companies-by-name/NotTest/employees');
self::assertEquals(422, $response->getStatusCode());
}

public function testUriVariableHasDummy(): void
{
if ('mongodb' === $container->getParameter('kernel.environment')) {

Check failure on line 169 in tests/Functional/Parameters/LinkProviderParameterTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 8.4)

Undefined variable: $container
$this->markTestSkipped();
}

$manager = $this->getManager();
$dummy = new Dummy();
$dummy->setName('hi');
$manager->persist($dummy);
$manager->flush();

self::createClient()->request('GET', '/link_parameter_provider_resources/'.$dummy->getId());

$this->assertJsonContains([
'dummy' => '/dummies/1',
]);
}
}
Loading