magento / magento2

Prior to making any Submission(s), you must sign an Adobe Contributor License Agreement, available here at: https://opensource.adobe.com/cla.html. All Submissions you make to Adobe Inc. and its affiliates, assigns and subsidiaries (collectively “Adobe”) are subject to the terms of the Adobe Contributor License Agreement.
http://www.magento.com
Open Software License 3.0
11.49k stars 9.31k forks source link

Magento 2.3.4 Quote Address Extension Attribute Issue #26682

Closed NathMorgan closed 4 years ago

NathMorgan commented 4 years ago

Preconditions (*)

  1. Using Magento 2.3.4
  2. USing a custom extension attribute on Magento\Quote\Api\Data\AddressInterface Example module here: https://github.com/NathMorgan/module-test-module

Steps to reproduce (*)

  1. Sending a post request to estimate-shipping-methods containing a extension_attributes

For 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"}}}

Expected result (*)

  1. Above action completed successfully

Actual result (*)

  1. 500 Error with the message Uncaught Error: Call to a member function setDiscounts() on array in /var/www/html/vendor/magento/module-sales-rule/Model/Quote/Discount.php: 117

This 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

magento-deployment-service[bot] commented 4 years ago

Thanks for opening this issue!

m2-assistant[bot] commented 4 years ago

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?


magento-deployment-service[bot] commented 4 years ago

Thanks for opening this issue!

NathMorgan commented 4 years ago

Currently working on a fix

m2-assistant[bot] commented 4 years ago

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:


Swahjak commented 4 years ago

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.

Swahjak commented 4 years ago

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:

  1. ~Since 'extraAddressData' uses the \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).~
  2. Add a line right after the add / extract data line $shippingAddress->setExtensionAttributes($address->getExtensionAttributes());. (3. Don't break stuff in minor releases)

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.

NathMorgan commented 4 years ago

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

amenk commented 4 years ago

@NathMorgan are you still working on this? Is there any WIP branch or workaround?

amenk commented 4 years ago

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);
azambon commented 4 years ago

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);
amenk commented 4 years ago

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.

azambon commented 4 years ago

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?

amenk commented 4 years ago

In composer-patches there is a level-parameter which was just invented for that :)

azambon commented 4 years ago

@amenk Didn't know about that parameter. I'll look into it. Thank you :+1:

ihor-sviziev commented 4 years ago

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.

magento-engcom-team commented 4 years ago

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.