Skip to content

Commit 97fcb38

Browse files
committed
fix(metadata): merge defaults arrays
1 parent 980ee04 commit 97fcb38

File tree

3 files changed

+26
-7
lines changed

3 files changed

+26
-7
lines changed

src/Metadata/Resource/Factory/AttributesResourceMetadataCollectionFactory.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,17 @@ private function addGlobalDefaults($operation)
246246
if (!isset($extraProperties[$key])) {
247247
$extraProperties[$key] = $value;
248248
}
249-
} elseif (null === $operation->{$getter}()) {
249+
} else {
250+
$currentValue = $operation->{$getter}();
251+
252+
if (\is_array($currentValue) && $currentValue) {
253+
$operation = $operation->{'with'.$upperKey}(array_merge($value, $currentValue));
254+
}
255+
256+
if (null !== $currentValue) {
257+
continue;
258+
}
259+
250260
$operation = $operation->{'with'.$upperKey}($value);
251261
}
252262
}

tests/Fixtures/TestBundle/Entity/AttributeDefaultOperations.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
use ApiPlatform\Metadata\ApiProperty;
1717
use ApiPlatform\Metadata\ApiResource;
1818

19-
#[ApiResource(paginationItemsPerPage: 10, graphQlOperations: [])]
19+
#[ApiResource(paginationItemsPerPage: 10, graphQlOperations: [], cacheHeaders: ['shared_max_age' => 60])]
2020
final class AttributeDefaultOperations
2121
{
2222
#[ApiProperty(identifier: true)]

tests/Metadata/Resource/Factory/AttributesResourceMetadataCollectionFactoryTest.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,17 @@ class: AttributeResources::class,
138138

139139
public function testCreateWithDefaults(): void
140140
{
141-
$attributeResourceMetadataCollectionFactory = new AttributesResourceMetadataCollectionFactory(null, null, ['cache_headers' => ['max_age' => 60], 'non_existing_attribute' => 'foo']);
142-
143-
$operation = new HttpOperation(shortName: 'AttributeDefaultOperations', class: AttributeDefaultOperations::class, cacheHeaders: ['max_age' => 60], paginationItemsPerPage: 10, extraProperties: ['non_existing_attribute' => 'foo']);
141+
$attributeResourceMetadataCollectionFactory = new AttributesResourceMetadataCollectionFactory(null, null, [
142+
'cache_headers' => [
143+
'max_age' => 60,
144+
'shared_max_age' => 120,
145+
'public' => true,
146+
],
147+
'non_existing_attribute' => 'foo',
148+
]);
149+
150+
// Check the AttributeDefaultOperations it specifies a shared_max_age that should not be overridden
151+
$operation = new HttpOperation(shortName: 'AttributeDefaultOperations', class: AttributeDefaultOperations::class, cacheHeaders: ['max_age' => 60, 'shared_max_age' => 60, 'public' => true], paginationItemsPerPage: 10, extraProperties: ['non_existing_attribute' => 'foo']);
144152

145153
$this->assertEquals(new ResourceMetadataCollection(AttributeDefaultOperations::class, [
146154
new ApiResource(
@@ -155,7 +163,7 @@ class: AttributeDefaultOperations::class,
155163
'_api_AttributeDefaultOperations_patch' => (new Patch())->withOperation($operation),
156164
'_api_AttributeDefaultOperations_delete' => (new Delete())->withOperation($operation),
157165
],
158-
cacheHeaders: ['max_age' => 60],
166+
cacheHeaders: ['max_age' => 60, 'shared_max_age' => 60, 'public' => true],
159167
paginationItemsPerPage: 10,
160168
extraProperties: ['non_existing_attribute' => 'foo']
161169
),
@@ -170,7 +178,7 @@ public function testCreateShouldNotOverrideWithDefault(): void
170178
]
171179
);
172180

173-
$operation = new HttpOperation(shortName: 'AttributeDefaultOperations', class: AttributeDefaultOperations::class, paginationItemsPerPage: 10);
181+
$operation = new HttpOperation(shortName: 'AttributeDefaultOperations', class: AttributeDefaultOperations::class, paginationItemsPerPage: 10, cacheHeaders: ['shared_max_age' => 60]);
174182
$this->assertEquals(new ResourceMetadataCollection(AttributeDefaultOperations::class, [
175183
new ApiResource(
176184
shortName: 'AttributeDefaultOperations',
@@ -183,6 +191,7 @@ class: AttributeDefaultOperations::class,
183191
'_api_AttributeDefaultOperations_patch' => (new Patch())->withOperation($operation),
184192
'_api_AttributeDefaultOperations_delete' => (new Delete())->withOperation($operation),
185193
],
194+
cacheHeaders: ['shared_max_age' => 60],
186195
graphQlOperations: [],
187196
paginationItemsPerPage: 10
188197
),

0 commit comments

Comments
 (0)