Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 96 additions & 12 deletions src/Hydra/Serializer/DocumentationNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use ApiPlatform\Metadata\ResourceClassResolverInterface;
use ApiPlatform\Metadata\UrlGeneratorInterface;
use ApiPlatform\Metadata\Util\TypeHelper;
use ApiPlatform\Metadata\Util\UriTemplateHelper;
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
Expand Down Expand Up @@ -71,19 +72,31 @@ public function normalize(mixed $data, ?string $format = null, array $context =
$entrypointProperties = [];
$hydraPrefix = $this->getHydraPrefix($context + $this->defaultContext);

$resourceMetadataCollections = [];
foreach ($data->getResourceNameCollection() as $resourceClass) {
$resourceMetadataCollection = $this->resourceMetadataFactory->create($resourceClass);
$resourceMetadataCollections[$resourceClass] = $this->resourceMetadataFactory->create($resourceClass);
}

$attachedOperations = [];
foreach ($resourceMetadataCollections as $resourceClass => $resourceMetadataCollection) {
foreach ($resourceMetadataCollection as $resourceMetadata) {
if (true === $resourceMetadata->getHideHydraOperation()) {
continue;
}

$shortName = $resourceMetadata->getShortName();
$prefixedShortName = "#$shortName";
$classes[] = $this->getClass($resourceClass, $resourceMetadata, $shortName, "#$shortName", $context, $hydraPrefix, $attachedOperations);
}
}

$this->populateEntrypointProperties($resourceMetadata, $shortName, $prefixedShortName, $entrypointProperties, $hydraPrefix);
$classes[] = $this->getClass($resourceClass, $resourceMetadata, $shortName, $prefixedShortName, $context, $hydraPrefix);
foreach ($resourceMetadataCollections as $resourceMetadataCollection) {
foreach ($resourceMetadataCollection as $resourceMetadata) {
if (true === $resourceMetadata->getHideHydraOperation()) {
continue;
}

$shortName = $resourceMetadata->getShortName();
$this->populateEntrypointProperties($resourceMetadata, $shortName, "#$shortName", $entrypointProperties, $hydraPrefix, $attachedOperations);
}
}

Expand All @@ -93,9 +106,9 @@ public function normalize(mixed $data, ?string $format = null, array $context =
/**
* Populates entrypoint properties.
*/
private function populateEntrypointProperties(ApiResource $resourceMetadata, string $shortName, string $prefixedShortName, array &$entrypointProperties, string $hydraPrefix): void
private function populateEntrypointProperties(ApiResource $resourceMetadata, string $shortName, string $prefixedShortName, array &$entrypointProperties, string $hydraPrefix, array $attachedOperations): void
{
$hydraCollectionOperations = $this->getHydraOperations(true, $resourceMetadata, $hydraPrefix);
$hydraCollectionOperations = $this->getHydraOperations(true, $resourceMetadata, $hydraPrefix, $attachedOperations);
if (empty($hydraCollectionOperations)) {
return;
}
Expand Down Expand Up @@ -130,7 +143,7 @@ private function populateEntrypointProperties(ApiResource $resourceMetadata, str
/**
* Gets a Hydra class.
*/
private function getClass(string $resourceClass, ApiResource $resourceMetadata, string $shortName, string $prefixedShortName, array $context, string $hydraPrefix): array
private function getClass(string $resourceClass, ApiResource $resourceMetadata, string $shortName, string $prefixedShortName, array $context, string $hydraPrefix, array &$attachedOperations = []): array
{
$description = $resourceMetadata->getDescription();
$isDeprecated = $resourceMetadata->getDeprecationReason();
Expand All @@ -139,7 +152,7 @@ private function getClass(string $resourceClass, ApiResource $resourceMetadata,
'@id' => $prefixedShortName,
'@type' => $hydraPrefix.'Class',
$hydraPrefix.'title' => $shortName,
$hydraPrefix.'supportedProperty' => $this->getHydraProperties($resourceClass, $resourceMetadata, $shortName, $prefixedShortName, $context, $hydraPrefix),
$hydraPrefix.'supportedProperty' => $this->getHydraProperties($resourceClass, $resourceMetadata, $shortName, $prefixedShortName, $context, $hydraPrefix, $attachedOperations),
$hydraPrefix.'supportedOperation' => $this->getHydraOperations(false, $resourceMetadata, $hydraPrefix),
];

Expand Down Expand Up @@ -208,7 +221,7 @@ private function getPropertyMetadataFactoryContext(ApiResource $resourceMetadata
/**
* Gets Hydra properties.
*/
private function getHydraProperties(string $resourceClass, ApiResource $resourceMetadata, string $shortName, string $prefixedShortName, array $context, string $hydraPrefix = ContextBuilder::HYDRA_PREFIX): array
private function getHydraProperties(string $resourceClass, ApiResource $resourceMetadata, string $shortName, string $prefixedShortName, array $context, string $hydraPrefix = ContextBuilder::HYDRA_PREFIX, array &$attachedOperations = []): array
{
$classes = [];

Expand Down Expand Up @@ -250,7 +263,9 @@ private function getHydraProperties(string $resourceClass, ApiResource $resource
continue;
}

$properties[] = $this->getProperty($propertyMetadata, $propertyName, $prefixedShortName, $shortName, $hydraPrefix);
$operations = false === $propertyMetadata->isReadable() ? [] : $this->getUriTemplateOperations($propertyMetadata, $hydraPrefix, $attachedOperations);

$properties[] = $this->getProperty($propertyMetadata, $propertyName, $prefixedShortName, $shortName, $hydraPrefix, $operations);
}
}

Expand All @@ -260,14 +275,18 @@ private function getHydraProperties(string $resourceClass, ApiResource $resource
/**
* Gets Hydra operations.
*/
private function getHydraOperations(bool $collection, ApiResource $resourceMetadata, string $hydraPrefix = ContextBuilder::HYDRA_PREFIX): array
private function getHydraOperations(bool $collection, ApiResource $resourceMetadata, string $hydraPrefix = ContextBuilder::HYDRA_PREFIX, array $attachedOperations = []): array
{
$hydraOperations = [];
foreach ($resourceMetadata->getOperations() as $operation) {
if (true === $operation->getHideHydraOperation() || $operation instanceof NotExposed) {
continue;
}

if (isset($attachedOperations[$this->getAttachedOperationKey($operation)])) {
continue;
}

if (('POST' === $operation->getMethod() || $operation instanceof CollectionOperationInterface) !== $collection) {
continue;
}
Expand All @@ -278,6 +297,63 @@ private function getHydraOperations(bool $collection, ApiResource $resourceMetad
return $hydraOperations;
}

/**
* Gets the collection operations a property exposes through its URI template, as AbstractItemNormalizer resolves them.
*
* Operations with URI variables are not reachable from the entrypoint, they are recorded in $attachedOperations to be documented only on the link.
*
* @return list<array<string, mixed>>
*/
private function getUriTemplateOperations(ApiProperty $propertyMetadata, string $hydraPrefix, array &$attachedOperations): array
{
if (null === $uriTemplate = $propertyMetadata->getUriTemplate()) {
return [];
}

$nativeType = $propertyMetadata->getNativeType();
if ($nativeType?->isSatisfiedBy(static fn ($t) => $t instanceof CollectionType)) {
$nativeType = TypeHelper::getCollectionValueType($nativeType);
}

$className = null;
if (!$nativeType?->isSatisfiedBy(function (Type $type) use (&$className): bool {
return $type instanceof ObjectType && $this->resourceClassResolver->isResourceClass($className = $type->getClassName());
}) || null === $className) {
return [];
}

$strippedUriTemplate = UriTemplateHelper::withoutFormatSuffix($uriTemplate);
$hydraOperations = [];
foreach ($this->resourceMetadataFactory->create($className) as $resourceMetadata) {
foreach ($resourceMetadata->getOperations() ?? [] as $operation) {
if (true === $operation->getHideHydraOperation() || $operation instanceof NotExposed) {
continue;
}

if (null === $operation->getUriTemplate() || UriTemplateHelper::withoutFormatSuffix($operation->getUriTemplate()) !== $strippedUriTemplate) {
continue;
}

if ('POST' !== $operation->getMethod() && !$operation instanceof CollectionOperationInterface) {
continue;
}

if ($operation->getUriVariables()) {
$attachedOperations[$this->getAttachedOperationKey($operation)] = true;
}

$hydraOperations[] = $this->getHydraOperation($operation, $operation->getShortName(), $hydraPrefix);
}
}

return $hydraOperations;
}

private function getAttachedOperationKey(HttpOperation $operation): string
{
return $operation->getShortName().' '.$operation->getMethod().' '.UriTemplateHelper::withoutFormatSuffix($operation->getUriTemplate() ?? '');
}

/**
* Gets and populates if applicable a Hydra operation.
*/
Expand Down Expand Up @@ -511,7 +587,7 @@ private function getClasses(array $entrypointProperties, array $classes, string
/**
* Gets a property definition.
*/
private function getProperty(ApiProperty $propertyMetadata, string $propertyName, string $prefixedShortName, string $shortName, string $hydraPrefix): array
private function getProperty(ApiProperty $propertyMetadata, string $propertyName, string $prefixedShortName, string $shortName, string $hydraPrefix, array $operations = []): array
{
if ($iri = $propertyMetadata->getIris()) {
$iri = 1 === (is_countable($iri) ? \count($iri) : 0) ? $iri[0] : $iri;
Expand All @@ -528,6 +604,14 @@ private function getProperty(ApiProperty $propertyMetadata, string $propertyName
'label' => $propertyName,
];

if ($operations) {
if (!isset($propertyMetadata->getJsonldContext()[$hydraPrefix.'property']['@type'])) {
$propertyData['@type'] = $hydraPrefix.'Link';
}

$propertyData[$hydraPrefix.'supportedOperation'] = $operations;
}

if (!isset($propertyData['owl:deprecated']) && $propertyMetadata->getDeprecationReason()) {
$propertyData['owl:deprecated'] = true;
}
Expand Down
Loading
Loading