From edaaefd0df6c57a93c9971be0573a237da23101a Mon Sep 17 00:00:00 2001 From: Dylan van der Hout Date: Fri, 24 Jul 2026 10:49:08 +0200 Subject: [PATCH] feat(object-mapper): support custom output DTO in object mapping --- .../Processor/ObjectMapperOutputProcessor.php | 2 +- .../ObjectMapperOutputProcessorTest.php | 29 +++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/State/Processor/ObjectMapperOutputProcessor.php b/src/State/Processor/ObjectMapperOutputProcessor.php index 91a4fa169eb..7076acb00f8 100644 --- a/src/State/Processor/ObjectMapperOutputProcessor.php +++ b/src/State/Processor/ObjectMapperOutputProcessor.php @@ -48,7 +48,7 @@ public function process(mixed $data, Operation $operation, array $uriVariables = $request = $context['request'] ?? null; $request?->attributes->set('persisted_data', $data); - $dto = $this->objectMapper->map($data, $operation->getClass()); + $dto = $this->objectMapper->map($data, $operation->getOutput()['class'] ?? $operation->getClass()); return $this->decorated ? $this->decorated->process($dto, $operation, $uriVariables, $context) : $dto; } diff --git a/src/State/Tests/Processor/ObjectMapperOutputProcessorTest.php b/src/State/Tests/Processor/ObjectMapperOutputProcessorTest.php index 3ed28bf1f1e..8fea6acc90e 100644 --- a/src/State/Tests/Processor/ObjectMapperOutputProcessorTest.php +++ b/src/State/Tests/Processor/ObjectMapperOutputProcessorTest.php @@ -107,6 +107,31 @@ public function testProcessMapsEntityToDto(): void $this->assertSame($result, $processor->process($entity, $operation)); } + public function testProcessMapsEntityToDefinedOutputClass(): void + { + $entity = new \stdClass(); + $entity->id = 1; + $dto = new \stdClass(); + $dto->id = 1; + $result = new \stdClass(); + $operation = new Post(class: ObjectMapperOutputDummy::class, output: ['class' => ObjectMapperOutputDtoDummy::class], map: true); + + $objectMapper = $this->createMock(ObjectMapperInterface::class); + $objectMapper->expects($this->once()) + ->method('map') + ->with($entity, ObjectMapperOutputDtoDummy::class) + ->willReturn($dto); + + $decorated = $this->createMock(ProcessorInterface::class); + $decorated->expects($this->once()) + ->method('process') + ->with($dto, $operation, [], $this->anything()) + ->willReturn($result); + + $processor = new ObjectMapperOutputProcessor($objectMapper, $decorated); + $this->assertSame($result, $processor->process($entity, $operation)); + } + public function testProcessSetsPersistedDataOnRequest(): void { $entity = new \stdClass(); @@ -136,3 +161,7 @@ public function testProcessSetsPersistedDataOnRequest(): void class ObjectMapperOutputDummy { } + +class ObjectMapperOutputDtoDummy +{ +}