Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(metadata): hide an hydra endpoint (draft) #6871

Merged
merged 1 commit into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/Hydra/Serializer/DocumentationNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ public function normalize(mixed $object, ?string $format = null, array $context
continue;
}

if (true === $resourceMetadata->getHideHydraOperation()) {
continue;
}

$shortName = $resourceMetadata->getShortName();

$prefixedShortName = $resourceMetadata->getTypes()[0] ?? "#$shortName";
Expand Down Expand Up @@ -243,6 +247,10 @@ private function getHydraOperations(bool $collection, ?ResourceMetadataCollectio
$hydraOperations = [];
foreach ($resourceMetadataCollection as $resourceMetadata) {
foreach ($resourceMetadata->getOperations() as $operation) {
if (true === $operation->getHideHydraOperation()) {
continue;
}

if (('POST' === $operation->getMethod() || $operation instanceof CollectionOperationInterface) !== $collection) {
continue;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Hydra/Serializer/EntrypointNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function normalize(mixed $object, ?string $format = null, array $context

foreach ($resource->getOperations() as $operation) {
$key = lcfirst($resource->getShortName());
if (!$operation instanceof CollectionOperationInterface || isset($entrypoint[$key])) {
if (true === $operation->getHideHydraOperation() || !$operation instanceof CollectionOperationInterface || isset($entrypoint[$key])) {
continue;
}

Expand Down
2 changes: 2 additions & 0 deletions src/Metadata/ApiResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -964,6 +964,7 @@ public function __construct(
array|string|null $middleware = null,
array|Parameters|null $parameters = null,
protected ?bool $strictQueryParameterValidation = null,
protected ?bool $hideHydraOperation = null,
protected array $extraProperties = [],
) {
parent::__construct(
Expand Down Expand Up @@ -1009,6 +1010,7 @@ class: $class,
policy: $policy,
middleware: $middleware,
strictQueryParameterValidation: $strictQueryParameterValidation,
hideHydraOperation: $hideHydraOperation,
extraProperties: $extraProperties
);

Expand Down
2 changes: 2 additions & 0 deletions src/Metadata/Delete.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ public function __construct(
?string $policy = null,
array|string|null $middleware = null,
?bool $strictQueryParameterValidation = null,
protected ?bool $hideHydraOperation = null,
array $extraProperties = [],
) {
parent::__construct(
Expand Down Expand Up @@ -180,6 +181,7 @@ class: $class,
collectDenormalizationErrors: $collectDenormalizationErrors,
parameters: $parameters,
strictQueryParameterValidation: $strictQueryParameterValidation,
hideHydraOperation: $hideHydraOperation,
stateOptions: $stateOptions,
);
}
Expand Down
1 change: 1 addition & 0 deletions src/Metadata/Extractor/XmlResourceExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ private function buildExtendedBase(\SimpleXMLElement $resource): array
'exceptionToStatus' => $this->buildExceptionToStatus($resource),
'queryParameterValidationEnabled' => $this->phpize($resource, 'queryParameterValidationEnabled', 'bool'),
'strictQueryParameterValidation' => $this->phpize($resource, 'strictQueryParameterValidation', 'bool'),
'hideHydraOperation' => $this->phpize($resource, 'hideHydraOperation', 'bool'),
'stateOptions' => $this->buildStateOptions($resource),
'links' => $this->buildLinks($resource),
'headers' => $this->buildHeaders($resource),
Expand Down
1 change: 1 addition & 0 deletions src/Metadata/Extractor/YamlResourceExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@ private function buildOperations(array $resource, array $root): ?array
'serialize' => $this->phpize($operation, 'serialize', 'bool'),
'queryParameterValidate' => $this->phpize($operation, 'queryParameterValidate', 'bool'),
'strictQueryParameterValidation' => $this->phpize($operation, 'strictQueryParameterValidation', 'bool'),
'hideHydraOperation' => $this->phpize($resource, 'hideHydraOperation', 'bool'),
'priority' => $this->phpize($operation, 'priority', 'integer'),
'name' => $this->phpize($operation, 'name', 'string'),
'class' => (string) $class,
Expand Down
1 change: 1 addition & 0 deletions src/Metadata/Extractor/schema/resources.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,7 @@
<xsd:attributeGroup name="extendedBase">
<xsd:attributeGroup ref="base"/>
<xsd:attribute type="xsd:boolean" name="strictQueryParameterValidation"/>
<xsd:attribute type="xsd:boolean" name="hideHydraOperation"/>
<xsd:attribute type="xsd:boolean" name="queryParameterValidationEnabled"/>
<xsd:attribute type="xsd:string" name="routePrefix"/>
<xsd:attribute type="xsd:boolean" name="stateless"/>
Expand Down
2 changes: 2 additions & 0 deletions src/Metadata/Get.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ public function __construct(
?string $policy = null,
array|string|null $middleware = null,
?bool $strictQueryParameterValidation = null,
protected ?bool $hideHydraOperation = null,
array $extraProperties = [],
) {
parent::__construct(
Expand Down Expand Up @@ -179,6 +180,7 @@ class: $class,
policy: $policy,
middleware: $middleware,
strictQueryParameterValidation: $strictQueryParameterValidation,
hideHydraOperation: $hideHydraOperation,
extraProperties: $extraProperties,
);
}
Expand Down
2 changes: 2 additions & 0 deletions src/Metadata/GetCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ public function __construct(
?string $policy = null,
array|string|null $middleware = null,
?bool $strictQueryParameterValidation = null,
protected ?bool $hideHydraOperation = null,
array $extraProperties = [],
private ?string $itemUriTemplate = null,
) {
Expand Down Expand Up @@ -180,6 +181,7 @@ class: $class,
policy: $policy,
middleware: $middleware,
strictQueryParameterValidation: $strictQueryParameterValidation,
hideHydraOperation: $hideHydraOperation,
stateOptions: $stateOptions,
);
}
Expand Down
2 changes: 2 additions & 0 deletions src/Metadata/HttpOperation.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ public function __construct(
protected ?array $links = null,
protected ?array $errors = null,
protected ?bool $strictQueryParameterValidation = null,
protected ?bool $hideHydraOperation = null,

?string $shortName = null,
?string $class = null,
Expand Down Expand Up @@ -259,6 +260,7 @@ class: $class,
middleware: $middleware,
queryParameterValidationEnabled: $queryParameterValidationEnabled,
strictQueryParameterValidation: $strictQueryParameterValidation,
hideHydraOperation: $hideHydraOperation,
extraProperties: $extraProperties
);
}
Expand Down
14 changes: 14 additions & 0 deletions src/Metadata/Metadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ public function __construct(
protected array|string|null $middleware = null,
protected ?bool $queryParameterValidationEnabled = null,
protected ?bool $strictQueryParameterValidation = null,
protected ?bool $hideHydraOperation = null,
protected array $extraProperties = [],
) {
if (\is_array($parameters) && $parameters) {
Expand Down Expand Up @@ -680,4 +681,17 @@ public function withStrictQueryParameterValidation(bool $strictQueryParameterVal

return $self;
}

public function getHideHydraOperation(): ?bool
{
return $this->hideHydraOperation;
}

public function withHideHydraOperation(bool $hideHydraOperation): static
{
$self = clone $this;
$self->hideHydraOperation = $hideHydraOperation;

return $self;
}
}
2 changes: 2 additions & 0 deletions src/Metadata/Operation.php
Original file line number Diff line number Diff line change
Expand Up @@ -812,6 +812,7 @@ public function __construct(
array|string|null $middleware = null,
?bool $queryParameterValidationEnabled = null,
protected ?bool $strictQueryParameterValidation = null,
protected ?bool $hideHydraOperation = null,
protected array $extraProperties = [],
) {
parent::__construct(
Expand Down Expand Up @@ -858,6 +859,7 @@ class: $class,
middleware: $middleware,
queryParameterValidationEnabled: $queryParameterValidationEnabled,
strictQueryParameterValidation: $strictQueryParameterValidation,
hideHydraOperation: $hideHydraOperation,
extraProperties: $extraProperties,
);
}
Expand Down
2 changes: 2 additions & 0 deletions src/Metadata/Patch.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ public function __construct(
?string $policy = null,
array|string|null $middleware = null,
?bool $strictQueryParameterValidation = null,
?bool $hideHydraOperation = null,
array $extraProperties = [],
) {
parent::__construct(
Expand Down Expand Up @@ -180,6 +181,7 @@ class: $class,
policy: $policy,
middleware: $middleware,
strictQueryParameterValidation: $strictQueryParameterValidation,
hideHydraOperation: $hideHydraOperation,
extraProperties: $extraProperties
);
}
Expand Down
2 changes: 2 additions & 0 deletions src/Metadata/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ public function __construct(
array $extraProperties = [],
private ?string $itemUriTemplate = null,
?bool $strictQueryParameterValidation = null,
?bool $hideHydraOperation = null,
) {
parent::__construct(
method: 'POST',
Expand Down Expand Up @@ -181,6 +182,7 @@ class: $class,
policy: $policy,
middleware: $middleware,
strictQueryParameterValidation: $strictQueryParameterValidation,
hideHydraOperation: $hideHydraOperation,
extraProperties: $extraProperties
);
}
Expand Down
2 changes: 2 additions & 0 deletions src/Metadata/Put.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ public function __construct(
array|string|null $middleware = null,
array $extraProperties = [],
?bool $strictQueryParameterValidation = null,
?bool $hideHydraOperation = null,
private ?bool $allowCreate = null,
) {
parent::__construct(
Expand Down Expand Up @@ -181,6 +182,7 @@ class: $class,
policy: $policy,
middleware: $middleware,
strictQueryParameterValidation: $strictQueryParameterValidation,
hideHydraOperation: $hideHydraOperation,
extraProperties: $extraProperties
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ final class XmlResourceAdapter implements ResourceAdapterInterface
'securityPostValidationMessage',
'queryParameterValidationEnabled',
'strictQueryParameterValidation',
'hideHydraOperation',
'stateOptions',
'collectDenormalizationErrors',
'links',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ final class ResourceMetadataCompatibilityTest extends TestCase
'securityPostValidationMessage' => 'Sorry, you must the owner of this resource to access it.',
'queryParameterValidationEnabled' => true,
'strictQueryParameterValidation' => false,
'hideHydraOperation' => false,
'types' => ['someirischema', 'anotheririschema'],
'formats' => [
'json' => null,
Expand Down Expand Up @@ -401,6 +402,7 @@ final class ResourceMetadataCompatibilityTest extends TestCase
],
'queryParameterValidationEnabled' => false,
'strictQueryParameterValidation' => false,
'hideHydraOperation' => false,
'read' => true,
'deserialize' => false,
'validate' => false,
Expand Down Expand Up @@ -489,6 +491,7 @@ final class ResourceMetadataCompatibilityTest extends TestCase
'controller',
'queryParameterValidationEnabled',
'strictQueryParameterValidation',
'hideHydraOperation',
'exceptionToStatus',
'types',
'formats',
Expand Down
4 changes: 4 additions & 0 deletions src/Metadata/Tests/Extractor/XmlExtractorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public function testValidXML(): void
'securityPostValidationMessage' => null,
'queryParameterValidationEnabled' => null,
'strictQueryParameterValidation' => null,
'hideHydraOperation' => null,
'input' => null,
'output' => null,
'types' => null,
Expand Down Expand Up @@ -138,6 +139,7 @@ public function testValidXML(): void
'securityPostValidationMessage' => null,
'queryParameterValidationEnabled' => null,
'strictQueryParameterValidation' => null,
'hideHydraOperation' => null,
'input' => null,
'output' => null,
'types' => ['someirischema', 'anotheririschema'],
Expand Down Expand Up @@ -267,6 +269,7 @@ public function testValidXML(): void
'serialize' => null,
'queryParameterValidate' => null,
'strictQueryParameterValidation' => null,
'hideHydraOperation' => null,
'collection' => null,
'method' => null,
'priority' => null,
Expand Down Expand Up @@ -371,6 +374,7 @@ public function testValidXML(): void
'serialize' => null,
'queryParameterValidate' => null,
'strictQueryParameterValidation' => null,
'hideHydraOperation' => null,
'collection' => null,
'method' => null,
'priority' => null,
Expand Down
2 changes: 2 additions & 0 deletions src/Metadata/Tests/Extractor/YamlExtractorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ public function testValidYaml(): void
'securityPostValidationMessage' => null,
'queryParameterValidationEnabled' => null,
'strictQueryParameterValidation' => null,
'hideHydraOperation' => null,
'input' => null,
'output' => null,
'types' => ['someirischema'],
Expand Down Expand Up @@ -355,6 +356,7 @@ public function testValidYaml(): void
'securityPostValidationMessage' => null,
'queryParameterValidationEnabled' => null,
'strictQueryParameterValidation' => null,
'hideHydraOperation' => null,
'input' => null,
'output' => null,
'types' => ['anotheririschema'],
Expand Down
33 changes: 33 additions & 0 deletions tests/Fixtures/TestBundle/ApiResource/HideHydraClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Tests\Fixtures\TestBundle\ApiResource;

use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GetCollection;

#[ApiResource(
hideHydraOperation: true,
normalizationContext: ['hydra_prefix' => false],
operations: [
new Get(),
new GetCollection(),
]
)]
final class HideHydraClass
{
public function __construct(public string $id, public string $title)
{
}
}
32 changes: 32 additions & 0 deletions tests/Fixtures/TestBundle/ApiResource/HideHydraOperation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Tests\Fixtures\TestBundle\ApiResource;

use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GetCollection;

#[ApiResource(
normalizationContext: ['hydra_prefix' => false],
operations: [
new Get(hideHydraOperation: true),
new GetCollection(hideHydraOperation: true),
]
)]
final class HideHydraOperation
{
public function __construct(public string $id, public string $title)
{
}
}
Loading
Loading