Skip to content

Commit 3984c68

Browse files
committed
Add support for input and output classes to Hydra
1 parent 5b5e6b7 commit 3984c68

File tree

2 files changed

+329
-16
lines changed

2 files changed

+329
-16
lines changed

src/Hydra/Serializer/DocumentationNormalizer.php

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -181,18 +181,31 @@ private function getPropertyNameCollectionFactoryContext(ResourceMetadata $resou
181181
*/
182182
private function getHydraProperties(string $resourceClass, ResourceMetadata $resourceMetadata, string $shortName, string $prefixedShortName, array $context): array
183183
{
184-
$properties = [];
185-
foreach ($this->propertyNameCollectionFactory->create($resourceClass, $this->getPropertyNameCollectionFactoryContext($resourceMetadata)) as $propertyName) {
186-
$propertyMetadata = $this->propertyMetadataFactory->create($resourceClass, $propertyName);
187-
if (true === $propertyMetadata->isIdentifier() && false === $propertyMetadata->isWritable()) {
188-
continue;
184+
$classes = [];
185+
foreach ($resourceMetadata->getCollectionOperations() as $operationName => $operation) {
186+
if (false !== $class = $resourceMetadata->getCollectionOperationAttribute($operationName, 'input_class', $resourceClass, true)) {
187+
$classes[$class] = true;
189188
}
190189

191-
if ($this->nameConverter) {
192-
$propertyName = $this->nameConverter->normalize($propertyName, $resourceClass, self::FORMAT, $context);
190+
if (false !== $class = $resourceMetadata->getCollectionOperationAttribute($operationName, 'output_class', $resourceClass, true)) {
191+
$classes[$class] = true;
193192
}
193+
}
194194

195-
$properties[] = $this->getProperty($propertyMetadata, $propertyName, $prefixedShortName, $shortName);
195+
$properties = [];
196+
foreach ($classes as $class => $v) {
197+
foreach ($this->propertyNameCollectionFactory->create($class, $this->getPropertyNameCollectionFactoryContext($resourceMetadata)) as $propertyName) {
198+
$propertyMetadata = $this->propertyMetadataFactory->create($class, $propertyName);
199+
if (true === $propertyMetadata->isIdentifier() && false === $propertyMetadata->isWritable()) {
200+
continue;
201+
}
202+
203+
if ($this->nameConverter) {
204+
$propertyName = $this->nameConverter->normalize($propertyName, $class, self::FORMAT, $context);
205+
}
206+
207+
$properties[] = $this->getProperty($propertyMetadata, $propertyName, $prefixedShortName, $shortName);
208+
}
196209
}
197210

198211
return $properties;
@@ -244,6 +257,8 @@ private function getHydraOperation(string $resourceClass, ResourceMetadata $reso
244257
}
245258

246259
$shortName = $resourceMetadata->getShortName();
260+
$inputClass = $resourceMetadata->getTypedOperationAttribute($operationType, $operationName, 'input_class', null, true);
261+
$outputClass = $resourceMetadata->getTypedOperationAttribute($operationType, $operationName, 'output_class', null, true);
247262

248263
if ('GET' === $method && OperationType::COLLECTION === $operationType) {
249264
$hydraOperation += [
@@ -255,34 +270,34 @@ private function getHydraOperation(string $resourceClass, ResourceMetadata $reso
255270
$hydraOperation += [
256271
'@type' => ['hydra:Operation', 'schema:FindAction'],
257272
'hydra:title' => $subresourceMetadata && $subresourceMetadata->isCollection() ? "Retrieves the collection of $shortName resources." : "Retrieves a $shortName resource.",
258-
'returns' => "#$shortName",
273+
'returns' => false === $outputClass ? 'owl:Nothing' : "#$shortName",
259274
];
260275
} elseif ('GET' === $method) {
261276
$hydraOperation += [
262277
'@type' => ['hydra:Operation', 'schema:FindAction'],
263278
'hydra:title' => "Retrieves $shortName resource.",
264-
'returns' => $prefixedShortName,
279+
'returns' => false === $outputClass ? 'owl:Nothing' : $prefixedShortName,
265280
];
266281
} elseif ('PATCH' === $method) {
267282
$hydraOperation += [
268283
'@type' => 'hydra:Operation',
269284
'hydra:title' => "Updates the $shortName resource.",
270-
'returns' => $prefixedShortName,
271-
'expects' => $prefixedShortName,
285+
'returns' => false === $outputClass ? 'owl:Nothing' : $prefixedShortName,
286+
'expects' => false === $inputClass ? 'owl:Nothing' : $prefixedShortName,
272287
];
273288
} elseif ('POST' === $method) {
274289
$hydraOperation += [
275290
'@type' => ['hydra:Operation', 'schema:CreateAction'],
276291
'hydra:title' => "Creates a $shortName resource.",
277-
'returns' => $prefixedShortName,
278-
'expects' => $prefixedShortName,
292+
'returns' => false === $outputClass ? 'owl:Nothing' : $prefixedShortName,
293+
'expects' => false === $inputClass ? 'owl:Nothing' : $prefixedShortName,
279294
];
280295
} elseif ('PUT' === $method) {
281296
$hydraOperation += [
282297
'@type' => ['hydra:Operation', 'schema:ReplaceAction'],
283298
'hydra:title' => "Replaces the $shortName resource.",
284-
'returns' => $prefixedShortName,
285-
'expects' => $prefixedShortName,
299+
'returns' => false === $outputClass ? 'owl:Nothing' : $prefixedShortName,
300+
'expects' => false === $inputClass ? 'owl:Nothing' : $prefixedShortName,
286301
];
287302
} elseif ('DELETE' === $method) {
288303
$hydraOperation += [

0 commit comments

Comments
 (0)