Skip to content

Commit 098146f

Browse files
committed
fix(jsonld): genId false should work with embeded resources
1 parent 64ff50f commit 098146f

File tree

3 files changed

+15
-11
lines changed

3 files changed

+15
-11
lines changed

src/JsonLd/Serializer/ItemNormalizer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ public function normalize(mixed $object, ?string $format = null, array $context
124124
unset($context['operation'], $context['operation_name']);
125125
}
126126

127-
if (true === ($context['force_iri_generation'] ?? true) && $iri = $this->iriConverter->getIriFromResource($object, UrlGeneratorInterface::ABS_PATH, $context['operation'] ?? null, $context)) {
127+
if (true === ($context['output']['genid'] ?? true) && true === ($context['force_iri_generation'] ?? true) && $iri = $this->iriConverter->getIriFromResource($object, UrlGeneratorInterface::ABS_PATH, $context['operation'] ?? null, $context)) {
128128
$context['iri'] = $iri;
129129
$metadata['@id'] = $iri;
130130
}

src/Serializer/AbstractItemNormalizer.php

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -670,7 +670,7 @@ protected function getAttributeValue(object $object, string $attribute, ?string
670670
&& ($className = $collectionValueType->getClassName())
671671
&& $this->resourceClassResolver->isResourceClass($className)
672672
) {
673-
$childContext = $this->createChildContext($this->createOperationContext($context, $className), $attribute, $format);
673+
$childContext = $this->createChildContext($this->createOperationContext($context, $className, $propertyMetadata), $attribute, $format);
674674

675675
// @see ApiPlatform\Hal\Serializer\ItemNormalizer:getComponents logic for intentional duplicate content
676676
// @see ApiPlatform\JsonApi\Serializer\ItemNormalizer:getComponents logic for intentional duplicate content
@@ -707,7 +707,7 @@ protected function getAttributeValue(object $object, string $attribute, ?string
707707
($className = $type->getClassName())
708708
&& $this->resourceClassResolver->isResourceClass($className)
709709
) {
710-
$childContext = $this->createChildContext($this->createOperationContext($context, $className), $attribute, $format);
710+
$childContext = $this->createChildContext($this->createOperationContext($context, $className, $propertyMetadata), $attribute, $format);
711711
unset($childContext['iri'], $childContext['uri_variables'], $childContext['item_uri_template']);
712712
if ('jsonld' === $format && $uriTemplate = $propertyMetadata->getUriTemplate()) {
713713
$operation = $this->resourceMetadataCollectionFactory->create($className)->getOperation(
@@ -749,22 +749,19 @@ protected function getAttributeValue(object $object, string $attribute, ?string
749749

750750
// Anonymous resources
751751
if ($className) {
752-
$childContext = $this->createChildContext($this->createOperationContext($context, $className), $attribute, $format);
753-
$childContext['output']['gen_id'] = $propertyMetadata->getGenId() ?? true;
754-
752+
$childContext = $this->createChildContext($this->createOperationContext($context, $className, $propertyMetadata), $attribute, $format);
755753
$attributeValue = $this->propertyAccessor->getValue($object, $attribute);
756754

757755
return $this->serializer->normalize($attributeValue, $format, $childContext);
758756
}
759757

760758
if ('array' === $type->getBuiltinType()) {
761759
if ($className = ($type->getCollectionValueTypes()[0] ?? null)?->getClassName()) {
762-
$context = $this->createOperationContext($context, $className);
760+
$context = $this->createOperationContext($context, $className, $propertyMetadata);
763761
}
764762

765763
$childContext = $this->createChildContext($context, $attribute, $format);
766764
$childContext['output']['gen_id'] = $propertyMetadata->getGenId() ?? true;
767-
768765
$attributeValue = $this->propertyAccessor->getValue($object, $attribute);
769766

770767
return $this->serializer->normalize($attributeValue, $format, $childContext);
@@ -825,7 +822,7 @@ protected function normalizeRelation(ApiProperty $propertyMetadata, ?object $rel
825822
throw new LogicException(\sprintf('The injected serializer must be an instance of "%s".', NormalizerInterface::class));
826823
}
827824

828-
$relatedContext = $this->createOperationContext($context, $resourceClass);
825+
$relatedContext = $this->createOperationContext($context, $resourceClass, $propertyMetadata);
829826
$normalizedRelatedObject = $this->serializer->normalize($relatedObject, $format, $relatedContext);
830827
if (!\is_string($normalizedRelatedObject) && !\is_array($normalizedRelatedObject) && !$normalizedRelatedObject instanceof \ArrayObject && null !== $normalizedRelatedObject) {
831828
throw new UnexpectedValueException('Expected normalized relation to be an IRI, array, \ArrayObject or null');
@@ -917,7 +914,7 @@ private function createAndValidateAttributeValue(string $attribute, mixed $value
917914
&& $this->resourceClassResolver->isResourceClass($className)
918915
) {
919916
$resourceClass = $this->resourceClassResolver->getResourceClass(null, $className);
920-
$childContext = $this->createChildContext($this->createOperationContext($context, $resourceClass), $attribute, $format);
917+
$childContext = $this->createChildContext($this->createOperationContext($context, $resourceClass, $propertyMetadata), $attribute, $format);
921918

922919
try {
923920
return $this->denormalizeRelation($attribute, $propertyMetadata, $resourceClass, $value, $format, $childContext);

src/Serializer/OperationContextTrait.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
namespace ApiPlatform\Serializer;
1515

16+
use ApiPlatform\Metadata\ApiProperty;
17+
1618
/**
1719
* @internal
1820
*/
@@ -22,7 +24,7 @@ trait OperationContextTrait
2224
* This context is created when working on a relation context or items of a collection. It cleans the previously given
2325
* context as the operation changes.
2426
*/
25-
protected function createOperationContext(array $context, ?string $resourceClass = null): array
27+
protected function createOperationContext(array $context, ?string $resourceClass = null, ?ApiProperty $propertyMetadata = null): array
2628
{
2729
if (isset($context['operation']) && !isset($context['root_operation'])) {
2830
$context['root_operation'] = $context['operation'];
@@ -45,6 +47,11 @@ protected function createOperationContext(array $context, ?string $resourceClass
4547
unset($context['operation'], $context['operation_name']);
4648
$context['resource_class'] = $resourceClass;
4749

50+
// At some point we should merge the jsonld context here, there's a TODO to simplify this somewhere else
51+
if ($propertyMetadata) {
52+
$context['output']['genid'] = $propertyMetadata->getGenId() ?? true;
53+
}
54+
4855
return $context;
4956
}
5057
}

0 commit comments

Comments
 (0)