Skip to content
Merged
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
5 changes: 3 additions & 2 deletions features/hydra/docs.feature
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,10 @@ Feature: Documentation support
And the value of the node "hydra:property.label" of the property "name" of the Hydra class "Dummy" is "name"
And the value of the node "hydra:property.domain" of the property "name" of the Hydra class "Dummy" is "#Dummy"
And the value of the node "hydra:property.range" of the property "name" of the Hydra class "Dummy" is "xsd:string"
And the value of the node "hydra:property.range" of the property "relatedDummy" of the Hydra class "Dummy" is "https://schema.org/Product"
And the value of the node "subClassOf" of the Hydra class "RelatedDummy" is "https://schema.org/Product"
And the value of the node "hydra:property.range" of the property "relatedDummy" of the Hydra class "Dummy" is "#RelatedDummy"
And the value of the node "hydra:property.owl:maxCardinality" of the property "relatedDummy" of the Hydra class "Dummy" is "1"
And the value of the node "hydra:property.range" of the property "relatedDummies" of the Hydra class "Dummy" is "https://schema.org/Product"
And the value of the node "hydra:property.range" of the property "relatedDummies" of the Hydra class "Dummy" is "#RelatedDummy"
And the value of the node "hydra:title" of the property "name" of the Hydra class "Dummy" is "name"
And the value of the node "hydra:description" of the property "name" of the Hydra class "Dummy" is "The dummy name"
# Operations
Expand Down
24 changes: 10 additions & 14 deletions src/Hydra/Serializer/DocumentationNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public function normalize(mixed $data, ?string $format = null, array $context =
}

$shortName = $resourceMetadata->getShortName();
$prefixedShortName = $resourceMetadata->getTypes()[0] ?? "#$shortName";
$prefixedShortName = "#$shortName";

$this->populateEntrypointProperties($resourceMetadata, $shortName, $prefixedShortName, $entrypointProperties, $hydraPrefix);
$classes[] = $this->getClass($resourceClass, $resourceMetadata, $shortName, $prefixedShortName, $context, $hydraPrefix);
Expand Down Expand Up @@ -156,6 +156,11 @@ private function getClass(string $resourceClass, ApiResource $resourceMetadata,
$class['subClassOf'] = 'Error';
}

$types = array_values(array_filter($resourceMetadata->getTypes() ?? [], static fn (string $type) => $type !== $prefixedShortName));
if ($types) {
$class['subClassOf'] = 1 === \count($types) ? $types[0] : $types;
}

if ($isDeprecated) {
$class['owl:deprecated'] = true;
}
Expand Down Expand Up @@ -408,12 +413,8 @@ private function getRange(ApiProperty $propertyMetadata): array|string|null
$resourceMetadata = $this->resourceMetadataFactory->create($className);
$operation = $resourceMetadata->getOperation();

if (!$operation instanceof HttpOperation || !$operation->getTypes()) {
if (!\in_array("#{$operation->getShortName()}", $types, true)) {
$types[] = "#{$operation->getShortName()}";
}
} else {
$types = array_unique(array_merge($types, $operation->getTypes()));
if (!\in_array("#{$operation->getShortName()}", $types, true)) {
$types[] = "#{$operation->getShortName()}";
}
}
// TODO: remove in 5.x
Expand Down Expand Up @@ -462,14 +463,9 @@ private function getRange(ApiProperty $propertyMetadata): array|string|null
$resourceMetadata = $this->resourceMetadataFactory->create($className);
$operation = $resourceMetadata->getOperation();

if (!$operation instanceof HttpOperation || !$operation->getTypes()) {
if (!\in_array("#{$operation->getShortName()}", $types, true)) {
$types[] = "#{$operation->getShortName()}";
}
break;
if (!\in_array("#{$operation->getShortName()}", $types, true)) {
$types[] = "#{$operation->getShortName()}";
}

$types = array_unique(array_merge($types, $operation->getTypes()));
break;
}
}
Expand Down
93 changes: 93 additions & 0 deletions src/Hydra/Tests/Serializer/DocumentationNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ private function doTestNormalize($resourceMetadataFactory = null): void
'hydra:writeable' => false,
],
],

'hydra:supportedOperation' => [
[
'@type' => ['hydra:Operation', 'schema:FindAction'],
Expand Down Expand Up @@ -777,6 +778,7 @@ public function testNormalizeWithoutPrefix(): void
'writeable' => false,
],
],

'supportedOperation' => [
[
'@type' => ['Operation', 'schema:FindAction'],
Expand Down Expand Up @@ -981,6 +983,97 @@ public function testNormalizeWithoutPrefix(): void
$this->assertEquals($expected, $documentationNormalizer->normalize($documentation, null, [ContextBuilder::HYDRA_CONTEXT_HAS_PREFIX => false]));
}

public function testNormalizeSubClassOfWithSchemaOrgTypes(): void
{
$title = 'Test Api';
$desc = 'test ApiGerard';
$version = '0.0.0';
$documentation = new Documentation(new ResourceNameCollection(['dummy' => 'dummy']), $title, $desc, $version);

$propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
$propertyNameCollectionFactoryProphecy->create('dummy', Argument::any())->willReturn(new PropertyNameCollection([]));

$resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataCollectionFactoryInterface::class);
$resourceMetadataFactoryProphecy->create('dummy')->willReturn(new ResourceMetadataCollection('dummy', [
(new ApiResource())->withShortName('AdminBook')->withTypes(['https://schema.org/Book'])->withOperations(new Operations([
'get' => (new Get())->withShortName('AdminBook'),
])),
(new ApiResource())->withShortName('Book')->withTypes(['https://schema.org/Book'])->withOperations(new Operations([
'get' => (new Get())->withShortName('Book'),
'get_collection' => (new GetCollection())->withShortName('Book'),
])),
]));

$resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
$resourceClassResolverProphecy->isResourceClass(Argument::type('string'))->willReturn(true);

$urlGenerator = $this->prophesize(UrlGeneratorInterface::class);
$urlGenerator->generate('api_entrypoint')->willReturn('/');
$urlGenerator->generate('api_doc', ['_format' => 'jsonld'])->willReturn('/doc');
$urlGenerator->generate('api_doc', ['_format' => 'jsonld'], 0)->willReturn('/doc');

$documentationNormalizer = new DocumentationNormalizer(
$resourceMetadataFactoryProphecy->reveal(),
$propertyNameCollectionFactoryProphecy->reveal(),
$this->prophesize(PropertyMetadataFactoryInterface::class)->reveal(),
$resourceClassResolverProphecy->reveal(),
$urlGenerator->reveal()
);

$doc = $documentationNormalizer->normalize($documentation);

// Both classes should have unique @id based on shortName
$adminClass = $doc['hydra:supportedClass'][0];
$bookClass = $doc['hydra:supportedClass'][1];

$this->assertSame('#AdminBook', $adminClass['@id']);
$this->assertSame('AdminBook', $adminClass['hydra:title']);
$this->assertSame('https://schema.org/Book', $adminClass['subClassOf']);

$this->assertSame('#Book', $bookClass['@id']);
$this->assertSame('Book', $bookClass['hydra:title']);
$this->assertSame('https://schema.org/Book', $bookClass['subClassOf']);
}

public function testNormalizeSubClassOfWithMultipleTypes(): void
{
$title = 'Test Api';
$desc = 'test';
$version = '0.0.0';
$documentation = new Documentation(new ResourceNameCollection(['dummy' => 'dummy']), $title, $desc, $version);

$propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
$propertyNameCollectionFactoryProphecy->create('dummy', Argument::any())->willReturn(new PropertyNameCollection([]));

$resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataCollectionFactoryInterface::class);
$resourceMetadataFactoryProphecy->create('dummy')->willReturn(new ResourceMetadataCollection('dummy', [
(new ApiResource())->withShortName('Product')->withTypes(['https://schema.org/Product', 'https://schema.org/Offer'])->withOperations(new Operations([
'get' => (new Get())->withShortName('Product'),
])),
]));

$resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
$urlGenerator = $this->prophesize(UrlGeneratorInterface::class);
$urlGenerator->generate('api_entrypoint')->willReturn('/');
$urlGenerator->generate('api_doc', ['_format' => 'jsonld'])->willReturn('/doc');
$urlGenerator->generate('api_doc', ['_format' => 'jsonld'], 0)->willReturn('/doc');

$documentationNormalizer = new DocumentationNormalizer(
$resourceMetadataFactoryProphecy->reveal(),
$propertyNameCollectionFactoryProphecy->reveal(),
$this->prophesize(PropertyMetadataFactoryInterface::class)->reveal(),
$resourceClassResolverProphecy->reveal(),
$urlGenerator->reveal()
);

$doc = $documentationNormalizer->normalize($documentation);
$class = $doc['hydra:supportedClass'][0];

$this->assertSame('#Product', $class['@id']);
// Multiple types should be an array
$this->assertSame(['https://schema.org/Product', 'https://schema.org/Offer'], $class['subClassOf']);
}

public function testNormalizeNoEntrypointAndHideHydraOperation(): void
{
$title = 'Test Api';
Expand Down
Loading