-
-
Notifications
You must be signed in to change notification settings - Fork 869
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(jsonld): remove request uri variables when denormalizing output (#…
- Loading branch information
Showing
7 changed files
with
225 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?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\Entity\Issue6465; | ||
|
||
use ApiPlatform\Metadata\ApiResource; | ||
use Doctrine\ORM\Mapping as ORM; | ||
|
||
#[ORM\Entity()] | ||
#[ApiResource(shortName: 'Bar6465')] | ||
#[ORM\Table(name: 'bar6465')] | ||
class Bar | ||
{ | ||
#[ORM\Id] | ||
#[ORM\GeneratedValue] | ||
#[ORM\Column] | ||
public ?int $id = null; | ||
|
||
#[ORM\Column] | ||
public string $title = ''; | ||
} |
22 changes: 22 additions & 0 deletions
22
tests/Fixtures/TestBundle/Entity/Issue6465/CustomInput.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?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\Entity\Issue6465; | ||
|
||
use Symfony\Component\Validator\Constraints\NotBlank; | ||
|
||
class CustomInput | ||
{ | ||
#[NotBlank] | ||
public Bar $bar; | ||
} |
21 changes: 21 additions & 0 deletions
21
tests/Fixtures/TestBundle/Entity/Issue6465/CustomOutput.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?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\Entity\Issue6465; | ||
|
||
class CustomOutput | ||
{ | ||
public function __construct(public string $title) | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?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\Entity\Issue6465; | ||
|
||
use ApiPlatform\Metadata\ApiResource; | ||
use ApiPlatform\Metadata\Get; | ||
use ApiPlatform\Metadata\GetCollection; | ||
use ApiPlatform\Metadata\Link; | ||
use ApiPlatform\Metadata\Post; | ||
use Doctrine\ORM\Mapping as ORM; | ||
|
||
#[ORM\Entity()] | ||
#[ORM\Table(name: 'foo6465')] | ||
#[ApiResource( | ||
shortName: 'Foo6465', | ||
operations: [ | ||
new Get(), | ||
new GetCollection(), | ||
new Post(), | ||
new Post( | ||
uriTemplate: '/foo/{id}/validate', | ||
uriVariables: ['id' => new Link(fromClass: Foo::class)], | ||
status: 200, | ||
input: CustomInput::class, | ||
output: CustomOutput::class, | ||
processor: [self::class, 'process'], | ||
), | ||
] | ||
)] | ||
class Foo | ||
{ | ||
#[ORM\Id] | ||
#[ORM\GeneratedValue] | ||
#[ORM\Column] | ||
public ?int $id = null; | ||
|
||
#[ORM\Column(length: 255)] | ||
public ?string $title = null; | ||
|
||
/** | ||
* @param CustomInput $data | ||
*/ | ||
public static function process($data): CustomOutput | ||
{ | ||
return new CustomOutput($data->bar->title); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
<?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\Functional; | ||
|
||
use ApiPlatform\Symfony\Bundle\Test\ApiTestCase; | ||
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Issue6465\Bar; | ||
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Issue6465\Foo; | ||
use Doctrine\ORM\EntityManagerInterface; | ||
use Doctrine\ORM\Tools\SchemaTool; | ||
|
||
class JsonLd extends ApiTestCase | ||
{ | ||
/** | ||
* The input DTO denormalizes an existing Doctrine entity. | ||
*/ | ||
public function testIssue6465(): void | ||
{ | ||
$container = static::getContainer(); | ||
if ('mongodb' === $container->getParameter('kernel.environment')) { | ||
$this->markTestSkipped(); | ||
} | ||
|
||
$response = self::createClient()->request('POST', '/foo/1/validate', [ | ||
'json' => ['bar' => '/bar6465s/2'], | ||
]); | ||
|
||
$res = $response->toArray(); | ||
$this->assertEquals('Bar two', $res['title']); | ||
} | ||
|
||
protected function setUp(): void | ||
{ | ||
self::bootKernel(); | ||
|
||
$container = static::getContainer(); | ||
$registry = $container->get('doctrine'); | ||
$manager = $registry->getManager(); | ||
if (!$manager instanceof EntityManagerInterface) { | ||
return; | ||
} | ||
|
||
$classes = []; | ||
foreach ([Foo::class, Bar::class] as $entityClass) { | ||
$classes[] = $manager->getClassMetadata($entityClass); | ||
} | ||
|
||
$schemaTool = new SchemaTool($manager); | ||
@$schemaTool->createSchema($classes); | ||
|
||
$foo = new Foo(); | ||
$foo->title = 'Foo'; | ||
$manager->persist($foo); | ||
$bar = new Bar(); | ||
$bar->title = 'Bar one'; | ||
$manager->persist($bar); | ||
$bar2 = new Bar(); | ||
$bar2->title = 'Bar two'; | ||
$manager->persist($bar2); | ||
$manager->flush(); | ||
} | ||
|
||
protected function tearDown(): void | ||
{ | ||
$container = static::getContainer(); | ||
$registry = $container->get('doctrine'); | ||
$manager = $registry->getManager(); | ||
if (!$manager instanceof EntityManagerInterface) { | ||
return; | ||
} | ||
|
||
$classes = []; | ||
foreach ([Foo::class, Bar::class] as $entityClass) { | ||
$classes[] = $manager->getClassMetadata($entityClass); | ||
} | ||
|
||
$schemaTool = new SchemaTool($manager); | ||
@$schemaTool->dropSchema($classes); | ||
parent::tearDown(); | ||
} | ||
} |