diff --git a/src/State/Processor/ObjectMapperProcessor.php b/src/State/Processor/ObjectMapperProcessor.php index 7c71d3e3f0..78d3341a3b 100644 --- a/src/State/Processor/ObjectMapperProcessor.php +++ b/src/State/Processor/ObjectMapperProcessor.php @@ -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; @@ -23,13 +24,16 @@ */ final class ObjectMapperProcessor implements ProcessorInterface { + use StateOptionsTrait; + /** * @param ProcessorInterface $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 @@ -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, @@ -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() ); diff --git a/src/Symfony/Tests/Fixtures/AnotherMappedObject.php b/src/Symfony/Tests/Fixtures/AnotherMappedObject.php new file mode 100644 index 0000000000..5111a342ba --- /dev/null +++ b/src/Symfony/Tests/Fixtures/AnotherMappedObject.php @@ -0,0 +1,8 @@ + false], )] +#[Map(target: AnotherMappedObject::class)] #[Map(target: MappedEntity::class)] final class MappedResource { diff --git a/tests/Fixtures/TestBundle/ApiResource/MappedResourceOdm.php b/tests/Fixtures/TestBundle/ApiResource/MappedResourceOdm.php index 2a61cabab7..3776bfc5de 100644 --- a/tests/Fixtures/TestBundle/ApiResource/MappedResourceOdm.php +++ b/tests/Fixtures/TestBundle/ApiResource/MappedResourceOdm.php @@ -16,6 +16,7 @@ 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; @@ -23,6 +24,7 @@ stateOptions: new Options(documentClass: MappedDocument::class), normalizationContext: [ContextBuilder::HYDRA_CONTEXT_HAS_PREFIX => false], )] +#[Map(target: AnotherMappedObject::class)] #[Map(target: MappedDocument::class)] final class MappedResourceOdm {