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/Processor/ObjectMapperProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@

use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\ProcessorInterface;
use Symfony\Component\ObjectMapper\ObjectMapperInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\ObjectMapper\ObjectMapperInterface;

/**
* @implements ProcessorInterface<mixed,mixed>
Expand Down
12 changes: 11 additions & 1 deletion src/State/Provider/ObjectMapperProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ public function provide(Operation $operation, array $uriVariables = [], array $c
{
$data = $this->decorated->provide($operation, $uriVariables, $context);

if (!$this->objectMapper || !\is_object($data) || !$operation->canMap()) {
if (!$this->objectMapper || !$operation->canMap()) {
return $data;
}

if (!\is_object($data) && !\is_array($data)) {
return $data;
}

Expand All @@ -49,6 +53,12 @@ public function provide(Operation $operation, array $uriVariables = [], array $c

if ($data instanceof PaginatorInterface) {
$data = new ArrayPaginator(array_map(fn ($v) => $this->objectMapper->map($v, $operation->getClass()), iterator_to_array($data)), 0, \count($data));
} elseif (\is_array($data)) {
foreach ($data as &$v) {
if (\is_object($v)) {
$v = $this->objectMapper->map($v, $operation->getClass());
}
}
} else {
$data = $this->objectMapper->map($data, $operation->getClass());
}
Expand Down
41 changes: 41 additions & 0 deletions tests/Fixtures/TestBundle/ApiResource/Issue7563/BookDto.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?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\Issue7563;

use ApiPlatform\Doctrine\Orm\State\Options;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Book;
use ApiPlatform\Tests\Fixtures\TestBundle\ObjectMapper\IsbnToCustomIsbnTransformer;
use Symfony\Component\ObjectMapper\Attribute\Map;

#[Get(
stateOptions: new Options(entityClass: Book::class)
)]
#[GetCollection(
stateOptions: new Options(entityClass: Book::class)
)]
#[Map(source: Book::class)]
class BookDto
{
public function __construct(
#[Map(source: 'id')]
public ?int $id = null,
#[Map(source: 'name')]
public ?string $name = null,
#[Map(source: 'isbn', transform: IsbnToCustomIsbnTransformer::class)]
public ?string $customIsbn = null,
) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?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\ObjectMapper;

use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Issue7563\BookDto;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Book;
use Symfony\Component\ObjectMapper\TransformCallableInterface;

/**
* @implements TransformCallableInterface<Book, BookDto>
*/
final readonly class IsbnToCustomIsbnTransformer implements TransformCallableInterface
{
public function __invoke(mixed $value, object $source, ?object $target): mixed
{
return 'custom'.$value;
}
}
4 changes: 4 additions & 0 deletions tests/Fixtures/app/config/config_common.yml
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,10 @@ services:
tags:
- name: 'api_platform.state_processor'

ApiPlatform\Tests\Fixtures\TestBundle\ObjectMapper\IsbnToCustomIsbnTransformer:
tags:
- name: 'object_mapper.transform_callable'

app.messenger_handler.messenger_with_response:
class: 'ApiPlatform\Tests\Fixtures\TestBundle\MessengerHandler\MessengerWithResponseHandler'
tags:
Expand Down
74 changes: 74 additions & 0 deletions tests/Functional/MappingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use ApiPlatform\Symfony\Bundle\Test\ApiTestCase;
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\FirstResource;
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Issue7563\BookDto;
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\MappedResource;
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\MappedResourceNoMap;
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\MappedResourceOdm;
Expand All @@ -24,6 +25,7 @@
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\MappedResourceWithRelationRelated;
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\SecondResource;
use ApiPlatform\Tests\Fixtures\TestBundle\Document\MappedDocument;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Book;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\MappedEntity;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\MappedEntityNoMap;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\MappedEntitySourceOnly;
Expand Down Expand Up @@ -55,6 +57,7 @@ public static function getResources(): array
MappedResourceWithInput::class,
MappedResourceSourceOnly::class,
MappedResourceNoMap::class,
BookDto::class,
];
}

Expand Down Expand Up @@ -277,4 +280,75 @@ private function loadFixtures(): void

$manager->flush();
}

private function loadBookFixtures(): void
{
$manager = $this->getManager();

for ($i = 1; $i <= 5; ++$i) {
$book = new Book();
$book->name = 'Book '.$i;
$book->isbn = 'ISBN-'.$i;
$manager->persist($book);
}

$manager->flush();
}

public function testGetSingleBookDto(): void
{
if ($this->isMongoDB()) {
$this->markTestSkipped('MongoDB not tested.');
}

$this->recreateSchema([Book::class]);
$this->loadBookFixtures();

self::createClient()->request('GET', '/book_dtos/1');
self::assertResponseIsSuccessful();
self::assertJsonContains([
'@type' => 'BookDto',
'id' => 1,
'name' => 'Book 1',
'customIsbn' => 'customISBN-1',
]);
}

public function testGetCollectionBookDtoPaginated(): void
{
if ($this->isMongoDB()) {
$this->markTestSkipped('MongoDB not tested.');
}

$this->recreateSchema([Book::class]);
$this->loadBookFixtures();

$response = self::createClient()->request('GET', '/book_dtos');
self::assertResponseIsSuccessful();

$json = $response->toArray();
self::assertCount(3, $json['hydra:member']);
foreach ($response->toArray()['hydra:member'] as $member) {
self::assertStringStartsWith('customISBN-', $member['customIsbn']);
}
}

public function testGetCollectionBookDtoUnpaginated(): void
{
if ($this->isMongoDB()) {
$this->markTestSkipped('MongoDB not tested.');
}

$this->recreateSchema([Book::class]);
$this->loadBookFixtures();

$response = self::createClient()->request('GET', '/book_dtos?pagination=false');
self::assertResponseIsSuccessful();

$json = $response->toArray();
self::assertCount(5, $json['hydra:member']);
foreach ($json['hydra:member'] as $member) {
self::assertStringStartsWith('customISBN-', $member['customIsbn']);
}
}
}
Loading
Loading