Skip to content

Commit ee0dfc1

Browse files
Try using serializer when denormalizing relation
1 parent 344f91a commit ee0dfc1

1 file changed

Lines changed: 27 additions & 38 deletions

File tree

src/Serializer/AbstractItemNormalizer.php

Lines changed: 27 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -266,21 +266,7 @@ public function denormalize(mixed $data, string $type, ?string $format = null, a
266266
}
267267

268268
if (\is_string($data)) {
269-
try {
270-
return $this->iriConverter->getResourceFromIri($data, $context + ['fetch_data' => true]);
271-
} catch (ItemNotFoundException $e) {
272-
if (!isset($context['not_normalizable_value_exceptions'])) {
273-
throw new UnexpectedValueException($e->getMessage(), $e->getCode(), $e);
274-
}
275-
276-
throw NotNormalizableValueException::createForUnexpectedDataType($e->getMessage(), $data, [$resourceClass], $context['deserialization_path'] ?? null, true, $e->getCode(), $e);
277-
} catch (InvalidArgumentException $e) {
278-
if (!isset($context['not_normalizable_value_exceptions'])) {
279-
throw new UnexpectedValueException(\sprintf('Invalid IRI "%s".', $data), $e->getCode(), $e);
280-
}
281-
282-
throw NotNormalizableValueException::createForUnexpectedDataType(\sprintf('Invalid IRI "%s".', $data), $data, [$resourceClass], $context['deserialization_path'] ?? null, true, $e->getCode(), $e);
283-
}
269+
return $this->getResourceFromIri($data, $context, $resourceClass);
284270
}
285271

286272
if (!\is_array($data)) {
@@ -699,32 +685,16 @@ protected function denormalizeObjectCollection(string $attribute, ApiProperty $p
699685
*/
700686
protected function denormalizeRelation(string $attributeName, ApiProperty $propertyMetadata, string $className, mixed $value, ?string $format, array $context): ?object
701687
{
702-
if (\is_string($value)) {
703-
try {
704-
return $this->iriConverter->getResourceFromIri($value, $context + ['fetch_data' => true]);
705-
} catch (ItemNotFoundException $e) {
706-
if (false === ($context['denormalize_throw_on_relation_not_found'] ?? true)) {
707-
return null;
708-
}
709-
710-
if (!isset($context['not_normalizable_value_exceptions'])) {
711-
throw new UnexpectedValueException($e->getMessage(), $e->getCode(), $e);
712-
}
713-
714-
throw NotNormalizableValueException::createForUnexpectedDataType($e->getMessage(), $value, [$className], $context['deserialization_path'] ?? null, true, $e->getCode(), $e);
715-
} catch (InvalidArgumentException $e) {
716-
if (!isset($context['not_normalizable_value_exceptions'])) {
717-
throw new UnexpectedValueException(\sprintf('Invalid IRI "%s".', $value), $e->getCode(), $e);
718-
}
719-
720-
throw NotNormalizableValueException::createForUnexpectedDataType(\sprintf('Invalid IRI "%s".', $value), $value, [$className], $context['deserialization_path'] ?? null, true, $e->getCode(), $e);
688+
if (\is_string($value) || $propertyMetadata->isWritableLink()) {
689+
if ($propertyMetadata->isWritableLink()) {
690+
$context['api_allow_update'] = true;
721691
}
722-
}
723-
724-
if ($propertyMetadata->isWritableLink()) {
725-
$context['api_allow_update'] = true;
726692

727693
if (!$this->serializer instanceof DenormalizerInterface) {
694+
if (\is_string($value)) {
695+
return $this->getResourceFromIri($value, $context, $className);
696+
}
697+
728698
throw new LogicException(\sprintf('The injected serializer must be an instance of "%s".', DenormalizerInterface::class));
729699
}
730700

@@ -743,6 +713,25 @@ protected function denormalizeRelation(string $attributeName, ApiProperty $prope
743713
throw NotNormalizableValueException::createForUnexpectedDataType(\sprintf('Nested documents for attribute "%s" are not allowed. Use IRIs instead.', $attributeName), $value, ['array', 'string'], $context['deserialization_path'] ?? null, true);
744714
}
745715

716+
private function getResourceFromIri(string $data, array $context, string $resourceClass): object
717+
{
718+
try {
719+
return $this->iriConverter->getResourceFromIri($data, $context + ['fetch_data' => true]);
720+
} catch (ItemNotFoundException $e) {
721+
if (!isset($context['not_normalizable_value_exceptions'])) {
722+
throw new UnexpectedValueException($e->getMessage(), $e->getCode(), $e);
723+
}
724+
725+
throw NotNormalizableValueException::createForUnexpectedDataType($e->getMessage(), $data, [$resourceClass], $context['deserialization_path'] ?? null, true, $e->getCode(), $e);
726+
} catch (InvalidArgumentException $e) {
727+
if (!isset($context['not_normalizable_value_exceptions'])) {
728+
throw new UnexpectedValueException(\sprintf('Invalid IRI "%s".', $data), $e->getCode(), $e);
729+
}
730+
731+
throw NotNormalizableValueException::createForUnexpectedDataType(\sprintf('Invalid IRI "%s".', $data), $data, [$resourceClass], $context['deserialization_path'] ?? null, true, $e->getCode(), $e);
732+
}
733+
}
734+
746735
/**
747736
* Gets the options for the property name collection / property metadata factories.
748737
*/

0 commit comments

Comments
 (0)