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
22 changes: 17 additions & 5 deletions src/State/Processor/ObjectMapperProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

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

Expand All @@ -23,13 +24,16 @@
*/
final class ObjectMapperProcessor implements ProcessorInterface
{
use StateOptionsTrait;

/**
* @param ProcessorInterface<mixed,mixed> $decorated
*/
public function __construct(
private readonly ?ObjectMapperInterface $objectMapper,
private readonly ProcessorInterface $decorated,
) {
private readonly ProcessorInterface $decorated,
)
{
}

public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): object|array|null
Expand All @@ -48,9 +52,17 @@ public function process(mixed $data, Operation $operation, array $uriVariables =
}

$request = $context['request'] ?? null;

// maps the Resource to an Entity
if ($request?->attributes->get('mapped_data')) {
$mappedData = $this->objectMapper->map($data, $request?->attributes->get('mapped_data'));
} else {
$mappedData = $this->objectMapper->map($data, $this->getStateOptionsClass($operation, $operation->getClass()));
}
$request?->attributes->set('mapped_data', $mappedData);

$persisted = $this->decorated->process(
// maps the Resource to an Entity
$this->objectMapper->map($data, $request?->attributes->get('mapped_data')),
$mappedData,
$operation,
$uriVariables,
$context,
Expand All @@ -65,7 +77,7 @@ public function process(mixed $data, Operation $operation, array $uriVariables =

// return the Resource representation of the persisted entity
return $this->objectMapper->map(
// persist the entity
// persist the entity
$persisted,
$operation->getClass()
);
Expand Down
8 changes: 8 additions & 0 deletions src/Symfony/Tests/Fixtures/AnotherMappedObject.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace ApiPlatform\Symfony\Tests\Fixtures;

class AnotherMappedObject
{

}
2 changes: 2 additions & 0 deletions tests/Fixtures/TestBundle/ApiResource/FirstResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use ApiPlatform\Doctrine\Orm\State\Options;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Symfony\Tests\Fixtures\AnotherMappedObject;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\SameEntity;
use Symfony\Component\ObjectMapper\Attribute\Map;

Expand All @@ -24,6 +25,7 @@
operations: [new GetCollection()],
stateOptions: new Options(entityClass: SameEntity::class)
)]
#[Map(target: AnotherMappedObject::class)]
#[Map(target: SameEntity::class)]
final class FirstResource
{
Expand Down
2 changes: 2 additions & 0 deletions tests/Fixtures/TestBundle/ApiResource/MappedResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@
use ApiPlatform\Doctrine\Orm\State\Options;
use ApiPlatform\JsonLd\ContextBuilder;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Symfony\Tests\Fixtures\AnotherMappedObject;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\MappedEntity;
use Symfony\Component\ObjectMapper\Attribute\Map;

#[ApiResource(
stateOptions: new Options(entityClass: MappedEntity::class),
normalizationContext: [ContextBuilder::HYDRA_CONTEXT_HAS_PREFIX => false],
)]
#[Map(target: AnotherMappedObject::class)]
#[Map(target: MappedEntity::class)]
final class MappedResource
{
Expand Down
2 changes: 2 additions & 0 deletions tests/Fixtures/TestBundle/ApiResource/MappedResourceOdm.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@
use ApiPlatform\Doctrine\Odm\State\Options;
use ApiPlatform\JsonLd\ContextBuilder;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Symfony\Tests\Fixtures\AnotherMappedObject;
use ApiPlatform\Tests\Fixtures\TestBundle\Document\MappedDocument;
use Symfony\Component\ObjectMapper\Attribute\Map;

#[ApiResource(
stateOptions: new Options(documentClass: MappedDocument::class),
normalizationContext: [ContextBuilder::HYDRA_CONTEXT_HAS_PREFIX => false],
)]
#[Map(target: AnotherMappedObject::class)]
#[Map(target: MappedDocument::class)]
final class MappedResourceOdm
{
Expand Down
Loading