From c95f6bc7b15b632030697f37ffa7f55f568b73ed Mon Sep 17 00:00:00 2001 From: tuyennn Date: Fri, 4 Oct 2024 15:30:39 +0700 Subject: [PATCH 1/3] Fix #24681 Support camelCase webapi --- .../Reflection/DataObjectProcessor.php | 28 +++++++++++++++++-- .../Test/Unit/DataObjectProcessorTest.php | 9 +++++- 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/lib/internal/Magento/Framework/Reflection/DataObjectProcessor.php b/lib/internal/Magento/Framework/Reflection/DataObjectProcessor.php index 94023d129d1db..bfdb46edf02dd 100644 --- a/lib/internal/Magento/Framework/Reflection/DataObjectProcessor.php +++ b/lib/internal/Magento/Framework/Reflection/DataObjectProcessor.php @@ -51,6 +51,11 @@ class DataObjectProcessor */ private $excludedMethodsClassMap; + /** + * @var array[] + */ + private $objectKeyMap; + /** * @param MethodsMap $methodsMapProcessor * @param TypeCaster $typeCaster @@ -59,6 +64,7 @@ class DataObjectProcessor * @param ExtensionAttributesProcessor $extensionAttributesProcessor * @param array $processors * @param array $excludedMethodsClassMap + * @param array $objectKeyMap */ public function __construct( MethodsMap $methodsMapProcessor, @@ -67,7 +73,8 @@ public function __construct( CustomAttributesProcessor $customAttributesProcessor, ExtensionAttributesProcessor $extensionAttributesProcessor, array $processors = [], - array $excludedMethodsClassMap = [] + array $excludedMethodsClassMap = [], + array $objectKeyMap = [] ) { $this->methodsMapProcessor = $methodsMapProcessor; $this->typeCaster = $typeCaster; @@ -76,6 +83,7 @@ public function __construct( $this->customAttributesProcessor = $customAttributesProcessor; $this->processors = $processors; $this->excludedMethodsClassMap = $excludedMethodsClassMap; + $this->objectKeyMap = $objectKeyMap; } /** @@ -143,7 +151,7 @@ public function buildOutputDataArray($dataObject, $dataObjectType) } } - $outputData[$key] = $value; + $outputData[$this->getKeyByObjectType($key, $dataObjectType)] = $value; } $outputData = $this->changeOutputArray($dataObject, $outputData); @@ -168,4 +176,20 @@ private function changeOutputArray($dataObject, array $outputData): array return $outputData; } + + /** + * @param string $key + * @param string $dataObjectType + * @return string + */ + private function getKeyByObjectType(string $key, string $dataObjectType): string { + $dataObjectType = ltrim($dataObjectType, '\\'); + if ( + array_key_exists($dataObjectType, $this->objectKeyMap) && + array_key_exists($key, $this->objectKeyMap[$dataObjectType]) + ) { + $key = $this->objectKeyMap[$dataObjectType][$key]; + } + return $key; + } } diff --git a/lib/internal/Magento/Framework/Reflection/Test/Unit/DataObjectProcessorTest.php b/lib/internal/Magento/Framework/Reflection/Test/Unit/DataObjectProcessorTest.php index 1510c6aee4cdb..a751d1e61c53b 100644 --- a/lib/internal/Magento/Framework/Reflection/Test/Unit/DataObjectProcessorTest.php +++ b/lib/internal/Magento/Framework/Reflection/Test/Unit/DataObjectProcessorTest.php @@ -76,6 +76,12 @@ public function testBuildOutputDataArray( ) { $objectManager = new ObjectManager($this); + $objectKeyMap = [ + TestDataInterface::class => [ + 'required_billing' => 'requiredBilling' + ], + ]; + $this->dataObjectProcessor = $objectManager->getObject( DataObjectProcessor::class, [ @@ -84,6 +90,7 @@ public function testBuildOutputDataArray( 'fieldNamer' => $objectManager->getObject(FieldNamer::class), 'extensionAttributesProcessor' => $this->extensionAttributesProcessorMock, 'excludedMethodsClassMap' => $excludedMethodsClassMap, + 'objectKeyMap' => $objectKeyMap, ] ); @@ -121,7 +128,7 @@ public static function buildOutputDataArrayDataProvider() 'id' => '1', 'address' => 'someAddress', 'default_shipping' => 'true', - 'required_billing' => 'false', + 'requiredBilling' => 'false' ]; $extensionAttributes = [ From 5bf2782513e6177d7accaddabe4cd1f3588e78d1 Mon Sep 17 00:00:00 2001 From: tuyennn Date: Fri, 4 Oct 2024 16:43:56 +0700 Subject: [PATCH 2/3] [fix] Static test Coding standards --- .../Magento/Framework/Reflection/DataObjectProcessor.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/internal/Magento/Framework/Reflection/DataObjectProcessor.php b/lib/internal/Magento/Framework/Reflection/DataObjectProcessor.php index bfdb46edf02dd..e6da13649c712 100644 --- a/lib/internal/Magento/Framework/Reflection/DataObjectProcessor.php +++ b/lib/internal/Magento/Framework/Reflection/DataObjectProcessor.php @@ -178,11 +178,14 @@ private function changeOutputArray($dataObject, array $outputData): array } /** + * Mapping argument processor to modify output api key + * * @param string $key * @param string $dataObjectType * @return string */ - private function getKeyByObjectType(string $key, string $dataObjectType): string { + private function getKeyByObjectType(string $key, string $dataObjectType): string + { $dataObjectType = ltrim($dataObjectType, '\\'); if ( array_key_exists($dataObjectType, $this->objectKeyMap) && From 6e8f432d6b0a02c8987b03ce68cde44fff279e2d Mon Sep 17 00:00:00 2001 From: tuyennn Date: Fri, 4 Oct 2024 17:35:17 +0700 Subject: [PATCH 3/3] [fix] Static test Coding standards --- .../Magento/Framework/Reflection/DataObjectProcessor.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/internal/Magento/Framework/Reflection/DataObjectProcessor.php b/lib/internal/Magento/Framework/Reflection/DataObjectProcessor.php index e6da13649c712..f6c0d540fa351 100644 --- a/lib/internal/Magento/Framework/Reflection/DataObjectProcessor.php +++ b/lib/internal/Magento/Framework/Reflection/DataObjectProcessor.php @@ -187,8 +187,7 @@ private function changeOutputArray($dataObject, array $outputData): array private function getKeyByObjectType(string $key, string $dataObjectType): string { $dataObjectType = ltrim($dataObjectType, '\\'); - if ( - array_key_exists($dataObjectType, $this->objectKeyMap) && + if (array_key_exists($dataObjectType, $this->objectKeyMap) && array_key_exists($key, $this->objectKeyMap[$dataObjectType]) ) { $key = $this->objectKeyMap[$dataObjectType][$key];