Skip to content

Commit

Permalink
#8035: Join extension attributes are not added to Order results (REST…
Browse files Browse the repository at this point in the history
… api)
  • Loading branch information
nmalevanec authored and al.kravchuk committed Mar 16, 2019
1 parent 92118f5 commit 961e05c
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 5 deletions.
15 changes: 13 additions & 2 deletions app/code/Magento/Sales/Model/OrderRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@

namespace Magento\Sales\Model;

use Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface;
use Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Exception\InputException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Sales\Api\Data\OrderExtensionFactory;
Expand All @@ -16,7 +18,6 @@
use Magento\Sales\Api\Data\ShippingAssignmentInterface;
use Magento\Sales\Model\Order\ShippingAssignmentBuilder;
use Magento\Sales\Model\ResourceModel\Metadata;
use Magento\Framework\App\ObjectManager;
use Magento\Tax\Api\OrderTaxManagementInterface;
use Magento\Payment\Api\Data\PaymentAdditionalInfoInterface;
use Magento\Payment\Api\Data\PaymentAdditionalInfoInterfaceFactory;
Expand Down Expand Up @@ -74,6 +75,11 @@ class OrderRepository implements \Magento\Sales\Api\OrderRepositoryInterface
*/
private $serializer;

/**
* @var JoinProcessorInterface
*/
private $extensionAttributesJoinProcessor;

/**
* Constructor
*
Expand All @@ -84,6 +90,7 @@ class OrderRepository implements \Magento\Sales\Api\OrderRepositoryInterface
* @param OrderTaxManagementInterface|null $orderTaxManagement
* @param PaymentAdditionalInfoInterfaceFactory|null $paymentAdditionalInfoFactory
* @param JsonSerializer|null $serializer
* @param JoinProcessorInterface $extensionAttributesJoinProcessor
*/
public function __construct(
Metadata $metadata,
Expand All @@ -92,7 +99,8 @@ public function __construct(
\Magento\Sales\Api\Data\OrderExtensionFactory $orderExtensionFactory = null,
OrderTaxManagementInterface $orderTaxManagement = null,
PaymentAdditionalInfoInterfaceFactory $paymentAdditionalInfoFactory = null,
JsonSerializer $serializer = null
JsonSerializer $serializer = null,
JoinProcessorInterface $extensionAttributesJoinProcessor = null
) {
$this->metadata = $metadata;
$this->searchResultFactory = $searchResultFactory;
Expand All @@ -106,6 +114,8 @@ public function __construct(
->get(PaymentAdditionalInfoInterfaceFactory::class);
$this->serializer = $serializer ?: ObjectManager::getInstance()
->get(JsonSerializer::class);
$this->extensionAttributesJoinProcessor = $extensionAttributesJoinProcessor
?: ObjectManager::getInstance()->get(JoinProcessorInterface::class);
}

/**
Expand Down Expand Up @@ -198,6 +208,7 @@ public function getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCr
{
/** @var \Magento\Sales\Api\Data\OrderSearchResultInterface $searchResult */
$searchResult = $this->searchResultFactory->create();
$this->extensionAttributesJoinProcessor->process($searchResult);
$this->collectionProcessor->process($searchCriteria, $searchResult);
$searchResult->setSearchCriteria($searchCriteria);
foreach ($searchResult->getItems() as $order) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,16 @@
</join>
</attribute>
</extension_attributes>
<extension_attributes for="Magento\Sales\Api\Data\OrderInterface">
<attribute code="orderApiTestAttribute" type="Magento\User\Api\Data\UserInterface">
<join reference_table="admin_user"
join_on_field="store_id"
reference_field="user_id"
>
<field>firstname</field>
<field>lastname</field>
<field>email</field>
</join>
</attribute>
</extension_attributes>
</config>
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@

namespace Magento\Webapi;

use Magento\Framework\Api\FilterBuilder;
use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Framework\Api\SortOrderBuilder;
use Magento\Framework\Api\SortOrder;
use Magento\Framework\Api\SearchCriteria;
use Magento\Framework\Api\FilterBuilder;
use Magento\Framework\Api\SortOrderBuilder;

class JoinDirectivesTest extends \Magento\TestFramework\TestCase\WebapiAbstract
{
Expand Down Expand Up @@ -124,6 +123,49 @@ public function testAutoGeneratedGetList()
$this->assertEquals($expectedExtensionAttributes['email'], $testAttribute['email']);
}

/**
* Test get list of orders with extension attributes.
*
* @magentoApiDataFixture Magento/Sales/_files/order.php
*/
public function testGetOrdertList()
{
$filter = $this->filterBuilder
->setField('increment_id')
->setValue('100000001')
->setConditionType('eq')
->create();
$this->searchBuilder->addFilters([$filter]);
$searchData = $this->searchBuilder->create()->__toArray();

$requestData = ['searchCriteria' => $searchData];

$restResourcePath = '/V1/orders/';
$soapService = 'salesOrderRepositoryV1';
$expectedExtensionAttributes = $this->getExpectedExtensionAttributes();

$serviceInfo = [
'rest' => [
'resourcePath' => $restResourcePath . '?' . http_build_query($requestData),
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
],
'soap' => [
'service' => $soapService,
'operation' => $soapService . 'GetList',
],
];
$searchResult = $this->_webApiCall($serviceInfo, $requestData);

$this->assertArrayHasKey('items', $searchResult);
$itemData = array_pop($searchResult['items']);
$this->assertArrayHasKey('extension_attributes', $itemData);
$this->assertArrayHasKey('order_api_test_attribute', $itemData['extension_attributes']);
$testAttribute = $itemData['extension_attributes']['order_api_test_attribute'];
$this->assertEquals($expectedExtensionAttributes['firstname'], $testAttribute['first_name']);
$this->assertEquals($expectedExtensionAttributes['lastname'], $testAttribute['last_name']);
$this->assertEquals($expectedExtensionAttributes['email'], $testAttribute['email']);
}

/**
* Retrieve the admin user's information.
*
Expand Down

0 comments on commit 961e05c

Please sign in to comment.