|
28 | 28 | use ApiPlatform\Metadata\Util\ClassInfoTrait; |
29 | 29 | use ApiPlatform\Serializer\AbstractItemNormalizer; |
30 | 30 | use ApiPlatform\Serializer\ContextTrait; |
31 | | -use ApiPlatform\Serializer\OperationResourceClassResolverInterface; |
| 31 | +use ApiPlatform\Serializer\OperationResourceResolverInterface; |
32 | 32 | use ApiPlatform\Serializer\TagCollectorInterface; |
33 | 33 | use Symfony\Component\PropertyAccess\PropertyAccessorInterface; |
34 | 34 | use Symfony\Component\Serializer\Exception\LogicException; |
@@ -72,7 +72,7 @@ final class ItemNormalizer extends AbstractItemNormalizer |
72 | 72 | '@vocab', |
73 | 73 | ]; |
74 | 74 |
|
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) |
76 | 76 | { |
77 | 77 | parent::__construct($propertyNameCollectionFactory, $propertyMetadataFactory, $iriConverter, $resourceClassResolver, $propertyAccessor, $nameConverter, $classMetadataFactory, $defaultContext, $resourceMetadataCollectionFactory, $resourceAccessChecker, $tagCollector, $operationResourceResolver); |
78 | 78 | } |
@@ -187,7 +187,16 @@ public function normalize(mixed $data, ?string $format = null, array $context = |
187 | 187 | if (!isset($metadata['@type']) && $operation) { |
188 | 188 | $types = $operation instanceof HttpOperation ? $operation->getTypes() : null; |
189 | 189 | 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 | + } |
191 | 200 | } |
192 | 201 | $metadata['@type'] = 1 === \count($types) ? $types[0] : $types; |
193 | 202 | } |
|
0 commit comments