Skip to content

Commit

Permalink
Merge branch '2.6'
Browse files Browse the repository at this point in the history
* 2.6:
  docs: update changelog
  fix: defaults when using attributes (#3978)
  • Loading branch information
dunglas committed Jan 24, 2021
2 parents 27d38a1 + 4b293b0 commit 23141d8
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,9 @@
# Changelog

## 2.6.1

* Fix defaults when using attributes (#3978)

## 2.6.0

* Cache: adds a `max_header_length` configuration (#2865)
Expand Down
Expand Up @@ -88,7 +88,15 @@ private function handleNotFound(?ResourceMetadata $parentPropertyMetadata, strin

private function createMetadata(ApiResource $annotation, ResourceMetadata $parentResourceMetadata = null): ResourceMetadata
{
$attributes = (null === $annotation->attributes && [] === $this->defaults['attributes']) ? null : (array) $annotation->attributes + $this->defaults['attributes'];
$attributes = null;
if (null !== $annotation->attributes || [] !== $this->defaults['attributes']) {
$attributes = (array) $annotation->attributes;
foreach ($this->defaults['attributes'] as $key => $value) {
if (!isset($attributes[$key])) {
$attributes[$key] = $value;
}
}
}

if (!$parentResourceMetadata) {
return new ResourceMetadata(
Expand Down
Expand Up @@ -59,7 +59,15 @@ public function create(string $resourceClass): ResourceMetadata
$resource['itemOperations'] = $resource['itemOperations'] ?? $this->defaults['item_operations'] ?? null;
$resource['collectionOperations'] = $resource['collectionOperations'] ?? $this->defaults['collection_operations'] ?? null;
$resource['graphql'] = $resource['graphql'] ?? $this->defaults['graphql'] ?? null;
$resource['attributes'] = (null === $resource['attributes'] && [] === $this->defaults['attributes']) ? null : (array) $resource['attributes'] + $this->defaults['attributes'];

if (null !== $resource['attributes'] || [] !== $this->defaults['attributes']) {
$resource['attributes'] = (array) $resource['attributes'];
foreach ($this->defaults['attributes'] as $key => $value) {
if (!isset($resource['attributes'][$key])) {
$resource['attributes'][$key] = $value;
}
}
}

return $this->update($parentResourceMetadata ?: new ResourceMetadata(), $resource);
}
Expand Down
Expand Up @@ -80,6 +80,7 @@ public function testCreateWithDefaults()
'attributes' => [
'pagination_client_enabled' => true,
'pagination_maximum_items_per_page' => 10,
'stateless' => null,
],
]);
$reader = $this->prophesize(Reader::class);
Expand Down
Expand Up @@ -312,6 +312,7 @@ public function testItFallbacksToDefaultConfiguration()
'subresourceOperations' => null,
'itemOperations' => ['get', 'delete'],
'attributes' => [
'pagination_items_per_page' => null,
'pagination_maximum_items_per_page' => 10,
'stateless' => false,
],
Expand Down

0 comments on commit 23141d8

Please sign in to comment.