Skip to content

Commit 8de6c8f

Browse files
committed
Merge remote-tracking branch 'github-magento/EPAM-MAGETWO-91568' into EPAM-PR-5
2 parents 6c7666e + 13e6074 commit 8de6c8f

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

dev/tests/api-functional/testsuite/Magento/Catalog/Api/ProductRepositoryInterfaceTest.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -802,6 +802,45 @@ public function testGetList()
802802
$this->assertEquals($expectedResult, $response['items'][0]['custom_attributes'][$index]['value']);
803803
}
804804

805+
/**
806+
* @magentoApiDataFixture Magento/Catalog/_files/product_simple.php
807+
*/
808+
public function testGetListWithAdditionalParams()
809+
{
810+
$this->_markTestAsRestOnly();
811+
$searchCriteria = [
812+
'searchCriteria' => [
813+
'current_page' => 1,
814+
'page_size' => 2,
815+
],
816+
];
817+
$additionalParams = urlencode('items[id,custom_attributes[description]]');
818+
819+
$serviceInfo = [
820+
'rest' => [
821+
'resourcePath' => self::RESOURCE_PATH . '?' . http_build_query($searchCriteria) . '&fields=' .
822+
$additionalParams,
823+
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
824+
]
825+
];
826+
827+
$response = $this->_webApiCall($serviceInfo, $searchCriteria);
828+
829+
$this->assertArrayHasKey('items', $response);
830+
$this->assertTrue(count($response['items']) > 0);
831+
832+
$indexDescription = null;
833+
foreach ($response['items'][0]['custom_attributes'] as $key => $customAttribute) {
834+
if ($customAttribute['attribute_code'] == 'description') {
835+
$indexDescription = $key;
836+
}
837+
}
838+
839+
$this->assertNotNull($response['items'][0]['custom_attributes'][$indexDescription]['attribute_code']);
840+
$this->assertNotNull($response['items'][0]['custom_attributes'][$indexDescription]['value']);
841+
$this->assertTrue(count($response['items'][0]['custom_attributes']) == 1);
842+
}
843+
805844
/**
806845
* @magentoApiDataFixture Magento/Catalog/_files/products_with_websites_and_stores.php
807846
* @return void

lib/internal/Magento/Framework/Webapi/Rest/Response/FieldsFilter.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
namespace Magento\Framework\Webapi\Rest\Response;
88

9+
use Magento\Framework\Api\AbstractExtensibleObject;
10+
use Magento\Framework\Api\AttributeInterface;
911
use Magento\Framework\Webapi\Rest\Request as RestRequest;
1012

1113
/**
@@ -178,10 +180,47 @@ protected function recursiveArrayIntersectKey(array $array1, array $array2)
178180
//If the field in array2 (filter) is not present in array1 (response) it will be removed after intersect
179181
$arrayIntersect = array_intersect_key($array1, $array2);
180182
foreach ($arrayIntersect as $key => &$value) {
183+
if ($key == AbstractExtensibleObject::CUSTOM_ATTRIBUTES_KEY
184+
&& is_array($array2[AbstractExtensibleObject::CUSTOM_ATTRIBUTES_KEY])
185+
) {
186+
$value = $this->filterCustomAttributes(
187+
$value,
188+
$array2[AbstractExtensibleObject::CUSTOM_ATTRIBUTES_KEY]
189+
);
190+
continue;
191+
}
181192
if (is_array($value) && is_array($array2[$key])) {
182193
$value = $this->applyFilter($value, $array2[$key]);
183194
}
184195
}
185196
return $arrayIntersect;
186197
}
198+
199+
/**
200+
* Filter for custom attributes.
201+
*
202+
* @param array $item
203+
* @param array $filter
204+
* @return array
205+
*/
206+
private function filterCustomAttributes(array $item, array $filter) : array
207+
{
208+
$fieldResult = [];
209+
foreach ($item as $key => $field) {
210+
$filterKeys = array_keys($filter);
211+
if (in_array($field[AttributeInterface::ATTRIBUTE_CODE], $filterKeys)) {
212+
$fieldResult[$key][AttributeInterface::ATTRIBUTE_CODE] = $field[AttributeInterface::ATTRIBUTE_CODE];
213+
$fieldResult[$key][AttributeInterface::VALUE] = $field[AttributeInterface::VALUE];
214+
} else {
215+
if (isset($filter[AttributeInterface::ATTRIBUTE_CODE])) {
216+
$fieldResult[$key][AttributeInterface::ATTRIBUTE_CODE] = $field[AttributeInterface::ATTRIBUTE_CODE];
217+
}
218+
if (isset($filter[AttributeInterface::VALUE])) {
219+
$fieldResult[$key][AttributeInterface::VALUE] = $field[AttributeInterface::VALUE];
220+
}
221+
}
222+
}
223+
224+
return $fieldResult;
225+
}
187226
}

0 commit comments

Comments
 (0)