Skip to content

Commit 9a3e6e2

Browse files
authored
Add deprecation support for Hydra (#1977)
* Add deprecation support for Hydra * Deprecate the class too
1 parent 7e1d63c commit 9a3e6e2

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed

features/bootstrap/HydraContext.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,14 @@ public function assertTheHydraClassNotExist(string $className)
7070
throw new ExpectationFailedException(sprintf('The class "%s" exists.', $className));
7171
}
7272

73+
/**
74+
* @Then the boolean value of the node :node of the Hydra class :class is true
75+
*/
76+
public function assertBooleanNodeValueIs(string $nodeName, string $className)
77+
{
78+
Assert::assertTrue($this->propertyAccessor->getValue($this->getClassInfo($className), $nodeName));
79+
}
80+
7381
/**
7482
* @Then the value of the node :node of the Hydra class :class is :value
7583
*/
@@ -81,6 +89,14 @@ public function assertNodeValueIs(string $nodeName, string $className, string $v
8189
);
8290
}
8391

92+
/**
93+
* @Then the boolean value of the node :node of the property :prop of the Hydra class :class is true
94+
*/
95+
public function assertPropertyNodeValueIsTrue(string $nodeName, string $propertyName, string $className)
96+
{
97+
Assert::assertTrue($this->propertyAccessor->getValue($this->getPropertyInfo($propertyName, $className), $nodeName));
98+
}
99+
84100
/**
85101
* @Then the value of the node :node of the property :prop of the Hydra class :class is :value
86102
*/
@@ -92,6 +108,14 @@ public function assertPropertyNodeValueIs(string $nodeName, string $propertyName
92108
);
93109
}
94110

111+
/**
112+
* @Then the boolean value of the node :node of the operation :operation of the Hydra class :class is true
113+
*/
114+
public function assertOperationNodeBooleanValueIs(string $nodeName, string $operationMethod, string $className)
115+
{
116+
Assert::assertTrue($this->propertyAccessor->getValue($this->getOperation($operationMethod, $className), $nodeName));
117+
}
118+
95119
/**
96120
* @Then the value of the node :node of the operation :operation of the Hydra class :class is :value
97121
*/

features/hydra/docs.feature

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,8 @@ Feature: Documentation support
8383
And the value of the node "hydra:title" of the operation "PUT" of the Hydra class "Dummy" is "Replaces the Dummy resource."
8484
And the value of the node "hydra:title" of the operation "DELETE" of the Hydra class "Dummy" is "Deletes the Dummy resource."
8585
And the value of the node "returns" of the operation "DELETE" of the Hydra class "Dummy" is "owl:Nothing"
86+
# Deprecations
87+
And the boolean value of the node "owl:deprecated" of the Hydra class "DeprecatedResource" is true
88+
And the boolean value of the node "owl:deprecated" of the property "deprecatedField" of the Hydra class "DeprecatedResource" is true
89+
And the boolean value of the node "owl:deprecated" of the property "The collection of DeprecatedResource resources" of the Hydra class "The API entrypoint" is true
90+
And the boolean value of the node "owl:deprecated" of the operation "GET" of the Hydra class "DeprecatedResource" is true

src/Hydra/Serializer/DocumentationNormalizer.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ private function populateEntrypointProperties(string $resourceClass, ResourceMet
9797
return;
9898
}
9999

100-
$entrypointProperties[] = [
100+
$entrypointProperty = [
101101
'@type' => 'hydra:SupportedProperty',
102102
'hydra:property' => [
103103
'@id' => sprintf('#Entrypoint/%s', lcfirst($shortName)),
@@ -119,6 +119,12 @@ private function populateEntrypointProperties(string $resourceClass, ResourceMet
119119
'hydra:readable' => true,
120120
'hydra:writable' => false,
121121
];
122+
123+
if ($resourceMetadata->getCollectionOperationAttribute('GET', 'deprecation_reason', null, true)) {
124+
$entrypointProperty['owl:deprecated'] = true;
125+
}
126+
127+
$entrypointProperties[] = $entrypointProperty;
122128
}
123129

124130
/**
@@ -146,6 +152,10 @@ private function getClass(string $resourceClass, ResourceMetadata $resourceMetad
146152
$class['hydra:description'] = $description;
147153
}
148154

155+
if ($resourceMetadata->getAttribute('deprecation_reason')) {
156+
$class['owl:deprecated'] = true;
157+
}
158+
149159
return $class;
150160
}
151161

@@ -263,6 +273,10 @@ private function getHydraOperation(string $resourceClass, ResourceMetadata $reso
263273
}
264274

265275
$hydraOperation = $operation['hydra_context'] ?? [];
276+
if ($resourceMetadata->getTypedOperationAttribute($operationType, $operationName, 'deprecation_reason', null, true)) {
277+
$hydraOperation['owl:deprecated'] = true;
278+
}
279+
266280
$shortName = $resourceMetadata->getShortName();
267281

268282
if ('GET' === $method && OperationType::COLLECTION === $operationType) {
@@ -504,6 +518,10 @@ private function getProperty(PropertyMetadata $propertyMetadata, string $propert
504518
$property['hydra:description'] = $description;
505519
}
506520

521+
if ($propertyMetadata->getAttribute('deprecation_reason')) {
522+
$property['owl:deprecated'] = true;
523+
}
524+
507525
return $property;
508526
}
509527

0 commit comments

Comments
 (0)