Skip to content

Commit

Permalink
feat(hydra): read hydra:property from ApiProperty::jsonLdContext (#6240)
Browse files Browse the repository at this point in the history
  • Loading branch information
soyuka committed Mar 23, 2024
1 parent b79c7ae commit 31d24ac
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/Hydra/Serializer/DocumentationNormalizer.php
Expand Up @@ -498,22 +498,22 @@ private function getProperty(ApiProperty $propertyMetadata, string $propertyName
$iri = "#$shortName/$propertyName";
}

$propertyData = [
$propertyData = ($propertyMetadata->getJsonldContext()['hydra:property'] ?? []) + [
'@id' => $iri,
'@type' => false === $propertyMetadata->isReadableLink() ? 'hydra:Link' : 'rdf:Property',
'rdfs:label' => $propertyName,
'domain' => $prefixedShortName,
];

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

if ($this->isSingleRelation($propertyMetadata)) {
if (!isset($propertyData['owl:maxCardinality']) && $this->isSingleRelation($propertyMetadata)) {
$propertyData['owl:maxCardinality'] = 1;
}

if (null !== $range = $this->getRange($propertyMetadata)) {
if (!isset($propertyData['range']) && null !== $range = $this->getRange($propertyMetadata)) {
$propertyData['range'] = $range;
}

Expand Down
56 changes: 56 additions & 0 deletions tests/Hydra/Serializer/DocumentationNormalizerTest.php
Expand Up @@ -669,4 +669,60 @@ public function testNormalizeInputOutputClass(): void

$this->assertEquals($expected, $documentationNormalizer->normalize($documentation));
}

public function testHasHydraContext(): 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::type('array'))->shouldBeCalled()->willReturn(new PropertyNameCollection(['name']));

$resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataCollectionFactoryInterface::class);
$resourceMetadataFactoryProphecy->create('dummy')->shouldBeCalled()->willReturn(new ResourceMetadataCollection('dummy', [
(new ApiResource())->withShortName('dummy')->withDescription('dummy')->withTypes(['#dummy'])->withOperations(new Operations([
'get' => (new Get())->withTypes(['#dummy'])->withShortName('dummy')->withInput(['class' => 'inputClass'])->withOutput(['class' => 'outputClass']),
])),
]));

$propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
$propertyMetadataFactoryProphecy->create('dummy', 'name', Argument::type('array'))->shouldBeCalled()->willReturn(
(new ApiProperty())
->withBuiltinTypes([new Type(Type::BUILTIN_TYPE_STRING)])
->withDescription('b')
->withReadable(true)
->withWritable(true)
->withJsonldContext([
'hydra:property' => [
'@type' => 'https://schema.org/Enumeration',
],
])
);

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

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

$documentationNormalizer = new DocumentationNormalizer(
$resourceMetadataFactoryProphecy->reveal(),
$propertyNameCollectionFactoryProphecy->reveal(),
$propertyMetadataFactoryProphecy->reveal(),
$resourceClassResolverProphecy->reveal(),
$urlGenerator->reveal()
);

$this->assertEquals([
'@id' => '#dummy/name',
'@type' => 'https://schema.org/Enumeration',
'rdfs:label' => 'name',
'domain' => '#dummy',
'range' => 'xmls:string',
], $documentationNormalizer->normalize($documentation)['hydra:supportedClass'][0]['hydra:supportedProperty'][0]['hydra:property']);
}
}

0 comments on commit 31d24ac

Please sign in to comment.