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
2 changes: 1 addition & 1 deletion src/State/Provider/ObjectMapperProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ 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->getInput()['class'] ?? $operation->getOutput()['class'] ?? $operation->getClass();

if (!$this->objectMapper || !$operation->canMap()) {
return $data;
Expand Down
47 changes: 47 additions & 0 deletions tests/State/Provider/ObjectMapperProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace ApiPlatform\Tests\State\Provider;

use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\Patch;
use ApiPlatform\State\Pagination\ArrayPaginator;
use ApiPlatform\State\Pagination\MappedObjectPaginator;
use ApiPlatform\State\Provider\ObjectMapperProvider;
Expand Down Expand Up @@ -168,6 +169,42 @@ public function testProvideMapsPaginator(): void
$this->assertSame($targetResource2, $items[1]);
}

public function testProvideMapsToInputClassWhenInputIsSet(): void
{
$sourceEntity = new SourceEntity();
$inputResource = new InputResource();
$operation = new Patch(class: TargetResource::class, input: ['class' => InputResource::class], output: ['class' => OutputResource::class], map: true);
$objectMapper = $this->createMock(ObjectMapperInterface::class);
$objectMapper->expects($this->once())
->method('map')
->with($sourceEntity, InputResource::class)
->willReturn($inputResource);
$decorated = $this->createStub(ProviderInterface::class);
$decorated->method('provide')->willReturn($sourceEntity);
$provider = new ObjectMapperProvider($objectMapper, $decorated);

$result = $provider->provide($operation);
$this->assertSame($inputResource, $result);
}

public function testProvideMapsToOutputClassWhenNoInput(): void
{
$sourceEntity = new SourceEntity();
$outputResource = new OutputResource();
$operation = new Get(class: TargetResource::class, output: ['class' => OutputResource::class], map: true);
$objectMapper = $this->createMock(ObjectMapperInterface::class);
$objectMapper->expects($this->once())
->method('map')
->with($sourceEntity, OutputResource::class)
->willReturn($outputResource);
$decorated = $this->createStub(ProviderInterface::class);
$decorated->method('provide')->willReturn($sourceEntity);
$provider = new ObjectMapperProvider($objectMapper, $decorated);

$result = $provider->provide($operation);
$this->assertSame($outputResource, $result);
}

public function testProvideMapsEmptyArray(): void
{
$operation = new Get(class: TargetResource::class, map: true);
Expand Down Expand Up @@ -207,3 +244,13 @@ class TargetResource
{
public string $name = 'target';
}

class InputResource
{
public string $name = 'input';
}

class OutputResource
{
public string $name = 'output';
}
Loading