Skip to content

Commit f626ff5

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

File tree

3 files changed

+17
-13
lines changed

3 files changed

+17
-13
lines changed

src/JsonLd/Serializer/ItemNormalizer.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public function normalize(mixed $object, ?string $format = null, array $context
111111
} elseif ($this->contextBuilder instanceof AnonymousContextBuilderInterface) {
112112
if ($context['api_collection_sub_level'] ?? false) {
113113
unset($context['api_collection_sub_level']);
114-
$context['output']['genid'] = true;
114+
$context['output']['gen_id'] ??= true;
115115
$context['output']['iri'] = null;
116116
}
117117

@@ -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 & 10 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,18 @@ 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);
766-
$childContext['output']['gen_id'] = $propertyMetadata->getGenId() ?? true;
767-
768764
$attributeValue = $this->propertyAccessor->getValue($object, $attribute);
769765

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

828-
$relatedContext = $this->createOperationContext($context, $resourceClass);
824+
$relatedContext = $this->createOperationContext($context, $resourceClass, $propertyMetadata);
829825
$normalizedRelatedObject = $this->serializer->normalize($relatedObject, $format, $relatedContext);
830826
if (!\is_string($normalizedRelatedObject) && !\is_array($normalizedRelatedObject) && !$normalizedRelatedObject instanceof \ArrayObject && null !== $normalizedRelatedObject) {
831827
throw new UnexpectedValueException('Expected normalized relation to be an IRI, array, \ArrayObject or null');
@@ -917,7 +913,7 @@ private function createAndValidateAttributeValue(string $attribute, mixed $value
917913
&& $this->resourceClassResolver->isResourceClass($className)
918914
) {
919915
$resourceClass = $this->resourceClassResolver->getResourceClass(null, $className);
920-
$childContext = $this->createChildContext($this->createOperationContext($context, $resourceClass), $attribute, $format);
916+
$childContext = $this->createChildContext($this->createOperationContext($context, $resourceClass, $propertyMetadata), $attribute, $format);
921917

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

src/Serializer/OperationContextTrait.php

Lines changed: 9 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'];
@@ -34,6 +36,12 @@ protected function createOperationContext(array $context, ?string $resourceClass
3436

3537
unset($context['iri'], $context['uri_variables'], $context['item_uri_template'], $context['force_resource_class']);
3638

39+
// At some point we should merge the jsonld context here, there's a TODO to simplify this somewhere else
40+
if ($propertyMetadata) {
41+
$context['output'] ??= [];
42+
$context['output']['gen_id'] = $propertyMetadata->getGenId() ?? true;
43+
}
44+
3745
if (!$resourceClass) {
3846
return $context;
3947
}

0 commit comments

Comments
 (0)