Skip to content

Commit fe34348

Browse files
committed
ability to disable hydra prop
1 parent 4c19bcd commit fe34348

File tree

4 files changed

+116
-85
lines changed

4 files changed

+116
-85
lines changed

src/Hydra/Serializer/DocumentationNormalizer.php

Lines changed: 82 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ public function normalize(mixed $object, ?string $format = null, array $context
8080
}
8181

8282
$shortName = $resourceMetadata->getShortName();
83-
8483
$prefixedShortName = $resourceMetadata->getTypes()[0] ?? "#$shortName";
84+
8585
$this->populateEntrypointProperties($resourceMetadata, $shortName, $prefixedShortName, $entrypointProperties, $hydraPrefix, $resourceMetadataCollection);
8686
$classes[] = $this->getClass($resourceClass, $resourceMetadata, $shortName, $prefixedShortName, $context, $hydraPrefix, $resourceMetadataCollection);
8787
}
@@ -105,7 +105,7 @@ private function populateEntrypointProperties(ApiResource $resourceMetadata, str
105105
'@id' => \sprintf('#Entrypoint/%s', lcfirst($shortName)),
106106
'@type' => $hydraPrefix.'Link',
107107
'domain' => '#Entrypoint',
108-
'rdfs:label' => "get{$shortName}Collection",
108+
'title' => "{$shortName}CollectionEntrypoint",
109109
'rdfs:range' => [
110110
['@id' => $hydraPrefix.'Collection'],
111111
[
@@ -117,7 +117,8 @@ private function populateEntrypointProperties(ApiResource $resourceMetadata, str
117117
],
118118
$hydraPrefix.'supportedOperation' => $hydraCollectionOperations,
119119
],
120-
$hydraPrefix.'title' => "The collection of $shortName resources",
120+
$hydraPrefix.'title' => "get{$shortName}Collection",
121+
$hydraPrefix.'description' => "The collection of $shortName resources",
121122
$hydraPrefix.'readable' => true,
122123
$hydraPrefix.'writeable' => false,
123124
];
@@ -140,7 +141,6 @@ private function getClass(string $resourceClass, ApiResource $resourceMetadata,
140141
$class = [
141142
'@id' => $prefixedShortName,
142143
'@type' => $hydraPrefix.'Class',
143-
'rdfs:label' => $shortName,
144144
$hydraPrefix.'title' => $shortName,
145145
$hydraPrefix.'supportedProperty' => $this->getHydraProperties($resourceClass, $resourceMetadata, $shortName, $prefixedShortName, $context, $hydraPrefix),
146146
$hydraPrefix.'supportedOperation' => $this->getHydraOperations(false, $resourceMetadataCollection, $hydraPrefix),
@@ -232,6 +232,10 @@ private function getHydraProperties(string $resourceClass, ApiResource $resource
232232
$propertyName = $this->nameConverter->normalize($propertyName, $class, self::FORMAT, $context);
233233
}
234234

235+
if (false === $propertyMetadata->getHydra()) {
236+
continue;
237+
}
238+
235239
$properties[] = $this->getProperty($propertyMetadata, $propertyName, $prefixedShortName, $shortName, $hydraPrefix);
236240
}
237241
}
@@ -254,6 +258,7 @@ private function getHydraOperations(bool $collection, ?ResourceMetadataCollectio
254258
if (('POST' === $operation->getMethod() || $operation instanceof CollectionOperationInterface) !== $collection) {
255259
continue;
256260
}
261+
257262
$hydraOperations[] = $this->getHydraOperation($operation, $operation->getShortName(), $hydraPrefix);
258263
}
259264
}
@@ -283,50 +288,46 @@ private function getHydraOperation(HttpOperation $operation, string $prefixedSho
283288
if ('GET' === $method && $operation instanceof CollectionOperationInterface) {
284289
$hydraOperation += [
285290
'@type' => [$hydraPrefix.'Operation', 'schema:FindAction'],
286-
$hydraPrefix.'title' => "Retrieves the collection of $shortName resources.",
291+
$hydraPrefix.'description' => "Retrieves the collection of $shortName resources.",
287292
'returns' => null === $outputClass ? 'owl:Nothing' : $hydraPrefix.'Collection',
288293
];
289294
} elseif ('GET' === $method) {
290295
$hydraOperation += [
291296
'@type' => [$hydraPrefix.'Operation', 'schema:FindAction'],
292-
$hydraPrefix.'title' => "Retrieves a $shortName resource.",
297+
$hydraPrefix.'description' => "Retrieves a $shortName resource.",
293298
'returns' => null === $outputClass ? 'owl:Nothing' : $prefixedShortName,
294299
];
295300
} elseif ('PATCH' === $method) {
296301
$hydraOperation += [
297302
'@type' => $hydraPrefix.'Operation',
298-
$hydraPrefix.'title' => "Updates the $shortName resource.",
303+
$hydraPrefix.'description' => "Updates the $shortName resource.",
299304
'returns' => null === $outputClass ? 'owl:Nothing' : $prefixedShortName,
300305
'expects' => null === $inputClass ? 'owl:Nothing' : $prefixedShortName,
301306
];
302307
} elseif ('POST' === $method) {
303308
$hydraOperation += [
304309
'@type' => [$hydraPrefix.'Operation', 'schema:CreateAction'],
305-
$hydraPrefix.'title' => "Creates a $shortName resource.",
310+
$hydraPrefix.'description' => "Creates a $shortName resource.",
306311
'returns' => null === $outputClass ? 'owl:Nothing' : $prefixedShortName,
307312
'expects' => null === $inputClass ? 'owl:Nothing' : $prefixedShortName,
308313
];
309314
} elseif ('PUT' === $method) {
310315
$hydraOperation += [
311316
'@type' => [$hydraPrefix.'Operation', 'schema:ReplaceAction'],
312-
$hydraPrefix.'title' => "Replaces the $shortName resource.",
317+
$hydraPrefix.'description' => "Replaces the $shortName resource.",
313318
'returns' => null === $outputClass ? 'owl:Nothing' : $prefixedShortName,
314319
'expects' => null === $inputClass ? 'owl:Nothing' : $prefixedShortName,
315320
];
316321
} elseif ('DELETE' === $method) {
317322
$hydraOperation += [
318323
'@type' => [$hydraPrefix.'Operation', 'schema:DeleteAction'],
319-
$hydraPrefix.'title' => "Deletes the $shortName resource.",
324+
$hydraPrefix.'description' => "Deletes the $shortName resource.",
320325
'returns' => 'owl:Nothing',
321326
];
322327
}
323328

324-
$hydraOperation[$hydraPrefix.'method'] ?? $hydraOperation[$hydraPrefix.'method'] = $method;
325-
326-
if (!isset($hydraOperation['rdfs:label'])) {
327-
// we know that this name is unique
328-
$hydraOperation['rdfs:label'] = $operation->getName();
329-
}
329+
$hydraOperation[$hydraPrefix.'method'] ??= $method;
330+
$hydraOperation[$hydraPrefix.'title'] ??= strtolower($method) . $shortName . ($operation instanceof CollectionOperationInterface ? 'Collection' : '');
330331

331332
ksort($hydraOperation);
332333

@@ -435,77 +436,78 @@ private function getClasses(array $entrypointProperties, array $classes, string
435436
$classes[] = [
436437
'@id' => '#Entrypoint',
437438
'@type' => $hydraPrefix.'Class',
438-
$hydraPrefix.'title' => 'The API entrypoint',
439+
$hydraPrefix.'title' => 'Entrypoint',
440+
$hydraPrefix.'description' => 'API Entrypoint',
439441
$hydraPrefix.'supportedProperty' => $entrypointProperties,
440442
$hydraPrefix.'supportedOperation' => [
441443
'@type' => $hydraPrefix.'Operation',
442444
$hydraPrefix.'method' => 'GET',
443-
'title' => 'The API entrypoint.',
444-
'rdfs:label' => 'getEntrypoint',
445-
'returns' => 'EntryPoint',
445+
$hydraPrefix.'title' => 'Entrypoint',
446+
'rdfs:label' => 'index',
447+
$hydraPrefix.'returns' => 'EntryPoint',
446448
],
447449
];
448450

449451
// Constraint violation
450-
$classes[] = [
451-
'@id' => '#ConstraintViolation',
452-
'@type' => $hydraPrefix.'Class',
453-
$hydraPrefix.'title' => 'A constraint violation',
454-
$hydraPrefix.'supportedProperty' => [
455-
[
456-
'@type' => $hydraPrefix.'SupportedProperty',
457-
$hydraPrefix.'property' => [
458-
'@id' => '#ConstraintViolation/propertyPath',
459-
'@type' => 'rdf:Property',
460-
'rdfs:label' => 'propertyPath',
461-
'domain' => '#ConstraintViolation',
462-
'range' => 'xmls:string',
463-
],
464-
$hydraPrefix.'title' => 'propertyPath',
465-
$hydraPrefix.'description' => 'The property path of the violation',
466-
$hydraPrefix.'readable' => true,
467-
$hydraPrefix.'writeable' => false,
468-
],
469-
[
470-
'@type' => $hydraPrefix.'SupportedProperty',
471-
$hydraPrefix.'property' => [
472-
'@id' => '#ConstraintViolation/message',
473-
'@type' => 'rdf:Property',
474-
'rdfs:label' => 'message',
475-
'domain' => '#ConstraintViolation',
476-
'range' => 'xmls:string',
477-
],
478-
$hydraPrefix.'title' => 'message',
479-
$hydraPrefix.'description' => 'The message associated with the violation',
480-
$hydraPrefix.'readable' => true,
481-
$hydraPrefix.'writeable' => false,
482-
],
483-
],
484-
];
485-
486-
// Constraint violation list
487-
$classes[] = [
488-
'@id' => '#ConstraintViolationList',
489-
'@type' => $hydraPrefix.'Class',
490-
'subClassOf' => $hydraPrefix.'Error',
491-
$hydraPrefix.'title' => 'A constraint violation list',
492-
$hydraPrefix.'supportedProperty' => [
493-
[
494-
'@type' => $hydraPrefix.'SupportedProperty',
495-
$hydraPrefix.'property' => [
496-
'@id' => '#ConstraintViolationList/violations',
497-
'@type' => 'rdf:Property',
498-
'rdfs:label' => 'violations',
499-
'domain' => '#ConstraintViolationList',
500-
'range' => '#ConstraintViolation',
501-
],
502-
$hydraPrefix.'title' => 'violations',
503-
$hydraPrefix.'description' => 'The violations',
504-
$hydraPrefix.'readable' => true,
505-
$hydraPrefix.'writeable' => false,
506-
],
507-
],
508-
];
452+
// $classes[] = [
453+
// '@id' => '#ConstraintViolation',
454+
// '@type' => $hydraPrefix.'Class',
455+
// $hydraPrefix.'title' => 'A constraint violation',
456+
// $hydraPrefix.'supportedProperty' => [
457+
// [
458+
// '@type' => $hydraPrefix.'SupportedProperty',
459+
// $hydraPrefix.'property' => [
460+
// '@id' => '#ConstraintViolation/propertyPath',
461+
// '@type' => 'rdf:Property',
462+
// 'rdfs:label' => 'propertyPath',
463+
// 'domain' => '#ConstraintViolation',
464+
// 'range' => 'xmls:string',
465+
// ],
466+
// $hydraPrefix.'title' => 'propertyPath',
467+
// $hydraPrefix.'description' => 'The property path of the violation',
468+
// $hydraPrefix.'readable' => true,
469+
// $hydraPrefix.'writeable' => false,
470+
// ],
471+
// [
472+
// '@type' => $hydraPrefix.'SupportedProperty',
473+
// $hydraPrefix.'property' => [
474+
// '@id' => '#ConstraintViolation/message',
475+
// '@type' => 'rdf:Property',
476+
// 'rdfs:label' => 'message',
477+
// 'domain' => '#ConstraintViolation',
478+
// 'range' => 'xmls:string',
479+
// ],
480+
// $hydraPrefix.'title' => 'message',
481+
// $hydraPrefix.'description' => 'The message associated with the violation',
482+
// $hydraPrefix.'readable' => true,
483+
// $hydraPrefix.'writeable' => false,
484+
// ],
485+
// ],
486+
// ];
487+
//
488+
// // Constraint violation list
489+
// $classes[] = [
490+
// '@id' => '#ConstraintViolationList',
491+
// '@type' => $hydraPrefix.'Class',
492+
// 'subClassOf' => $hydraPrefix.'Error',
493+
// $hydraPrefix.'title' => 'A constraint violation list',
494+
// $hydraPrefix.'supportedProperty' => [
495+
// [
496+
// '@type' => $hydraPrefix.'SupportedProperty',
497+
// $hydraPrefix.'property' => [
498+
// '@id' => '#ConstraintViolationList/violations',
499+
// '@type' => 'rdf:Property',
500+
// 'rdfs:label' => 'violations',
501+
// 'domain' => '#ConstraintViolationList',
502+
// 'range' => '#ConstraintViolation',
503+
// ],
504+
// $hydraPrefix.'title' => 'violations',
505+
// $hydraPrefix.'description' => 'The violations',
506+
// $hydraPrefix.'readable' => true,
507+
// $hydraPrefix.'writeable' => false,
508+
// ],
509+
// ],
510+
// ];
509511

510512
return $classes;
511513
}
@@ -526,7 +528,7 @@ private function getProperty(ApiProperty $propertyMetadata, string $propertyName
526528
$propertyData = ($propertyMetadata->getJsonldContext()[$hydraPrefix.'property'] ?? []) + [
527529
'@id' => $iri,
528530
'@type' => false === $propertyMetadata->isReadableLink() ? $hydraPrefix.'Link' : 'rdf:Property',
529-
'rdfs:label' => $propertyName,
531+
'title' => $propertyName,
530532
'domain' => $prefixedShortName,
531533
];
532534

src/Metadata/ApiProperty.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
*
2727
* @author Kévin Dunglas <[email protected]>
2828
*/
29-
#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD | \Attribute::TARGET_PARAMETER | \Attribute::TARGET_CLASS_CONSTANT | \Attribute::TARGET_CLASS)]
29+
#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD | \Attribute::TARGET_PARAMETER | \Attribute::TARGET_CLASS_CONSTANT | \Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE)]
3030
final class ApiProperty
3131
{
3232
private ?array $types;
@@ -216,6 +216,10 @@ public function __construct(
216216
private ?string $property = null,
217217
private ?string $policy = null,
218218
array|Context|Groups|Ignore|SerializedName|SerializedPath|MaxDepth|null $serialize = null,
219+
/**
220+
* Whether to document this property as a hydra:supportedProperty
221+
*/
222+
private ?bool $hydra = null,
219223
private array $extraProperties = [],
220224
) {
221225
$this->types = \is_string($types) ? (array) $types : $types;
@@ -517,7 +521,7 @@ public function withSchema(array $schema = []): self
517521
return $self;
518522
}
519523

520-
public function withInitializable(?bool $initializable): self
524+
public function withInitializable(bool $initializable): self
521525
{
522526
$self = clone $this;
523527
$self->initializable = $initializable;
@@ -626,4 +630,17 @@ public function withSerialize(array|Context|Groups|Ignore|SerializedName|Seriali
626630

627631
return $self;
628632
}
633+
634+
public function getHydra(): ?bool
635+
{
636+
return $this->hydra;
637+
}
638+
639+
public function withHydra(bool $hydra): static
640+
{
641+
$self = clone $this;
642+
$self->hydra = $hydra;
643+
644+
return $self;
645+
}
629646
}

src/Metadata/Error.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ public function __construct(
9494
$provider = null,
9595
$processor = null,
9696
?OptionsInterface $stateOptions = null,
97+
?bool $hideHydraOperation = null,
9798
array $extraProperties = [],
9899
) {
99100
parent::__construct(
@@ -169,6 +170,7 @@ class: $class,
169170
provider: $provider,
170171
processor: $processor,
171172
stateOptions: $stateOptions,
173+
hideHydraOperation: $hideHydraOperation,
172174
extraProperties: $extraProperties,
173175
);
174176
}

0 commit comments

Comments
 (0)