Skip to content

Conversation

@mfickers
Copy link
Contributor

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 [Issue] TypeError in Creditmemo\Validation\QuantityValidator caused by min() with string quantity #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)

@m2-assistant
Copy link

m2-assistant bot commented Nov 18, 2025

Hi @mfickers. Thank you for your contribution!
Here are some useful tips on how you can test your changes using Magento test environment.
❗ 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
ℹ️ 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.

@swnsma
Copy link
Contributor

swnsma commented Nov 19, 2025

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.

@mfickers
Copy link
Contributor Author

@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
Copy link
Contributor Author

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?

Copy link
Contributor

@swnsma swnsma left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @mfickers,

Thank you for the explanation!
Yes please, I see much sense of adding type casting to the \Magento\Sales\Model\Order\Creditmemo\Validation\QuantityValidator as well.

According to the definition of done I also need to ask you to cover this change with tests.
Could you extend an unit test to cover this explicit case with type casting? Thank you.

@mfickers mfickers force-pushed the feature/40302-invoice-refund-qty-type branch from 6f10e2a to 15e71b1 Compare November 19, 2025 13:18
@mfickers
Copy link
Contributor Author

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.

@swnsma
Copy link
Contributor

swnsma commented Nov 19, 2025

@magento run all tests

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Issue] TypeError in Creditmemo\Validation\QuantityValidator caused by min() with string quantity

2 participants