Fix type error for item qty when refunding an invoice
Description (*)
When min is called with different types, there is no defined behavior which result is returned. When using the invoice/<invoice_id>/refund REST API endpoint, this might cause a type error:
This PR casts the invoice item quantity to float to prevent the type error.
Related Pull Requests
Fixed Issues (if relevant)
- Fixes magento/magento2#40302
Manual testing scenarios (*)
- Create an order with one item with qty 1
- Create invoice for order
- Make a call to
invoice/invoice_id/refundand refund the item with qty 1
Depending on the configuration of the PHP environment, you might need to reorder the arguments provided to min at app/code/Magento/Sales/Model/Order/CreditmemoFactory.php:139 to be able to reproduce:
Note that
$qty is type string, when it should be float.
Questions or comments
Contribution checklist (*)
- [ ] Pull request has a meaningful description of its purpose
- [ ] All commits are accompanied by meaningful commit messages
- [ ] All new or changed code is covered with unit/integration tests (if applicable)
- [ ] README.md files for modified modules are updated and included in the pull request if any README.md predefined sections require an update
- [ ] All automated tests passed successfully (all builds are green)
Hi @mfickers. Thank you for your contribution! Here are some useful tips on how you can test your changes using Magento test environment. :exclamation: Automated tests can be triggered manually with an appropriate comment:
-
@magento run all tests- run or re-run all required tests against the PR changes -
@magento run <test-build(s)>- run or re-run specific test build(s) For example:@magento run Unit Tests
<test-build(s)> is a comma-separated list of build names.
Allowed build names are:
-
Database Compare -
Functional Tests CE -
Functional Tests EE -
Functional Tests B2B -
Integration Tests -
Magento Health Index -
Sample Data Tests CE -
Sample Data Tests EE -
Sample Data Tests B2B -
Static Tests -
Unit Tests -
WebAPI Tests -
Semantic Version Checker
You can find more information about the builds here :information_source: Run only required test builds during development. Run all test builds before sending your pull request for review.
For more details, review the Code Contributions documentation. Join Magento Community Engineering Slack and ask your questions in #github channel.
Hi @mfickers, Thank you for your contribution!
I have a question: in the ticket and on your first screenshot the error is related to the \Magento\Sales\Model\Order\Creditmemo\Validation\QuantityValidator::isValidDecimalRefundQty() method. But your fix is happens at \Magento\Sales\Model\Order\CreditmemoFactory::createByInvoice. Could you elaborate that this change is related to the issue and we are not looking at two different problems? Thank you.
@swnsma
Basically \Magento\Sales\Api\Data\CreditmemoItemInterface::getQty has an annotated return type of float, but the \Magento\Sales\Model\Order\CreditmemoFactory::createByInvoice will create CreditmemoItem objects with the qty set as a string under specific circumstances.
\Magento\Sales\Model\Order\Creditmemo\Validation\QuantityValidator::isValidDecimalRefundQty has a typed parameter $itemQty which must be float. It is called with the result of CreditmemoItemInterface::getQty here: \Magento\Sales\Model\Order\Creditmemo\Validation\QuantityValidator::validateTotalQuantityRefundable.
I feel like the code in the QuantityValidator is correct, as it expects CreditmemoItemInterface::getQty to return float, which it does according to the annotation. Meanwhile the CreditmemoFactory calls \Magento\Sales\Model\Order\Creditmemo\Item::setQty with a string, even though the type annotation on setQty says it should be float.
That is why I fixed it in the Factory instead of the Validator.
But you're right, this problem exists independently of the CreditmemoFactory. If I validate a Creditmemo that was just loaded from DB, it will fail as well:
Do you want me to update the QuantityValidator to handle this?
I've added type casting to the QuantityValidator to make sure type errors regarding CreditmemoItem qty don't happen anymore.
I've also added a test case for the QuantityValidatorTest where OrderItem and CreditmemoItem qtys are both '1.0000' which is the expected value if those where loaded from DB.
@magento run all tests
@magento run all tests
@magento run Static Tests,Functional Tests B2B,Functional Tests CE,Functional Tests EE
@magento run Functional Tests B2B,Functional Tests CE,Functional Tests EE
@magento run Functional Tests B2B,Functional Tests CE,Functional Tests EE
@magento run Functional Tests B2B,Functional Tests CE,Functional Tests EE
@magento run Functional Tests B2B
Hi @mfickers, May I ask if you have intentionally closed the PR or that happened by mistake?
Wow, I referenced this MR in another (private) project and upon merging the other MR, this one was automatically closed 😳
Hi @mfickers, No problem :)
Hi @mfickers,
Thanks for the collaboration & contribution!
:heavy_multiplication_x: QA not Passed
Before: :heavy_multiplication_x:
After: :heavy_check_mark:
After taking PR changes also the issue was the same we are not able to create the credit memo.Could you please let us know still if we are missing anything.
Thanks.