Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(symfony): no read should not throw on wrong uri variables #6359

Merged
merged 2 commits into from
May 10, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 15 additions & 0 deletions features/main/input_output.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Feature: DTO input and output
In order to use a hypermedia API
As a client software developer
I need to be able to use DTOs on my resources as Input or Output objects.

Background:
Given I add "Accept" header equal to "application/ld+json"
And I add "Content-Type" header equal to "application/ld+json"

@!mongodb
Scenario: Fetch a collection of outputs with an entityClass as state option
When I send a "GET" request to "/output_and_entity_classes"
And the JSON node "hydra:member[0].@type" should be equal to "OutputAndEntityClassEntity"


2 changes: 1 addition & 1 deletion src/Serializer/SerializerContextBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public function createFromRequest(Request $request, bool $normalization, ?array
}
}

if (($options = $operation?->getStateOptions()) && $options instanceof Options && $options->getEntityClass()) {
if (null === $context['output'] && ($options = $operation?->getStateOptions()) && $options instanceof Options && $options->getEntityClass()) {
$context['force_resource_class'] = $operation->getClass();
}

Expand Down
8 changes: 7 additions & 1 deletion src/State/Processor/SerializeProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,14 @@ public function process(mixed $data, Operation $operation, array $uriVariables =
// @see ApiPlatform\State\Processor\RespondProcessor
$context['original_data'] = $data;

$class = $operation->getClass();
if ($request->attributes->get('_api_resource_class') && $request->attributes->get('_api_resource_class') !== $operation->getClass()) {
$class = $request->attributes->get('_api_resource_class');
trigger_deprecation('api-platform/core', '3.3', 'The resource class on the router is not the same as the operation\'s class which leads to wrong behaviors. Prefer using "stateOptions" if you need to change the entity class.');
}

$serializerContext = $this->serializerContextBuilder->createFromRequest($request, true, [
'resource_class' => $operation->getClass(),
'resource_class' => $class,
'operation' => $operation,
]);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?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\Issue6358;

use ApiPlatform\Doctrine\Orm\State\Options;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\GetCollection;

#[ApiResource(
operations: [
new GetCollection(
output: OutputAndEntityClassDto::class,
provider: [self::class, 'provide'],
stateOptions: new Options(entityClass: OutputAndEntityClassEntity::class)
),
],
)]
class OutputAndEntityClass
{
public static function provide(): array
{
return [new OutputAndEntityClassEntity(1)];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?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\Issue6358;

class OutputAndEntityClassDto
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?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\Issue6358;

use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity]
class OutputAndEntityClassEntity
{
public function __construct(
#[ORM\Id]
#[ORM\Column(type: 'integer')]
#[ORM\GeneratedValue(strategy: 'AUTO')]
public readonly ?int $id = null
) {
}
}
22 changes: 22 additions & 0 deletions tests/Fixtures/TestBundle/Entity/IdentifierShortcut.php
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;

use ApiPlatform\Metadata\Patch;

#[Patch(uriTemplate: '/identifiers_shortcut/{id}', uriVariables: [self::class, 'id'])]
class IdentifierShortcut
{
public $id;
}