Skip to content
Open
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
5 changes: 4 additions & 1 deletion src/State/Provider/ObjectMapperProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace ApiPlatform\State\Provider;

use ApiPlatform\Metadata\HttpOperation;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\Metadata\Util\CloneTrait;
use ApiPlatform\State\Pagination\MappedObjectPaginator;
Expand Down Expand Up @@ -41,7 +42,9 @@ public function __construct(
public function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null
{
$data = $this->decorated->provide($operation, $uriVariables, $context);
$class = $operation->getOutput()['class'] ?? $operation->getClass();
$class = $operation instanceof HttpOperation && !\in_array($operation->getMethod(), [HttpOperation::METHOD_GET, HttpOperation::METHOD_HEAD, HttpOperation::METHOD_OPTIONS, HttpOperation::METHOD_QUERY], true)
? $operation->getClass()
: ($operation->getOutput()['class'] ?? $operation->getClass());

if (!$this->objectMapper || !$operation->canMap()) {
return $data;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?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\Doctrine\Orm\State\Options;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Delete;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\Patch;
use ApiPlatform\Tests\Fixtures\TestBundle\Dto\MappedResourceLevelOutputDto;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\MappedResourceLevelOutputEntity;
use Symfony\Component\ObjectMapper\Attribute\Map;

#[ApiResource(
stateOptions: new Options(entityClass: MappedResourceLevelOutputEntity::class),
output: MappedResourceLevelOutputDto::class,
operations: [new Get(), new Patch(), new Delete()],
)]
#[Map(target: MappedResourceLevelOutputEntity::class)]
final class MappedResourceLevelOutput
{
#[Map(if: false)]
public ?int $id = null;

public ?string $name = null;

public ?string $description = null;
}
21 changes: 21 additions & 0 deletions tests/Fixtures/TestBundle/Dto/MappedResourceLevelOutputDto.php
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\Dto;

final class MappedResourceLevelOutputDto
{
public ?int $id = null;

public string $name;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?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\Tests\Fixtures\TestBundle\ApiResource\MappedResourceLevelOutput;
use ApiPlatform\Tests\Fixtures\TestBundle\Dto\MappedResourceLevelOutputDto;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\ObjectMapper\Attribute\Map;

#[ORM\Entity]
#[Map(target: MappedResourceLevelOutput::class)]
#[Map(target: MappedResourceLevelOutputDto::class)]
class MappedResourceLevelOutputEntity
{
#[ORM\Column(type: 'integer')]
#[ORM\Id]
#[ORM\GeneratedValue(strategy: 'AUTO')]
private ?int $id = null;

#[ORM\Column]
private string $name;

#[ORM\Column]
private string $description;

public function getId(): ?int
{
return $this->id;
}

public function getName(): string
{
return $this->name;
}

public function setName(string $name): void
{
$this->name = $name;
}

public function getDescription(): string
{
return $this->description;
}

public function setDescription(string $description): void
{
$this->description = $description;
}
}
79 changes: 79 additions & 0 deletions tests/Functional/MappedResourceLevelOutputTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?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\Test\ApiTestCase;
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\MappedResourceLevelOutput;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\MappedResourceLevelOutputEntity;
use ApiPlatform\Tests\RecreateSchemaTrait;
use ApiPlatform\Tests\SetupClassResourcesTrait;

final class MappedResourceLevelOutputTest extends ApiTestCase
{
use RecreateSchemaTrait;
use SetupClassResourcesTrait;
protected static ?bool $alwaysBootKernel = false;

/**
* @return class-string[]
*/
public static function getResources(): array
{
return [MappedResourceLevelOutput::class];
}

public function testDeleteRemovesEntityWhenOutputIsDeclared(): void
{
$id = $this->persistEntity('before', 'kept');

self::createClient()->request('DELETE', '/mapped_resource_level_outputs/'.$id);

$this->assertResponseIsSuccessful();
$this->assertNull($this->getManager()->getRepository(MappedResourceLevelOutputEntity::class)->find($id));
}

public function testPatchKeepsUnsentFieldsWhenOutputIsDeclared(): void
{
$id = $this->persistEntity('before', 'kept');

self::createClient()->request('PATCH', '/mapped_resource_level_outputs/'.$id, [
'headers' => ['Content-Type' => 'application/merge-patch+json'],
'json' => ['name' => 'after'],
]);

$this->assertResponseIsSuccessful();
$entity = $this->getManager()->getRepository(MappedResourceLevelOutputEntity::class)->find($id);
$this->assertSame('after', $entity->getName());
$this->assertSame('kept', $entity->getDescription());
}

private function persistEntity(string $name, string $description): int
{
if ($this->isMongoDB()) {
$this->markTestSkipped('MongoDB not tested.');
}

$this->recreateSchema([MappedResourceLevelOutputEntity::class]);
$manager = $this->getManager();
$entity = new MappedResourceLevelOutputEntity();
$entity->setName($name);
$entity->setDescription($description);
$manager->persist($entity);
$manager->flush();
$id = $entity->getId();
$manager->clear();

return $id;
}
}
2 changes: 1 addition & 1 deletion tests/State/Provider/ObjectMapperProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ public function testProvideIgnoresInputClassAndMapsToOutputClass(): void
{
$sourceEntity = new SourceEntity();
$outputResource = new OutputResource();
$operation = new Patch(class: TargetResource::class, input: ['class' => InputResource::class], output: ['class' => OutputResource::class], map: true);
$operation = new Get(class: TargetResource::class, input: ['class' => InputResource::class], output: ['class' => OutputResource::class], map: true);
$objectMapper = $this->createMock(ObjectMapperInterface::class);
$objectMapper->expects($this->once())
->method('map')
Expand Down
Loading