Skip to content

Commit 4cd3468

Browse files
authored
fix!(hydra): expose all ApiResource declarations in documentation and entrypoint (#7746)
1 parent 97aea06 commit 4cd3468

File tree

11 files changed

+459
-35
lines changed

11 files changed

+459
-35
lines changed

phpunit.baseline.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,19 @@
3030
<issue><![CDATA[Since symfony/validator 7.1: Not passing a value for the "requireTld" option to the Url constraint is deprecated. Its default value will change to "true".]]></issue>
3131
</line>
3232
</file>
33+
<file path="src/Metadata/Resource/Factory/MetadataCollectionFactoryTrait.php">
34+
<line number="222" hash="29843c95ea9de65e8e7867216969c55e2c33d9e2">
35+
<issue><![CDATA[Since api-platform/core 4.2: Having multiple "#[ApiResource]" attributes with the same "shortName" "SecuredDummy" on class "ApiPlatform\Tests\Fixtures\TestBundle\Entity\SecuredDummy" is deprecated and will result in automatic short name deduplication in API Platform 5.x. Set "defaults.extra_properties.deduplicate_resource_short_names" to "true" in the API Platform configuration to enable it now.]]></issue>
36+
<issue><![CDATA[Since api-platform/core 4.2: Having multiple "#[ApiResource]" attributes with the same "shortName" "RelatedDummy" on class "ApiPlatform\Tests\Fixtures\TestBundle\Entity\RelatedDummy" is deprecated and will result in automatic short name deduplication in API Platform 5.x. Set "defaults.extra_properties.deduplicate_resource_short_names" to "true" in the API Platform configuration to enable it now.]]></issue>
37+
<issue><![CDATA[Since api-platform/core 4.2: Having multiple "#[ApiResource]" attributes with the same "shortName" "Question" on class "ApiPlatform\Tests\Fixtures\TestBundle\Entity\Question" is deprecated and will result in automatic short name deduplication in API Platform 5.x. Set "defaults.extra_properties.deduplicate_resource_short_names" to "true" in the API Platform configuration to enable it now.]]></issue>
38+
<issue><![CDATA[Since api-platform/core 4.2: Having multiple "#[ApiResource]" attributes with the same "shortName" "Answer" on class "ApiPlatform\Tests\Fixtures\TestBundle\Entity\Answer" is deprecated and will result in automatic short name deduplication in API Platform 5.x. Set "defaults.extra_properties.deduplicate_resource_short_names" to "true" in the API Platform configuration to enable it now.]]></issue>
39+
<issue><![CDATA[Since api-platform/core 4.2: Having multiple "#[ApiResource]" attributes with the same "shortName" "ThirdLevel" on class "ApiPlatform\Tests\Fixtures\TestBundle\Entity\ThirdLevel" is deprecated and will result in automatic short name deduplication in API Platform 5.x. Set "defaults.extra_properties.deduplicate_resource_short_names" to "true" in the API Platform configuration to enable it now.]]></issue>
40+
<issue><![CDATA[Since api-platform/core 4.2: Having multiple "#[ApiResource]" attributes with the same "shortName" "RelatedToDummyFriend" on class "ApiPlatform\Tests\Fixtures\TestBundle\Entity\RelatedToDummyFriend" is deprecated and will result in automatic short name deduplication in API Platform 5.x. Set "defaults.extra_properties.deduplicate_resource_short_names" to "true" in the API Platform configuration to enable it now.]]></issue>
41+
<issue><![CDATA[Since api-platform/core 4.2: Having multiple "#[ApiResource]" attributes with the same "shortName" "Dummy" on class "ApiPlatform\Tests\Fixtures\TestBundle\Entity\Dummy" is deprecated and will result in automatic short name deduplication in API Platform 5.x. Set "defaults.extra_properties.deduplicate_resource_short_names" to "true" in the API Platform configuration to enable it now.]]></issue>
42+
<issue><![CDATA[Since api-platform/core 4.2: Having multiple "#[ApiResource]" attributes with the same "shortName" "FourthLevel" on class "ApiPlatform\Tests\Fixtures\TestBundle\Entity\FourthLevel" is deprecated and will result in automatic short name deduplication in API Platform 5.x. Set "defaults.extra_properties.deduplicate_resource_short_names" to "true" in the API Platform configuration to enable it now.]]></issue>
43+
<issue><![CDATA[Since api-platform/core 4.2: Having multiple "#[ApiResource]" attributes with the same "shortName" "Employee" on class "ApiPlatform\Tests\Fixtures\TestBundle\Entity\Employee" is deprecated and will result in automatic short name deduplication in API Platform 5.x. Set "defaults.extra_properties.deduplicate_resource_short_names" to "true" in the API Platform configuration to enable it now.]]></issue>
44+
<issue><![CDATA[Since api-platform/core 4.2: Having multiple "#[ApiResource]" attributes with the same "shortName" "Company" on class "ApiPlatform\Tests\Fixtures\TestBundle\Entity\Company" is deprecated and will result in automatic short name deduplication in API Platform 5.x. Set "defaults.extra_properties.deduplicate_resource_short_names" to "true" in the API Platform configuration to enable it now.]]></issue>
45+
<issue><![CDATA[Since api-platform/core 4.2: Having multiple "#[ApiResource]" attributes with the same "shortName" "AttributeResource" on class "ApiPlatform\Tests\Fixtures\TestBundle\Entity\AttributeResource" is deprecated and will result in automatic short name deduplication in API Platform 5.x. Set "defaults.extra_properties.deduplicate_resource_short_names" to "true" in the API Platform configuration to enable it now.]]></issue>
46+
</line>
47+
</file>
3348
</files>

src/Hydra/Serializer/DocumentationNormalizer.php

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
2727
use ApiPlatform\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
2828
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
29-
use ApiPlatform\Metadata\Resource\ResourceMetadataCollection;
3029
use ApiPlatform\Metadata\ResourceClassResolverInterface;
3130
use ApiPlatform\Metadata\UrlGeneratorInterface;
3231
use ApiPlatform\Metadata\Util\TypeHelper;
@@ -76,16 +75,17 @@ public function normalize(mixed $data, ?string $format = null, array $context =
7675
foreach ($data->getResourceNameCollection() as $resourceClass) {
7776
$resourceMetadataCollection = $this->resourceMetadataFactory->create($resourceClass);
7877

79-
$resourceMetadata = $resourceMetadataCollection[0];
80-
if (true === $resourceMetadata->getHideHydraOperation()) {
81-
continue;
82-
}
78+
foreach ($resourceMetadataCollection as $resourceMetadata) {
79+
if (true === $resourceMetadata->getHideHydraOperation()) {
80+
continue;
81+
}
8382

84-
$shortName = $resourceMetadata->getShortName();
85-
$prefixedShortName = $resourceMetadata->getTypes()[0] ?? "#$shortName";
83+
$shortName = $resourceMetadata->getShortName();
84+
$prefixedShortName = $resourceMetadata->getTypes()[0] ?? "#$shortName";
8685

87-
$this->populateEntrypointProperties($resourceMetadata, $shortName, $prefixedShortName, $entrypointProperties, $hydraPrefix, $resourceMetadataCollection);
88-
$classes[] = $this->getClass($resourceClass, $resourceMetadata, $shortName, $prefixedShortName, $context, $hydraPrefix, $resourceMetadataCollection);
86+
$this->populateEntrypointProperties($resourceMetadata, $shortName, $prefixedShortName, $entrypointProperties, $hydraPrefix);
87+
$classes[] = $this->getClass($resourceClass, $resourceMetadata, $shortName, $prefixedShortName, $context, $hydraPrefix);
88+
}
8989
}
9090

9191
return $this->computeDoc($data, $this->getClasses($entrypointProperties, $classes, $hydraPrefix), $hydraPrefix);
@@ -94,9 +94,9 @@ public function normalize(mixed $data, ?string $format = null, array $context =
9494
/**
9595
* Populates entrypoint properties.
9696
*/
97-
private function populateEntrypointProperties(ApiResource $resourceMetadata, string $shortName, string $prefixedShortName, array &$entrypointProperties, string $hydraPrefix, ?ResourceMetadataCollection $resourceMetadataCollection = null): void
97+
private function populateEntrypointProperties(ApiResource $resourceMetadata, string $shortName, string $prefixedShortName, array &$entrypointProperties, string $hydraPrefix): void
9898
{
99-
$hydraCollectionOperations = $this->getHydraOperations(true, $resourceMetadataCollection, $hydraPrefix);
99+
$hydraCollectionOperations = $this->getHydraOperations(true, $resourceMetadata, $hydraPrefix);
100100
if (empty($hydraCollectionOperations)) {
101101
return;
102102
}
@@ -135,7 +135,7 @@ private function populateEntrypointProperties(ApiResource $resourceMetadata, str
135135
/**
136136
* Gets a Hydra class.
137137
*/
138-
private function getClass(string $resourceClass, ApiResource $resourceMetadata, string $shortName, string $prefixedShortName, array $context, string $hydraPrefix, ?ResourceMetadataCollection $resourceMetadataCollection = null): array
138+
private function getClass(string $resourceClass, ApiResource $resourceMetadata, string $shortName, string $prefixedShortName, array $context, string $hydraPrefix): array
139139
{
140140
$description = $resourceMetadata->getDescription();
141141
$isDeprecated = $resourceMetadata->getDeprecationReason();
@@ -145,7 +145,7 @@ private function getClass(string $resourceClass, ApiResource $resourceMetadata,
145145
'@type' => $hydraPrefix.'Class',
146146
$hydraPrefix.'title' => $shortName,
147147
$hydraPrefix.'supportedProperty' => $this->getHydraProperties($resourceClass, $resourceMetadata, $shortName, $prefixedShortName, $context, $hydraPrefix),
148-
$hydraPrefix.'supportedOperation' => $this->getHydraOperations(false, $resourceMetadataCollection, $hydraPrefix),
148+
$hydraPrefix.'supportedOperation' => $this->getHydraOperations(false, $resourceMetadata, $hydraPrefix),
149149
];
150150

151151
if (null !== $description) {
@@ -252,21 +252,19 @@ private function getHydraProperties(string $resourceClass, ApiResource $resource
252252
/**
253253
* Gets Hydra operations.
254254
*/
255-
private function getHydraOperations(bool $collection, ?ResourceMetadataCollection $resourceMetadataCollection = null, string $hydraPrefix = ContextBuilder::HYDRA_PREFIX): array
255+
private function getHydraOperations(bool $collection, ApiResource $resourceMetadata, string $hydraPrefix = ContextBuilder::HYDRA_PREFIX): array
256256
{
257257
$hydraOperations = [];
258-
foreach ($resourceMetadataCollection as $resourceMetadata) {
259-
foreach ($resourceMetadata->getOperations() as $operation) {
260-
if (true === $operation->getHideHydraOperation()) {
261-
continue;
262-
}
263-
264-
if (('POST' === $operation->getMethod() || $operation instanceof CollectionOperationInterface) !== $collection) {
265-
continue;
266-
}
258+
foreach ($resourceMetadata->getOperations() as $operation) {
259+
if (true === $operation->getHideHydraOperation()) {
260+
continue;
261+
}
267262

268-
$hydraOperations[] = $this->getHydraOperation($operation, $operation->getShortName(), $hydraPrefix);
263+
if (('POST' === $operation->getMethod() || $operation instanceof CollectionOperationInterface) !== $collection) {
264+
continue;
269265
}
266+
267+
$hydraOperations[] = $this->getHydraOperation($operation, $operation->getShortName(), $hydraPrefix);
270268
}
271269

272270
return $hydraOperations;

src/Hydra/Serializer/EntrypointNormalizer.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,6 @@ public function normalize(mixed $data, ?string $format = null, array $context =
5050
$resourceMetadata = $this->resourceMetadataFactory->create($resourceClass);
5151

5252
foreach ($resourceMetadata as $resource) {
53-
if ($resource->getExtraProperties()['is_alternate_resource_metadata'] ?? false) {
54-
continue;
55-
}
56-
5753
foreach ($resource->getOperations() as $operation) {
5854
$key = lcfirst($resource->getShortName());
5955
if (true === $operation->getHideHydraOperation() || !$operation instanceof CollectionOperationInterface || isset($entrypoint[$key])) {

0 commit comments

Comments
 (0)