Skip to content

Commit ef248e4

Browse files
meyerbaptistesoyuka
authored andcommitted
Fix Hydra documentation
1 parent 0c31397 commit ef248e4

File tree

7 files changed

+30
-18
lines changed

7 files changed

+30
-18
lines changed

features/bootstrap/HydraContext.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,16 @@ public function assertOperationNodeValueIs($nodeName, $operationMethod, $classNa
9494
\PHPUnit_Framework_Assert::assertEquals($this->propertyAccessor->getValue($property, $nodeName), $value);
9595
}
9696

97+
/**
98+
* @Then the value of the node ":node" of the operation ":operation" of the Hydra class ":class" contains ":value"
99+
*/
100+
public function assertOperationNodeValueContains($nodeName, $operationMethod, $className, $value)
101+
{
102+
$property = $this->getOperation($operationMethod, $className);
103+
104+
\PHPUnit_Framework_Assert::assertContains($value, $this->propertyAccessor->getValue($property, $nodeName));
105+
}
106+
97107
/**
98108
* @Then :nb operations are available for Hydra class ":class"
99109
*/

features/hydra/collection.feature

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@ Feature: Collections support
1818
"@id": {"pattern": "^/dummies$"},
1919
"@type": {"pattern": "^hydra:Collection$"},
2020
"hydra:totalItems": {"type":"number", "maximum": 0},
21-
"hydra:itemsPerPage": {"type":"number", "maximum": 3},
22-
"hydra:firstPage": {"pattern": "^/dummies$"},
23-
"hydra:lastPage": {"pattern": "^/dummies$"},
2421
"hydra:member": {
2522
"type": "array",
2623
"maxItems": 0

features/hydra/docs.feature

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ Feature: Documentation support
7070
And the value of the node "hydra:title" of the property "name" of the Hydra class "Dummy" is "name"
7171
And the value of the node "hydra:description" of the property "name" of the Hydra class "Dummy" is "The dummy name"
7272
# Operations
73-
And the value of the node "@type" of the operation "GET" of the Hydra class "Dummy" is "hydra:Operation"
73+
And the value of the node "@type" of the operation "GET" of the Hydra class "Dummy" contains "hydra:Operation"
74+
And the value of the node "@type" of the operation "GET" of the Hydra class "Dummy" contains "schema:FindAction"
7475
And the value of the node "hydra:method" of the operation "GET" of the Hydra class "Dummy" is "GET"
7576
And the value of the node "hydra:title" of the operation "GET" of the Hydra class "Dummy" is "Retrieves Dummy resource."
7677
And the value of the node "rdfs:label" of the operation "GET" of the Hydra class "Dummy" is "Retrieves Dummy resource."

features/main/relation.feature

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,6 @@ Feature: Relations support
176176
"@id": {"pattern": "^/dummies$"},
177177
"@type": {"pattern": "^hydra:Collection$"},
178178
"hydra:totalItems": {"type":"number", "maximum": 1},
179-
"hydra:itemsPerPage": {"type":"number", "maximum": 3},
180179
"hydra:member": {
181180
"type": "array",
182181
"items": {
@@ -212,7 +211,6 @@ Feature: Relations support
212211
"@id": {"pattern": "^/dummies$"},
213212
"@type": {"pattern": "^hydra:Collection$"},
214213
"hydra:totalItems": {"type":"number", "maximum": 1},
215-
"hydra:itemsPerPage": {"type":"number", "maximum": 3},
216214
"hydra:member": {
217215
"type": "array",
218216
"items": {

src/Hydra/Serializer/DocumentationNormalizer.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ private function populateEntrypointProperties(string $resourceClass, ResourceMet
9696
'domain' => '#Entrypoint',
9797
'rdfs:label' => "The collection of $shortName resources",
9898
'rdfs:range' => [
99-
'hydra:PagedCollection',
99+
'hydra:Collection',
100100
[
101101
'owl:equivalentClass' => [
102102
'owl:onProperty' => 'hydra:member',
@@ -241,36 +241,38 @@ private function getHydraOperation(string $resourceClass, ResourceMetadata $reso
241241

242242
if ('GET' === $method && $collection) {
243243
$hydraOperation = [
244+
'@type' => ['hydra:Operation', 'schema:FindAction'],
244245
'hydra:title' => "Retrieves the collection of $shortName resources.",
245-
'returns' => 'hydra:PagedCollection',
246+
'returns' => 'hydra:Collection',
246247
] + $hydraOperation;
247248
} elseif ('GET' === $method) {
248249
$hydraOperation = [
250+
'@type' => ['hydra:Operation', 'schema:FindAction'],
249251
'hydra:title' => "Retrieves $shortName resource.",
250252
'returns' => $prefixedShortName,
251253
] + $hydraOperation;
252254
} elseif ('POST' === $method) {
253255
$hydraOperation = [
254-
'@type' => 'hydra:CreateResourceOperation',
256+
'@type' => ['hydra:Operation', 'schema:CreateAction'],
255257
'hydra:title' => "Creates a $shortName resource.",
256258
'returns' => $prefixedShortName,
257259
'expects' => $prefixedShortName,
258260
] + $hydraOperation;
259261
} elseif ('PUT' === $method) {
260262
$hydraOperation = [
261-
'@type' => 'hydra:ReplaceResourceOperation',
263+
'@type' => ['hydra:Operation', 'schema:ReplaceAction'],
262264
'hydra:title' => "Replaces the $shortName resource.",
263265
'returns' => $prefixedShortName,
264266
'expects' => $prefixedShortName,
265267
] + $hydraOperation;
266268
} elseif ('DELETE' === $method) {
267269
$hydraOperation = [
270+
'@type' => ['hydra:Operation', 'schema:DeleteAction'],
268271
'hydra:title' => "Deletes the $shortName resource.",
269272
'returns' => 'owl:Nothing',
270273
] + $hydraOperation;
271274
}
272275

273-
$hydraOperation['@type'] ?? $hydraOperation['@type'] = 'hydra:Operation';
274276
$hydraOperation['hydra:method'] ?? $hydraOperation['hydra:method'] = $method;
275277

276278
if (!isset($hydraOperation['rdfs:label']) && isset($hydraOperation['hydra:title'])) {
@@ -474,7 +476,7 @@ private function getProperty(PropertyMetadata $propertyMetadata, string $propert
474476
*/
475477
private function computeDoc(Documentation $object, array $classes): array
476478
{
477-
$doc = ['@context' => $this->getContext(), '@id' => $this->urlGenerator->generate('api_doc', ['_format' => self::FORMAT])];
479+
$doc = ['@context' => $this->getContext(), '@id' => $this->urlGenerator->generate('api_doc', ['_format' => self::FORMAT]), '@type' => 'hydra:ApiDocumentation'];
478480

479481
if ('' !== $object->getTitle()) {
480482
$doc['hydra:title'] = $object->getTitle();
@@ -504,6 +506,7 @@ private function getContext(): array
504506
'rdfs' => ContextBuilderInterface::RDFS_NS,
505507
'xmls' => ContextBuilderInterface::XML_NS,
506508
'owl' => ContextBuilderInterface::OWL_NS,
509+
'schema' => ContextBuilderInterface::SCHEMA_ORG_NS,
507510
'domain' => ['@id' => 'rdfs:domain', '@type' => '@id'],
508511
'range' => ['@id' => 'rdfs:range', '@type' => '@id'],
509512
'subClassOf' => ['@id' => 'rdfs:subClassOf', '@type' => '@id'],

src/JsonLd/ContextBuilderInterface.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ interface ContextBuilderInterface
2828
const RDFS_NS = 'http://www.w3.org/2000/01/rdf-schema#';
2929
const XML_NS = 'http://www.w3.org/2001/XMLSchema#';
3030
const OWL_NS = 'http://www.w3.org/2002/07/owl#';
31+
const SCHEMA_ORG_NS = 'http://schema.org/';
3132

3233
/**
3334
* Gets the base context.

tests/Hydra/Serializer/DocumentationNormalizerTest.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ public function testNormalize()
8282
'rdfs' => 'http://www.w3.org/2000/01/rdf-schema#',
8383
'xmls' => 'http://www.w3.org/2001/XMLSchema#',
8484
'owl' => 'http://www.w3.org/2002/07/owl#',
85+
'schema' => 'http://schema.org/',
8586
'domain' => [
8687
'@id' => 'rdfs:domain',
8788
'@type' => '@id',
@@ -104,6 +105,7 @@ public function testNormalize()
104105
],
105106
],
106107
'@id' => '/doc',
108+
'@type' => 'hydra:ApiDocumentation',
107109
'hydra:title' => 'Test Api',
108110
'hydra:description' => 'test ApiGerard',
109111
'hydra:supportedClass' => [
@@ -147,14 +149,14 @@ public function testNormalize()
147149
],
148150
'hydra:supportedOperation' => [
149151
0 => [
150-
'@type' => 'hydra:Operation',
152+
'@type' => ['hydra:Operation', 'schema:FindAction'],
151153
'hydra:method' => 'GET',
152154
'hydra:title' => 'Retrieves dummy resource.',
153155
'rdfs:label' => 'Retrieves dummy resource.',
154156
'returns' => '#dummy',
155157
],
156158
1 => [
157-
'@type' => 'hydra:ReplaceResourceOperation',
159+
'@type' => ['hydra:Operation', 'schema:ReplaceAction'],
158160
'expects' => '#dummy',
159161
'hydra:method' => 'PUT',
160162
'hydra:title' => 'Replaces the dummy resource.',
@@ -176,7 +178,7 @@ public function testNormalize()
176178
'rdfs:label' => 'The collection of dummy resources',
177179
'domain' => '#Entrypoint',
178180
'rdfs:range' => [
179-
'hydra:PagedCollection',
181+
'hydra:Collection',
180182
[
181183
'owl:equivalentClass' => [
182184
'owl:onProperty' => 'hydra:member',
@@ -186,14 +188,14 @@ public function testNormalize()
186188
],
187189
'hydra:supportedOperation' => [
188190
0 => [
189-
'@type' => 'hydra:Operation',
191+
'@type' => ['hydra:Operation', 'schema:FindAction'],
190192
'hydra:method' => 'GET',
191193
'hydra:title' => 'Retrieves the collection of dummy resources.',
192194
'rdfs:label' => 'Retrieves the collection of dummy resources.',
193-
'returns' => 'hydra:PagedCollection',
195+
'returns' => 'hydra:Collection',
194196
],
195197
1 => [
196-
'@type' => 'hydra:CreateResourceOperation',
198+
'@type' => ['hydra:Operation', 'schema:CreateAction'],
197199
'expects' => '#dummy',
198200
'hydra:method' => 'POST',
199201
'hydra:title' => 'Creates a dummy resource.',

0 commit comments

Comments
 (0)