Skip to content

fix(metadata): defaults should not override values #4702

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

Merged
merged 5 commits into from
Apr 5, 2022
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
39 changes: 39 additions & 0 deletions features/hydra/collection.feature
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,45 @@ Feature: Collections support
}
"""

@!mongodb
@php8
Scenario: Change the number of element by page client side with v3, attributes and PHP8. Defaults (max 40) should not override resource attribute (max 30)
Given there are 80 pagination entities
When I send a "GET" request to "/pagination_entities?page=2&itemsPerPage=40"
Then the response status code should be 200
And the response should be in JSON
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
And the JSON should be valid according to this schema:
"""
{
"type": "object",
"properties": {
"@context": {"pattern": "^/contexts/PaginationEntity"},
"@id": {"pattern": "^/pagination_entities"},
"@type": {"pattern": "^hydra:Collection$"},
"hydra:totalItems": {"type":"number", "maximum": 80},
"hydra:member": {
"type": "array",
"minItems": 30,
"maxItems": 30
},
"hydra:view": {
"type": "object",
"properties": {
"@id": {"pattern": "^/pagination_entities\\?itemsPerPage=40&page=2$"},
"@type": {"pattern": "^hydra:PartialCollectionView$"},
"hydra:first": {"pattern": "^/pagination_entities\\?itemsPerPage=40&page=1$"},
"hydra:last": {"pattern": "^/pagination_entities\\?itemsPerPage=40&page=3$"},
"hydra:previous": {"pattern": "^/pagination_entities\\?itemsPerPage=40&page=1$"},
"hydra:next": {"pattern": "^/pagination_entities\\?itemsPerPage=40&page=3$"}
}
},
"hydra:search": {}
},
"additionalProperties": false
}
"""

Scenario: Test presence of next
When I send a "GET" request to "/dummies?page=3"
Then the response status code should be 200
Expand Down
28 changes: 14 additions & 14 deletions src/Metadata/GraphQl/Mutation.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ class Mutation extends Operation
* {@inheritdoc}
*/
public function __construct(
bool $delete = false,
?bool $delete = null,
?string $resolver = null,
bool $collection = false,
?bool $collection = null,
?array $args = null,
?string $shortName = null,
?string $class = null,
Expand All @@ -36,34 +36,34 @@ public function __construct(
?bool $paginationClientPartial = null,
?bool $paginationFetchJoinCollection = null,
?bool $paginationUseOutputWalkers = null,
array $order = [],
?array $order = null,
?string $description = null,
array $normalizationContext = [],
array $denormalizationContext = [],
?array $normalizationContext = null,
?array $denormalizationContext = null,
?string $security = null,
?string $securityMessage = null,
?string $securityPostDenormalize = null,
?string $securityPostDenormalizeMessage = null,
?string $securityPostValidation = null,
?string $securityPostValidationMessage = null,
?string $deprecationReason = null,
array $filters = [],
array $validationContext = [],
?array $filters = null,
?array $validationContext = null,
$input = null,
$output = null,
$mercure = null,
$messenger = null,
?bool $elasticsearch = null,
?int $urlGenerationStrategy = null,
bool $read = true,
bool $deserialize = true,
bool $validate = true,
bool $write = true,
bool $serialize = true,
?bool $read = null,
?bool $deserialize = null,
?bool $validate = null,
?bool $write = null,
?bool $serialize = null,
?bool $fetchPartial = null,
?bool $forceEager = null,
int $priority = 0,
string $name = '',
?int $priority = null,
?string $name = null,
array $extraProperties = []
) {
parent::__construct(...\func_get_args());
Expand Down
4 changes: 2 additions & 2 deletions src/Metadata/GraphQl/Operation.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ class Operation
public function __construct(
?bool $delete = null,
?string $resolver = null,
bool $collection = false,
?bool $collection = null,
?array $args = null,
?string $shortName = null,
?string $class = null,
Expand Down Expand Up @@ -228,7 +228,7 @@ public function withResolver(?string $resolver = null): self
return $self;
}

public function isCollection(): bool
public function isCollection(): ?bool
{
return $this->collection;
}
Expand Down
26 changes: 13 additions & 13 deletions src/Metadata/GraphQl/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Query extends Operation
*/
public function __construct(
?string $resolver = null,
bool $collection = false,
?bool $collection = null,
?array $args = null,
?string $shortName = null,
?string $class = null,
Expand All @@ -35,34 +35,34 @@ public function __construct(
?bool $paginationClientPartial = null,
?bool $paginationFetchJoinCollection = null,
?bool $paginationUseOutputWalkers = null,
array $order = [],
?array $order = null,
?string $description = null,
array $normalizationContext = [],
array $denormalizationContext = [],
?array $normalizationContext = null,
?array $denormalizationContext = null,
?string $security = null,
?string $securityMessage = null,
?string $securityPostDenormalize = null,
?string $securityPostDenormalizeMessage = null,
?string $securityPostValidation = null,
?string $securityPostValidationMessage = null,
?string $deprecationReason = null,
array $filters = [],
array $validationContext = [],
?array $filters = null,
?array $validationContext = null,
$input = null,
$output = null,
$mercure = null,
$messenger = null,
?bool $elasticsearch = null,
?int $urlGenerationStrategy = null,
bool $read = true,
bool $deserialize = true,
bool $validate = true,
bool $write = true,
bool $serialize = true,
?bool $read = null,
?bool $deserialize = null,
?bool $validate = null,
?bool $write = null,
?bool $serialize = null,
?bool $fetchPartial = null,
?bool $forceEager = null,
int $priority = 0,
string $name = '',
?int $priority = null,
?string $name = null,
array $extraProperties = []
) {
parent::__construct(false, ...\func_get_args());
Expand Down
26 changes: 13 additions & 13 deletions src/Metadata/GraphQl/QueryCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ final class QueryCollection extends Query
*/
public function __construct(
?string $resolver = null,
bool $collection = false,
?bool $collection = null,
?array $args = null,
?string $shortName = null,
?string $class = null,
Expand All @@ -35,34 +35,34 @@ public function __construct(
?bool $paginationClientPartial = null,
?bool $paginationFetchJoinCollection = null,
?bool $paginationUseOutputWalkers = null,
array $order = [],
?array $order = null,
?string $description = null,
array $normalizationContext = [],
array $denormalizationContext = [],
?array $normalizationContext = null,
?array $denormalizationContext = null,
?string $security = null,
?string $securityMessage = null,
?string $securityPostDenormalize = null,
?string $securityPostDenormalizeMessage = null,
?string $securityPostValidation = null,
?string $securityPostValidationMessage = null,
?string $deprecationReason = null,
array $filters = [],
array $validationContext = [],
?array $filters = null,
?array $validationContext = null,
$input = null,
$output = null,
$mercure = null,
$messenger = null,
?bool $elasticsearch = null,
?int $urlGenerationStrategy = null,
bool $read = true,
bool $deserialize = true,
bool $validate = true,
bool $write = true,
bool $serialize = true,
?bool $read = null,
?bool $deserialize = null,
?bool $validate = null,
?bool $write = null,
?bool $serialize = null,
?bool $fetchPartial = null,
?bool $forceEager = null,
int $priority = 0,
string $name = '',
?int $priority = null,
?string $name = null,
array $extraProperties = []
) {
parent::__construct(...\func_get_args());
Expand Down
22 changes: 11 additions & 11 deletions src/Metadata/GraphQl/Subscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ final class Subscription extends Operation
*/
public function __construct(
?string $resolver = null,
bool $collection = false,
?bool $collection = null,
?array $args = null,
?string $shortName = null,
?string $class = null,
Expand All @@ -35,7 +35,7 @@ public function __construct(
?bool $paginationClientPartial = null,
?bool $paginationFetchJoinCollection = null,
?bool $paginationUseOutputWalkers = null,
array $order = [],
?array $order = null,
?string $description = null,
array $normalizationContext = [],
array $denormalizationContext = [],
Expand All @@ -46,23 +46,23 @@ public function __construct(
?string $securityPostValidation = null,
?string $securityPostValidationMessage = null,
?string $deprecationReason = null,
array $filters = [],
array $validationContext = [],
?array $filters = null,
?array $validationContext = null,
$input = null,
$output = null,
$mercure = null,
$messenger = null,
?bool $elasticsearch = null,
?int $urlGenerationStrategy = null,
bool $read = true,
bool $deserialize = true,
bool $validate = true,
bool $write = true,
bool $serialize = true,
?bool $read = null,
?bool $deserialize = null,
?bool $validate = null,
?bool $write = null,
?bool $serialize = null,
?bool $fetchPartial = null,
?bool $forceEager = null,
int $priority = 0,
string $name = '',
?int $priority = null,
?string $name = null,
array $extraProperties = []
) {
parent::__construct(false, ...\func_get_args());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,10 @@ private function buildResourceOperations(array $attributes, string $resourceClas
}

if (null === $graphQlOperations) {
$resources[$index] = $this->addDefaultGraphQlOperations($resources[$index]);
// Add default graphql operations on the first resource
if (0 === $index) {
$resources[$index] = $this->addDefaultGraphQlOperations($resources[$index]);
}
continue;
}

Expand All @@ -168,13 +171,7 @@ private function buildResourceOperations(array $attributes, string $resourceClas
*/
private function getOperationWithDefaults(ApiResource $resource, $operation): array
{
foreach ($this->defaults['attributes'] as $key => $value) {
[$key, $value] = $this->getKeyValue($key, $value);
if (method_exists($operation, 'get'.ucfirst($key)) && null !== $operation->{'get'.ucfirst($key)}()) {
$operation = $operation->{'with'.ucfirst($key)}($value);
}
}

// Inherit from resource defaults
foreach (get_class_methods($resource) as $methodName) {
if (0 !== strpos($methodName, 'get')) {
continue;
Expand All @@ -197,6 +194,9 @@ private function getOperationWithDefaults(ApiResource $resource, $operation): ar
$operation = $operation->{'with'.substr($methodName, 3)}($value);
}

// Add global defaults attributes to the operation
$operation = $this->addGlobalDefaults($operation);

if ($operation instanceof GraphQlOperation) {
if (!$operation->getName()) {
throw new RuntimeException('No GraphQL operation name.');
Expand All @@ -216,6 +216,7 @@ private function getOperationWithDefaults(ApiResource $resource, $operation): ar
if ($operation->getRouteName()) {
$operation = $operation->withName($operation->getRouteName());
}

// Check for name conflict
if ($operation->getName()) {
if (null !== $resource->getOperations() && !$resource->getOperations()->has($operation->getName())) {
Expand All @@ -232,20 +233,30 @@ private function getOperationWithDefaults(ApiResource $resource, $operation): ar
];
}

private function getResourceWithDefaults(string $resourceClass, string $shortName, ApiResource $resource): ApiResource
/**
* @param ApiResource|Operation|GraphQlOperation $operation
*/
private function addGlobalDefaults($operation)
{
$resource = $resource
->withShortName($resource->getShortName() ?? $shortName)
->withClass($resourceClass);

foreach ($this->defaults['attributes'] as $key => $value) {
[$key, $value] = $this->getKeyValue($key, $value);
if (method_exists($resource, 'get'.ucfirst($key)) && !$resource->{'get'.ucfirst($key)}()) {
$resource = $resource->{'with'.ucfirst($key)}($value);
$upperKey = ucfirst($key);
$getter = 'get'.$upperKey;
if (method_exists($operation, $getter) && null === $operation->{$getter}()) {
$operation = $operation->{'with'.$upperKey}($value);
}
}

return $resource;
return $operation;
}

private function getResourceWithDefaults(string $resourceClass, string $shortName, ApiResource $resource): ApiResource
{
$resource = $resource
->withShortName($resource->getShortName() ?? $shortName)
->withClass($resourceClass);

return $this->addGlobalDefaults($resource);
}

private function hasResourceAttributes(\ReflectionClass $reflectionClass): bool
Expand Down
14 changes: 14 additions & 0 deletions tests/Core/Behat/DoctrineContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\NetworkPathDummy;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\NetworkPathRelationDummy;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Order;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\PaginationEntity;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\PatchDummyRelation;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Payment;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Person;
Expand Down Expand Up @@ -278,6 +279,19 @@ public function thereAreDummyObjects(int $nb)
$this->manager->flush();
}

/**
* @Given there are :nb pagination entities
*/
public function thereArePaginationEntities(int $nb)
{
for ($i = 1; $i <= $nb; ++$i) {
$paginationEntity = new PaginationEntity();
$this->manager->persist($paginationEntity);
}

$this->manager->flush();
}

/**
* @Given there are :nb of these so many objects
*/
Expand Down
Loading