Magento\Quote\Api\Data\AddressInterface Example module here: https://github.com/NathMorgan/module-test-moduleestimate-shipping-methods containing a extension_attributesFor example a post request to /rest/default/V1/guest-carts/{cartId}/estimate-shipping-methods with the following example data {"address":{"street":["Example High Street"],"city":"Example City","region_id":null,"region":null,"country_id":"GB","postcode":"TES 01","company":"Example Company","telephone":"0000 00000","fax":null,"extension_attributes":{"test_attribute":"2"}}}
Uncaught Error: Call to a member function setDiscounts() on array in /var/www/html/vendor/magento/module-sales-rule/Model/Quote/Discount.php: 117This issue seems to be introduced with this commit: https://github.com/magento/magento2/commit/6900c38941850706d9bff3aae76a428afae1343c
This seems to effect any extension attribute set on Magento\Quote\Api\Data\AddressInterface that is not a object
Thanks for opening this issue!
Hi @NathMorgan. Thank you for your report.
To help us process this issue please make sure that you provided the following information:
Please make sure that the issue is reproducible on the vanilla Magento instance following Steps to reproduce. To deploy vanilla Magento instance on our environment, please, add a comment to the issue:
@magento give me 2.4-develop instance - upcoming 2.4.x release
For more details, please, review the Magento Contributor Assistant documentation.
@NathMorgan do you confirm that you were able to reproduce the issue on vanilla Magento instance following steps to reproduce?
Thanks for opening this issue!
Currently working on a fix
Hi @NathMorgan. Thank you for working on this issue.
In order to make sure that issue has enough information and ready for development, please read and check the following instruction: :point_down:
Issue: Format is valid will be added to the issue automatically. Please, edit issue description if needed, until label Issue: Format is valid appears.[x] 2. Verify that issue has a meaningful description and provides enough information to reproduce the issue. If the report is valid, add Issue: Clear Description label to the issue by yourself.
[x] 3. Add Component: XXXXX label(s) to the ticket, indicating the components it may be related to.
2.4-develop branch@magento give me 2.4-develop instance to deploy test instance on Magento infrastructure. 2.4-develop branch, please, add the label Reproduced on 2.4.x.This seems to be specifically caused by https://github.com/magento/magento2/commit/6900c38941850706d9bff3aae76a428afae1343c#diff-0dba08a1fa5d66da0b3c252c4949907eR106 which populates the extension attribute. Then when it passes through https://github.com/magento/magento2/blob/2.4-develop/app/code/Magento/Quote/Model/ShippingMethodManagement.php#L312 it will be converted to array by the \Magento\Framework\Reflection\DataObjectProcessor. Then the addData will blindly overwrite extension_attributes with an array value.
This could be a 'custom' implementation error only, because this will only happen if the discount collector is called before (any public function that depends on getShippingMethods) ShippingMethodManager.
On a side note; this would also cause errors if any other extension attribute is added that doesn't default to null.
Dug in a little deeper and in my opinion the solution / problem is this line https://github.com/magento/magento2/blob/2.4-develop/app/code/Magento/Quote/Model/ShippingMethodManagement.php#L312
I think there are two solutions to the problem:
\Magento\Framework\Reflection\DataObjectProcessor it would make sense to restore the data using the Magento\Framework\Webapi\ServiceInputProcessor to restore the data. This already has build in support for extension attributes. Although it would be more consistent if the \Magento\Framework\Reflection\DataObjectProcessor would also be replaced with Magento\Framework\Webapi\ServiceOutputProcessor (although the more or less give the same result).~$shippingAddress->setExtensionAttributes($address->getExtensionAttributes());.The second option is the easiest to implement but doen't really 'fix' the issue, it just works around the issue in a non-standard way.
Edit: Strike the first solution. I thought the ServiceInputProcessor could be used as a 'hydrator' but it can not. This would require some ugly recursive merge which would most definitely cause issues.
@Swahjak Sadly the 2nd method is not as simple due to the fact that the $address param can be multiple diferent class types (That contain there own type of extension attributes). I've currently pushing up a change that was done to "fix" this (Only sets data if both the $address and the $shippingAddress extension attributes match)
@NathMorgan are you still working on this? Is there any WIP branch or workaround?
For our case the following patch seems to work. We just avoid overwriting the extension_attributes. Is is really necessary to overwrite them using extractAddressData() ? I think it is just a sideeffect and extension attributes should not be overwritten in that way
--- ShippingMethodManagement.orig.php 2020-03-18 15:37:24.262000000 +0100
+++ ShippingMethodManagement.php 2020-03-18 15:37:30.458462262 +0100
@@ -297,7 +297,12 @@
{
$output = [];
$shippingAddress = $quote->getShippingAddress();
- $shippingAddress->addData($this->extractAddressData($address));
+
+ $addressData = $this->extractAddressData($address);
+ if (array_key_exists('extension_attributes', $addressData)) {
+ unset($addressData['extension_attributes']);
+ }
+ $shippingAddress->addData($addressData);
$shippingAddress->setCollectShippingRates(true);
$this->totalsCollector->collectAddressTotals($quote, $shippingAddress);
I tried @amenk 's patch and it seems to work. My only doubt is whether the same thing should be done for "custom attributes" as well.
In case anyone else wants to try that same patch without editing the core, I turned it onto a diff that can be applied automatically with Composer Patches. Just save this diff into a file and configure Composer Patches to apply it to the magento/module-quote module:
diff --git a/Model/ShippingMethodManagement.php b/Model/ShippingMethodManagement.php
index 1b86f06..56edacf 100644
--- a/Model/ShippingMethodManagement.php
+++ b/Model/ShippingMethodManagement.php
@@ -297,7 +297,16 @@ class ShippingMethodManagement implements
{
$output = [];
$shippingAddress = $quote->getShippingAddress();
- $shippingAddress->addData($this->extractAddressData($address));
+
+ //Patch for Magento issue #26682
+ /** @see https://github.com/magento/magento2/issues/26682 */
+ $addressData = $this->extractAddressData($address);
+ if (array_key_exists('extension_attributes', $addressData)) {
+ unset($addressData['extension_attributes']);
+ }
+ $shippingAddress->addData($addressData);
+ //End patch
+
$shippingAddress->setCollectShippingRates(true);
$this->totalsCollector->collectAddressTotals($quote, $shippingAddress);
Thanks @azambon . You can also get a diff by adding .diff to the PR's URL:
https://patch-diff.githubusercontent.com/raw/magento/magento2/pull/27338.diff
Can even be referenced from composer patches directly.
Hmm, are you sure that it works using the straight PR diff?
The issue that I see is the file path. In the PR diff the path is app/code/Magento/Quote/Model/ShippingMethodManagement.php, which is correct if one wants to apply the patch to a clone of the main Magento repo.
But if one wants to patch a regular Magento 2 installation where each module is a separate dependency ( https://devdocs.magento.com/guides/v2.3/install-gde/composer.html ), then the file path in the diff must be changed accordingly to refer to the root of the single component.
Am I missing something?
In composer-patches there is a level-parameter which was just invented for that :)
@amenk Didn't know about that parameter. I'll look into it. Thank you :+1:
Hi,
This issue was already fixed in 2.4-develop branch in https://github.com/magento/magento2/commit/fcd1b5808625364e09dc6aea2d9eb8c0ee209d25#diff-e2b9d1a5f4b9f3503a2f8dd072bc2d8aR360.
Additional test coverage will be also added in https://github.com/magento/magento2/pull/27338.
Hi @NathMorgan. Thank you for your report.
The issue has been fixed in magento/magento2#27338 by @amenk in 2.4-develop branch
Related commit(s):
The fix will be available with the upcoming 2.4.1 release.
Most helpful comment
@Swahjak Sadly the 2nd method is not as simple due to the fact that the
$addressparam can be multiple diferent class types (That contain there own type of extension attributes). I've currently pushing up a change that was done to "fix" this (Only sets data if both the $address and the $shippingAddress extension attributes match)