Question

I am trying to convert a \Magento\Sales\Api\Data\OrderInterface object to an array using \Magento\Framework\Api\ExtensibleDataObjectConverter.
I think it is not relevant here but I am using Magento CE 2.1.4

Here is the code I use :

<?php

use Magento\Framework\Api\ExtensibleDataObjectConverter;      

public function __construct(
    [...]
    ExtensibleDataObjectConverter $dataObjectConverter
) {
    [...]
    $this->dataObjectConverter = $dataObjectConverter;
}

public function get()
{
    /** @var \Magento\Sales\Api\Data\OrderInterface $order */
    $order = $this->orderRepository->get($orderId);

    /** @var array $data */
    $data = $this->dataObjectConverter->toFlatArray($order);
}

But here is the error I got :

Getter return type must be specified using @return annotation. See Magento\Sales\Model\Order::getPayment()

If we take a look at \Magento\Framework\Reflection\TypeProcessor::getGetterReturnType() there is this line :

$returnAnnotations = $methodDocBlock->getTags('return');

Which is using \Zend\Code\Reflection\DocBlockReflection::getTags() to list all DockBlock tags and check if there is a return

Now if we take a look at \Magento\Sales\Model\Order::getPayment() here is the actual function declaration :

/**
 * {@inheritdoc}
 */
public function getPayment()
{
    [...]
}

To fix this issue I have to manually add the @return tag to \Magento\Sales\Model\Order::getPayment()

According to https://www.phpdoc.org/docs/latest/guides/inheritance.html#methods the @return tag is subject to the inheritance so I should not have to put the @return tag in addition to the @inheritdoc one.


My questions are simple :

  • Is that a bug?
  • Am I using the wrong method to convert object to array in Magento 2?
Was it helpful?

Solution

I think you have to use the Interface Type for converting, since as API, it should have all the return types specified

try to call the function like this

$data = $this->dataObjectConverter->toFlatArray($order, [], \Magento\Sales\Api\Data\OrderInterface::class);

If you do not set a type, the class of the actual object will be used: https://github.com/magento/magento2/blob/develop/lib/internal/Magento/Framework/Api/ExtensibleDataObjectConverter.php#L44

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top