From f9f7f26a1cd7e8d162fb8b12776fd505c860f32f Mon Sep 17 00:00:00 2001 From: Jan Esser Date: Fri, 19 Apr 2024 09:43:48 +0200 Subject: [PATCH] fix merging of data failes or has existing data go missing in case certain xml keys are missing from extending config like properties, item opteration, collection operations or resources --- .../Merger/LegacyResourceMetadataMerger.php | 8 +++++++- .../ApiPlatform/Metadata/MergingXmlExtractor.php | 4 ++-- .../Merger/LegacyResourceMetadataMergerSpec.php | 15 +++++++++++++++ 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/src/Sylius/Bundle/ApiBundle/ApiPlatform/Metadata/Merger/LegacyResourceMetadataMerger.php b/src/Sylius/Bundle/ApiBundle/ApiPlatform/Metadata/Merger/LegacyResourceMetadataMerger.php index 66b0bc3f289..f1d76be7804 100644 --- a/src/Sylius/Bundle/ApiBundle/ApiPlatform/Metadata/Merger/LegacyResourceMetadataMerger.php +++ b/src/Sylius/Bundle/ApiBundle/ApiPlatform/Metadata/Merger/LegacyResourceMetadataMerger.php @@ -23,6 +23,10 @@ public function merge(array $oldMetadata, array $newMetadata): array foreach ($newMetadata as $key => $value) { if ('properties' === $key) { + if ($value === null) { + continue; + } + foreach ($value as $keyProperty => $property) { $oldMetadata[$key][$keyProperty] = $property; } @@ -47,7 +51,9 @@ public function merge(array $oldMetadata, array $newMetadata): array continue; } - $oldMetadata[$key] = $value; + if ($value !== null || !isset($oldMetadata[$key])) { + $oldMetadata[$key] = $value; + } } return $oldMetadata; diff --git a/src/Sylius/Bundle/ApiBundle/ApiPlatform/Metadata/MergingXmlExtractor.php b/src/Sylius/Bundle/ApiBundle/ApiPlatform/Metadata/MergingXmlExtractor.php index 229d9035c70..b145a318c4e 100644 --- a/src/Sylius/Bundle/ApiBundle/ApiPlatform/Metadata/MergingXmlExtractor.php +++ b/src/Sylius/Bundle/ApiBundle/ApiPlatform/Metadata/MergingXmlExtractor.php @@ -107,8 +107,8 @@ protected function extractPath(string $path): void $resourceMetadata, ); $this->properties[$resourceClass] = array_merge( - $this->properties[$resourceClass], - $resourceMetadata['properties'], + $this->properties[$resourceClass] ?? [], + $resourceMetadata['properties'] ?? [], ); continue; diff --git a/src/Sylius/Bundle/ApiBundle/spec/ApiPlatform/Metadata/Merger/LegacyResourceMetadataMergerSpec.php b/src/Sylius/Bundle/ApiBundle/spec/ApiPlatform/Metadata/Merger/LegacyResourceMetadataMergerSpec.php index 7992b860759..d41a2706bf9 100644 --- a/src/Sylius/Bundle/ApiBundle/spec/ApiPlatform/Metadata/Merger/LegacyResourceMetadataMergerSpec.php +++ b/src/Sylius/Bundle/ApiBundle/spec/ApiPlatform/Metadata/Merger/LegacyResourceMetadataMergerSpec.php @@ -170,6 +170,21 @@ function it_merges_subresource_operations(): void ]); } + function it_keeps_existing_subresource_operations_when_the_new_one_doesnt_have_them(): void + { + $this->merge([ + 'subresourceOperations' => [ + 'get' => ['foo' => 'bar'], + ], + ], [ + 'subresourceOperations' => null, + ])->shouldReturn([ + 'subresourceOperations' => [ + 'get' => ['foo' => 'bar'], + ], + ]); + } + function it_merges_complex_metadata(): void { $this->merge([