Skip to content

Commit 31d24ac

Browse files
authored
feat(hydra): read hydra:property from ApiProperty::jsonLdContext (#6240)
1 parent b79c7ae commit 31d24ac

File tree

2 files changed

+60
-4
lines changed

2 files changed

+60
-4
lines changed

src/Hydra/Serializer/DocumentationNormalizer.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -498,22 +498,22 @@ private function getProperty(ApiProperty $propertyMetadata, string $propertyName
498498
$iri = "#$shortName/$propertyName";
499499
}
500500

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

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

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

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

tests/Hydra/Serializer/DocumentationNormalizerTest.php

+56
Original file line numberDiff line numberDiff line change
@@ -669,4 +669,60 @@ public function testNormalizeInputOutputClass(): void
669669

670670
$this->assertEquals($expected, $documentationNormalizer->normalize($documentation));
671671
}
672+
673+
public function testHasHydraContext(): void
674+
{
675+
$title = 'Test Api';
676+
$desc = 'test ApiGerard';
677+
$version = '0.0.0';
678+
$documentation = new Documentation(new ResourceNameCollection(['dummy' => 'dummy']), $title, $desc, $version);
679+
680+
$propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
681+
$propertyNameCollectionFactoryProphecy->create('dummy', Argument::type('array'))->shouldBeCalled()->willReturn(new PropertyNameCollection(['name']));
682+
683+
$resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataCollectionFactoryInterface::class);
684+
$resourceMetadataFactoryProphecy->create('dummy')->shouldBeCalled()->willReturn(new ResourceMetadataCollection('dummy', [
685+
(new ApiResource())->withShortName('dummy')->withDescription('dummy')->withTypes(['#dummy'])->withOperations(new Operations([
686+
'get' => (new Get())->withTypes(['#dummy'])->withShortName('dummy')->withInput(['class' => 'inputClass'])->withOutput(['class' => 'outputClass']),
687+
])),
688+
]));
689+
690+
$propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
691+
$propertyMetadataFactoryProphecy->create('dummy', 'name', Argument::type('array'))->shouldBeCalled()->willReturn(
692+
(new ApiProperty())
693+
->withBuiltinTypes([new Type(Type::BUILTIN_TYPE_STRING)])
694+
->withDescription('b')
695+
->withReadable(true)
696+
->withWritable(true)
697+
->withJsonldContext([
698+
'hydra:property' => [
699+
'@type' => 'https://schema.org/Enumeration',
700+
],
701+
])
702+
);
703+
704+
$resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
705+
$resourceClassResolverProphecy->isResourceClass(Argument::type('string'))->willReturn(true);
706+
707+
$urlGenerator = $this->prophesize(UrlGeneratorInterface::class);
708+
$urlGenerator->generate('api_entrypoint')->willReturn('/')->shouldBeCalledTimes(1);
709+
$urlGenerator->generate('api_doc', ['_format' => 'jsonld'])->willReturn('/doc')->shouldBeCalledTimes(1);
710+
$urlGenerator->generate('api_doc', ['_format' => 'jsonld'], 0)->willReturn('/doc')->shouldBeCalledTimes(1);
711+
712+
$documentationNormalizer = new DocumentationNormalizer(
713+
$resourceMetadataFactoryProphecy->reveal(),
714+
$propertyNameCollectionFactoryProphecy->reveal(),
715+
$propertyMetadataFactoryProphecy->reveal(),
716+
$resourceClassResolverProphecy->reveal(),
717+
$urlGenerator->reveal()
718+
);
719+
720+
$this->assertEquals([
721+
'@id' => '#dummy/name',
722+
'@type' => 'https://schema.org/Enumeration',
723+
'rdfs:label' => 'name',
724+
'domain' => '#dummy',
725+
'range' => 'xmls:string',
726+
], $documentationNormalizer->normalize($documentation)['hydra:supportedClass'][0]['hydra:supportedProperty'][0]['hydra:property']);
727+
}
672728
}

0 commit comments

Comments
 (0)