Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
soyuka committed May 10, 2024
1 parent 2edf04c commit 4e76fbc
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 1 deletion.
15 changes: 15 additions & 0 deletions features/main/input_output.feature
@@ -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/State/Processor/SerializeProcessor.php
Expand Up @@ -52,7 +52,7 @@ public function process(mixed $data, Operation $operation, array $uriVariables =
$context['original_data'] = $data;

$class = $operation->getClass();
if ($request->attributes->get('_api_resource_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.');
}
Expand Down
@@ -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)];
}
}
@@ -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
{
}
@@ -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
@@ -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;
}

0 comments on commit 4e76fbc

Please sign in to comment.