Skip to content

Commit 657ed55

Browse files
committed
todo
1 parent 09f248b commit 657ed55

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

src/JsonLd/Serializer/ItemNormalizer.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
use ApiPlatform\Metadata\Util\ClassInfoTrait;
2929
use ApiPlatform\Serializer\AbstractItemNormalizer;
3030
use ApiPlatform\Serializer\ContextTrait;
31-
use ApiPlatform\Serializer\OperationResourceClassResolverInterface;
31+
use ApiPlatform\Serializer\OperationResourceResolverInterface;
3232
use ApiPlatform\Serializer\TagCollectorInterface;
3333
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
3434
use Symfony\Component\Serializer\Exception\LogicException;
@@ -72,7 +72,7 @@ final class ItemNormalizer extends AbstractItemNormalizer
7272
'@vocab',
7373
];
7474

75-
public function __construct(ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory, PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory, IriConverterInterface $iriConverter, ResourceClassResolverInterface $resourceClassResolver, private readonly ContextBuilderInterface $contextBuilder, ?PropertyAccessorInterface $propertyAccessor = null, ?NameConverterInterface $nameConverter = null, ?ClassMetadataFactoryInterface $classMetadataFactory = null, array $defaultContext = [], ?ResourceAccessCheckerInterface $resourceAccessChecker = null, protected ?TagCollectorInterface $tagCollector = null, private ?OperationMetadataFactoryInterface $operationMetadataFactory = null, ?OperationResourceClassResolverInterface $operationResourceResolver = null)
75+
public function __construct(ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory, PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory, IriConverterInterface $iriConverter, ResourceClassResolverInterface $resourceClassResolver, private readonly ContextBuilderInterface $contextBuilder, ?PropertyAccessorInterface $propertyAccessor = null, ?NameConverterInterface $nameConverter = null, ?ClassMetadataFactoryInterface $classMetadataFactory = null, array $defaultContext = [], ?ResourceAccessCheckerInterface $resourceAccessChecker = null, protected ?TagCollectorInterface $tagCollector = null, private ?OperationMetadataFactoryInterface $operationMetadataFactory = null, ?OperationResourceResolverInterface $operationResourceResolver = null)
7676
{
7777
parent::__construct($propertyNameCollectionFactory, $propertyMetadataFactory, $iriConverter, $resourceClassResolver, $propertyAccessor, $nameConverter, $classMetadataFactory, $defaultContext, $resourceMetadataCollectionFactory, $resourceAccessChecker, $tagCollector, $operationResourceResolver);
7878
}
@@ -187,7 +187,16 @@ public function normalize(mixed $data, ?string $format = null, array $context =
187187
if (!isset($metadata['@type']) && $operation) {
188188
$types = $operation instanceof HttpOperation ? $operation->getTypes() : null;
189189
if (null === $types) {
190-
$types = [$operation->getShortName()];
190+
// TODO: 5.x break on this as this looks wrong, CollectionReferencingItem returns an IRI that point through
191+
// ItemReferencedInCollection but it returns a CollectionReferencingItem therefore we should use the current
192+
// 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) {
198+
$types = [$operation->getShortName()];
199+
}
191200
}
192201
$metadata['@type'] = 1 === \count($types) ? $types[0] : $types;
193202
}

0 commit comments

Comments
 (0)