Skip to content

Commit 332272c

Browse files
authored
fix(jsonld): restore item_uri_template @type resolution after 4.2 merge (#7764)
| Q | A | ------------- | --- | Branch? | main | Tickets | ∅ | License | MIT | Doc PR | ∅ * Revert output+item_uri_template early-return from d70eec5 back to 40ad568's approach * Use operation shortName directly for item_uri_template @type instead of resource-level lookup * Fixes @type being "Recipe" instead of "ItemRecipe" when operation has explicit shortName
1 parent 771ad58 commit 332272c

1 file changed

Lines changed: 13 additions & 33 deletions

File tree

src/JsonLd/Serializer/ItemNormalizer.php

Lines changed: 13 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -103,26 +103,7 @@ public function normalize(mixed $data, ?string $format = null, array $context =
103103
$resourceClass = $this->getObjectClass($data);
104104
$outputClass = $this->getOutputClass($context);
105105

106-
if ($outputClass) {
107-
if ($context['item_uri_template'] ?? null) {
108-
// When both output and item_uri_template are present, temporarily remove
109-
// item_uri_template so the output re-dispatch produces the correct @type
110-
// from the output class (not from the item_uri_template operation).
111-
$itemUriTemplate = $context['item_uri_template'];
112-
unset($context['item_uri_template']);
113-
$originalData = $data;
114-
$data = parent::normalize($data, $format, $context);
115-
if (\is_array($data)) {
116-
try {
117-
$context['item_uri_template'] = $itemUriTemplate;
118-
$data['@id'] = $this->iriConverter->getIriFromResource($originalData, UrlGeneratorInterface::ABS_PATH, null, $context);
119-
} catch (\Exception) {
120-
}
121-
}
122-
123-
return $data;
124-
}
125-
106+
if ($outputClass && !($context['item_uri_template'] ?? null)) {
126107
return parent::normalize($data, $format, $context);
127108
}
128109

@@ -141,14 +122,7 @@ public function normalize(mixed $data, ?string $format = null, array $context =
141122
}
142123

143124
if (isset($context['item_uri_template']) && $this->operationMetadataFactory) {
144-
$itemOp = $this->operationMetadataFactory->create($context['item_uri_template']);
145-
// Use resource-level shortName for @type, not operation-specific shortName
146-
try {
147-
$itemResourceShortName = $this->resourceMetadataCollectionFactory->create($itemOp->getClass())[0]->getShortName();
148-
$context['output']['operation'] = $itemOp->withShortName($itemResourceShortName);
149-
} catch (\Exception) {
150-
$context['output']['operation'] = $itemOp;
151-
}
125+
$context['output']['operation'] = $this->operationMetadataFactory->create($context['item_uri_template']);
152126
} elseif ($this->resourceClassResolver->isResourceClass($resourceClass)) {
153127
$context['output']['operation'] = $this->resourceMetadataCollectionFactory->create($resourceClass)->getOperation();
154128
}
@@ -190,12 +164,18 @@ public function normalize(mixed $data, ?string $format = null, array $context =
190164
// TODO: 5.x break on this as this looks wrong, CollectionReferencingItem returns an IRI that point through
191165
// ItemReferencedInCollection but it returns a CollectionReferencingItem therefore we should use the current
192166
// object's class Type and not rely on operation ?
193-
// Use resource-level shortName to avoid operation-specific overrides
194-
$typeClass = $isResourceClass ? $resourceClass : ($operation->getClass() ?? $resourceClass);
195-
try {
196-
$types = [$this->resourceMetadataCollectionFactory->create($typeClass)[0]->getShortName()];
197-
} catch (\Exception) {
167+
if (isset($context['item_uri_template'])) {
168+
// When the operation comes from item_uri_template, use its shortName directly
169+
// as $resourceClass refers to the collection resource, not the item resource
198170
$types = [$operation->getShortName()];
171+
} else {
172+
// Use resource-level shortName to avoid operation-specific overrides
173+
$typeClass = $isResourceClass ? $resourceClass : ($operation->getClass() ?? $resourceClass);
174+
try {
175+
$types = [$this->resourceMetadataCollectionFactory->create($typeClass)[0]->getShortName()];
176+
} catch (\Exception) {
177+
$types = [$operation->getShortName()];
178+
}
199179
}
200180
}
201181
$metadata['@type'] = 1 === \count($types) ? $types[0] : $types;

0 commit comments

Comments
 (0)