diff --git a/lib/internal/Magento/Framework/Reflection/DataObjectProcessor.php b/lib/internal/Magento/Framework/Reflection/DataObjectProcessor.php index 94023d129d1db..f6c0d540fa351 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,22 @@ private function changeOutputArray($dataObject, array $outputData): array return $outputData; } + + /** + * Mapping argument processor to modify output api key + * + * @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 = [