protected function denormalizeRelation(string $attributeName, PropertyMetadata $propertyMetadata, string $className, $value, ?string $format, array $context)
    {
        if (\is_string($value)) {
            try {
                return $this->iriConverter->getItemFromIri($value, $context + ['fetch_data' => true]);
            } catch (ItemNotFoundException $e) {
                throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
            } catch (InvalidArgumentException $e) {
                // Give a chance to other normalizers (e.g.: DateTimeNormalizer)
            }
        }

        if (
            !$this->resourceClassResolver->isResourceClass($className) ||
            $propertyMetadata->isWritableLink()
        ) {
            $context['resource_class'] = $className;
            $context['api_allow_update'] = true;

            try {
                if (!$this->serializer instanceof DenormalizerInterface) {
                    throw new LogicException(sprintf('The injected serializer must be an instance of "%s".', DenormalizerInterface::class));
                }

                return $this->serializer->denormalize($value, $className, $format, $context);
            } catch (InvalidValueException $e) {
                if (!$this->allowPlainIdentifiers || null === $this->itemDataProvider) {
                    throw $e;
                }
            }
        }

        if (!\is_array($value)) {
            // repeat the code so that IRIs keep working with the json format
            if (true === $this->allowPlainIdentifiers && $this->itemDataProvider) {
                $item = $this->itemDataProvider->getItem($className, $value, null, $context + ['fetch_data' => true]);
                if (null === $item) {
                    throw new ItemNotFoundException(sprintf('Item not found for "%s".', $value));
                }

                return $item;
            }

            throw new UnexpectedValueException(sprintf(
                'Expected IRI or nested document for attribute "%s", "%s" given.', $attributeName, \gettype($value)
            ));
        }

        throw new UnexpectedValueException(sprintf('Nested documents for attribute "%s" are not allowed. Use IRIs instead.', $attributeName));
    }