-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Issue#16425: Fixed "OR Condition in searchCriteria (REST API) doesn't work" #27588
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Hi @andrewbess. Thank you for your contribution
For more details, please, review the Magento Contributor Guide documentation. |
app/code/Magento/Eav/Model/Api/SearchCriteria/CollectionProcessor/FilterProcessor.php
Show resolved
Hide resolved
app/code/Magento/Eav/Model/Api/SearchCriteria/CollectionProcessor/FilterProcessor.php
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hello @andrewbess, thank you for your contribution!
Could you please check the minor CR recommendations and fix the failed tests?
Please note, the new changes should be covered by an appropriate autotest. The Web API Functional is preferable in this particular case.
Thank you!
$customFilter = $this->getCustomFilterForField($filter->getField()); | ||
if ($customFilter) { | ||
$isApplied = $customFilter->apply($filter, $collection); | ||
if ($filter->getConditionType() == 'eq') { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use strict comparison whenever it's possible.
if ($filter->getConditionType() == 'eq') { | |
if ($filter->getConditionType() === 'eq') { |
|
||
class FilterProcessorTest extends \PHPUnit\Framework\TestCase | ||
class FilterProcessorTest extends TestCase | ||
{ | ||
/** | ||
* Return model |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please move the private method getModel
to the bottom of the class. The private and protected methods should not be followed by public ones.
@@ -31,8 +35,11 @@ private function getModel(array $customFilters, array $fieldMapping) | |||
*/ | |||
public function testProcess() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
public function testProcess() | |
public function testProcess(): void |
@@ -144,25 +151,24 @@ public function testProcess() | |||
$model->process($searchCriteriaMock, $collectionMock); | |||
} | |||
|
|||
/** | |||
* @expectedException \InvalidArgumentException | |||
*/ | |||
public function testProcessWithException() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
public function testProcessWithException() | |
public function testProcessWithException(): void |
public function testProcessWithException() | ||
{ | ||
/** @var \stdClass|\PHPUnit_Framework_MockObject_MockObject $customFilterMock */ | ||
$customFilterMock = $this->createPartialMock(\stdClass::class, ['apply']); | ||
$this->expectException(InvalidArgumentException::class); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add the definition for the assertion of an exception right before the original methods call.
E.g.
$this->expectException(InvalidArgumentException::class);
$model->process($searchCriteriaMock, $collectionMock);
Otherwise, some real unexpected exception can be interpreted incorrectly.
@@ -5,14 +5,18 @@ | |||
*/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
*/ | |
*/ | |
declare(strict_types=1); | |
Hello @dmytro-ch |
@magento run all tests |
@andrewbess, thank you for the updates! Thank you! |
Hi @andrewbess, did you have to change a look? |
Hello @andrewbess, I'm closing this PR now due to inactivity. |
Hi @andrewbess, thank you for your contribution! |
Hi @andrewbess. Thank you for your contribution
❗ Automated tests can be triggered manually with an appropriate comment:
You can find more information about the builds here ℹ️ Please run only needed test builds instead of all when developing. Please run all test builds before sending your PR for review. For more details, please, review the Magento Contributor Guide documentation. |
Note: Automation tests are passed |
Need to check additional conditions |
@magento run all tests |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @andrewbess I have tested additional condition and there is issue if requests are with multiple filters and different condition_type we get empty response
Case 1:
- Create some categories
- Create products for each category:
-
- Product1 -> Category1
-
- Product2 -> Category2
-
- Product3 -> Category3
- Enable a REST API token
- Make a call to resource "/V1/products" with a search parameter:
?searchCriteria[filterGroups][0][filters][0][field]=category_id&searchCriteria[filterGroups][0][filters][0][value]=4&searchCriteria[filterGroups][0][filters][0][conditionType]=gt&searchCriteria[filterGroups][0][filters][1][field]=category_id&searchCriteria[filterGroups][0][filters][1][value]=3&searchCriteria[filterGroups][0][filters][1][conditionType]=eq
Result:
Empty response
Note: Expecting to see products from categories with ID's 5 and 6 and 3 (productsProduct1
,Product3
,Product4
)
{
"items": [],
"search_criteria": {
"filter_groups": [
{
"filters": [
{
"field": "category_id",
"value": "4",
"condition_type": "gt"
},
{
"field": "category_id",
"value": "3",
"condition_type": "eq"
}
]
}
]
},
"total_count": 0
}
Case 2:
- Create some categories
- Create products for each category:
-
- Product1 -> Category1
-
- Product2 -> Category2
-
- Product3 -> Category3
- Enable a REST API token
- Make a call to resource "/V1/products" with a search parameter:
?searchCriteria[filterGroups][0][filters][0][field]=category_id&searchCriteria[filterGroups][0][filters][0][value]=4&searchCriteria[filterGroups][0][filters][0][conditionType]=gt&searchCriteria[filterGroups][0][filters][1][field]=name&searchCriteria[filterGroups][0][filters][1][value]=Product1&searchCriteria[filterGroups][0][filters][1][conditionType]=eq
Result:
Empty response
Note: Expecting to see products from categories with ID's 5 and 6(productsProduct3
,Product4
) and productProduct1
{
"items": [],
"search_criteria": {
"filter_groups": [
{
"filters": [
{
"field": "category_id",
"value": "4",
"condition_type": "gt"
},
{
"field": "name",
"value": "Product1",
"condition_type": "eq"
}
]
}
]
},
"total_count": 0
}
Case 3 according comment:
Filter by category_id = ?
or store_id = ?
return no product in response
- Create some categories
- Create products for each category:
-
- Product1 -> Category1
-
- Product2 -> Category2
-
- Product3 -> Category3
- Create additional website
- Create product
ProductForStoreId
and assign it only to additional website - Enable a REST API token
- Make a call to resource "/V1/products" with a search parameter:
?searchCriteria[filterGroups][0][filters][0][field]=category_id&searchCriteria[filterGroups][0][filters][0][value]=4&searchCriteria[filterGroups][0][filters][0][conditionType]=gt&searchCriteria[filterGroups][0][filters][1][field]=name&searchCriteria[filterGroups][0][filters][1][value]=Product1&searchCriteria[filterGroups][0][filters][1][conditionType]=eq
Result:
Empty response
Note: Expecting to see product from category with ID 4(productProduct2
) and productProductForStoreId
{
"items": [],
"search_criteria": {
"filter_groups": [
{
"filters": [
{
"field": "category_id",
"value": "4",
"condition_type": "eq"
},
{
"field": "store_id",
"value": "3",
"condition_type": "eq"
}
]
}
]
},
"total_count": 0
}
Could you take a look?
Hello @engcom-Delta |
Hi @gabrieldagama could you provide some info on why this PR was moved to 'on hold' status? |
Hi @ihor-sviziev, I've tried to start a conversation internally to help to find a solution for this but was deffered due to other priorities at the moment. We can try to discuss this problem and find a solution with the community. Thanks! |
Description (*)
This PR fixes the problem with OR condition in searchCriteria.
Related Pull Requests
Fixed Issues (if relevant)
Manual testing scenarios (*)
?searchCriteria[filter_groups][0][filters][0][field]=category_id&searchCriteria[filter_groups][0][filters][0][value]=5&searchCriteria[filter_groups][0][filters][1][field]=category_id&searchCriteria[filter_groups][0][filters][1][value]=6
Questions or comments
Contribution checklist (*)