Skip to content

Commit a5cbb02

Browse files
committed
fix(metadata): enhance resource class determination before Object Mapper Processor return
1 parent 2a34498 commit a5cbb02

File tree

2 files changed

+29
-18
lines changed

2 files changed

+29
-18
lines changed

src/Metadata/Resource/Factory/ObjectMapperMetadataCollectionFactory.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,14 @@ public function create(string $resourceClass): ResourceMetadataCollection
5252
$entityClass = $options->getDocumentClass();
5353
}
5454

55-
$class = $operation->getInput()['class'] ?? $operation->getClass();
55+
$inputClass = $operation->getInput()['class'] ?? $operation->getClass();
56+
$outputClass = $operation->getOutput()['class'] ?? null;
5657
$entityMap = null;
5758

5859
// Look for Mapping metadata
59-
if ($this->canBeMapped($class) || ($entityClass && ($entityMap = $this->canBeMapped($entityClass)))) {
60+
if ($this->canBeMapped($inputClass)
61+
|| ($outputClass && $this->canBeMapped($outputClass))
62+
|| ($entityClass && ($entityMap = $this->canBeMapped($entityClass)))) {
6063
$found = true;
6164
if ($entityMap) {
6265
foreach ($entityMap as $mapping) {

src/State/Processor/ObjectMapperProcessor.php

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,35 +34,43 @@ public function __construct(
3434

3535
public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): object|array|null
3636
{
37-
$class = $operation->getInput()['class'] ?? $operation->getClass();
38-
3937
if (
4038
$data instanceof Response
4139
|| !$this->objectMapper
4240
|| !$operation->canWrite()
4341
|| null === $data
44-
|| !is_a($data, $class, true)
4542
|| !$operation->canMap()
4643
) {
4744
return $this->decorated->process($data, $operation, $uriVariables, $context);
4845
}
4946

5047
$request = $context['request'] ?? null;
51-
$persisted = $this->decorated->process(
52-
// maps the Resource to an Entity
53-
$this->objectMapper->map($data, $request?->attributes->get('mapped_data')),
54-
$operation,
55-
$uriVariables,
56-
$context,
57-
);
48+
$resourceClass = $operation->getClass();
49+
$inputClass = $operation->getInput()['class'] ?? null;
50+
$outputClass = $operation->getOutput()['class'] ?? null;
51+
52+
$hasCustomInput = null !== $inputClass && $inputClass !== $resourceClass;
53+
$hasCustomOutput = null !== $outputClass && $outputClass !== $resourceClass;
54+
55+
if (!$hasCustomInput && !$hasCustomOutput) {
56+
return $this->decorated->process($data, $operation, $uriVariables, $context);
57+
}
5858

59+
if ($hasCustomInput) {
60+
if (!is_a($data, $inputClass, true)) {
61+
return $this->decorated->process($data, $operation, $uriVariables, $context);
62+
}
63+
64+
$data = $this->objectMapper->map($data, $request?->attributes->get('mapped_data'));
65+
}
66+
67+
$persisted = $this->decorated->process($data, $operation, $uriVariables, $context);
5968
$request?->attributes->set('persisted_data', $persisted);
6069

61-
// return the Resource representation of the persisted entity
62-
return $this->objectMapper->map(
63-
// persist the entity
64-
$persisted,
65-
$operation->getClass()
66-
);
70+
if ($hasCustomOutput) {
71+
return $this->objectMapper->map($persisted, $outputClass);
72+
}
73+
74+
return $persisted;
6775
}
6876
}

0 commit comments

Comments
 (0)