magento2 icon indicating copy to clipboard operation
magento2 copied to clipboard

Fix type error for item qty when refunding an invoice

Open mfickers opened this issue 2 months ago • 16 comments

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: CleanShot 2025-11-18 at 14 48 06

This PR casts the invoice item quantity to float to prevent the type error.

Related Pull Requests

Fixed Issues (if relevant)

  1. Fixes magento/magento2#40302

Manual testing scenarios (*)

  1. Create an order with one item with qty 1
  2. Create invoice for order
  3. Make a call to invoice/invoice_id/refund and 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: CleanShot 2025-11-18 at 14 47 43 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)

mfickers avatar Nov 18 '25 13:11 mfickers

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:
  1. Database Compare
  2. Functional Tests CE
  3. Functional Tests EE
  4. Functional Tests B2B
  5. Integration Tests
  6. Magento Health Index
  7. Sample Data Tests CE
  8. Sample Data Tests EE
  9. Sample Data Tests B2B
  10. Static Tests
  11. Unit Tests
  12. WebAPI Tests
  13. 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.

m2-assistant[bot] avatar Nov 18 '25 13:11 m2-assistant[bot]

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 avatar Nov 19 '25 09:11 swnsma

@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.

mfickers avatar Nov 19 '25 10:11 mfickers

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: CleanShot 2025-11-19 at 12 11 16

Do you want me to update the QuantityValidator to handle this?

mfickers avatar Nov 19 '25 11:11 mfickers

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.

mfickers avatar Nov 19 '25 13:11 mfickers

@magento run all tests

swnsma avatar Nov 19 '25 16:11 swnsma

@magento run all tests

swnsma avatar Nov 20 '25 09:11 swnsma

@magento run Static Tests,Functional Tests B2B,Functional Tests CE,Functional Tests EE

swnsma avatar Nov 20 '25 12:11 swnsma

@magento run Functional Tests B2B,Functional Tests CE,Functional Tests EE

swnsma avatar Nov 21 '25 08:11 swnsma

@magento run Functional Tests B2B,Functional Tests CE,Functional Tests EE

swnsma avatar Nov 21 '25 13:11 swnsma

@magento run Functional Tests B2B,Functional Tests CE,Functional Tests EE

swnsma avatar Nov 22 '25 09:11 swnsma

@magento run Functional Tests B2B

swnsma avatar Nov 22 '25 22:11 swnsma

Hi @mfickers, May I ask if you have intentionally closed the PR or that happened by mistake?

swnsma avatar Dec 01 '25 14:12 swnsma

Wow, I referenced this MR in another (private) project and upon merging the other MR, this one was automatically closed 😳

mfickers avatar Dec 01 '25 14:12 mfickers

Hi @mfickers, No problem :)

swnsma avatar Dec 01 '25 14:12 swnsma

Hi @mfickers,

Thanks for the collaboration & contribution!

:heavy_multiplication_x: QA not Passed

Before: :heavy_multiplication_x:

Screenshot 2025-12-10 at 3 04 43 pm

After: :heavy_check_mark:

Screenshot 2025-12-10 at 3 14 00 pm Screenshot 2025-12-10 at 3 11 03 pm

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.

engcom-Bravo avatar Dec 10 '25 09:12 engcom-Bravo