Skip to content

Magento 2.3.4 Quote Address Extension Attribute Issue #26682

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
NathMorgan opened this issue Feb 3, 2020 · 17 comments · Fixed by #27338
Closed

Magento 2.3.4 Quote Address Extension Attribute Issue #26682

NathMorgan opened this issue Feb 3, 2020 · 17 comments · Fixed by #27338
Assignees
Labels
Component: Checkout Component: Quote Component: Shipping Fixed in 2.4.x The issue has been fixed in 2.4-develop branch Issue: Clear Description Gate 2 Passed. Manual verification of the issue description passed Issue: Format is valid Gate 1 Passed. Automatic verification of issue format passed Reported on 2.3.4 Indicates original Magento version for the Issue report. Reproduced on 2.3.x The issue has been reproduced on latest 2.3 release Reproduced on 2.4.x The issue has been reproduced on latest 2.4-develop branch Severity: S2 Major restrictions or short-term circumventions are required until a fix is available. Triage: Dev.Experience Issue related to Developer Experience and needs help with Triage to Confirm or Reject it

Comments

@NathMorgan
Copy link
Member

NathMorgan commented Feb 3, 2020

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: 6900c38

This seems to effect any extension attribute set on Magento\Quote\Api\Data\AddressInterface that is not a object

@magento-deployment-service
Copy link

Thanks for opening this issue!

@m2-assistant
Copy link

m2-assistant bot commented Feb 3, 2020

Hi @NathMorgan. Thank you for your report.
To help us process this issue please make sure that you provided the following information:

  • Summary of the issue
  • Information on your environment
  • Steps to reproduce
  • Expected and actual results

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?

  • yes
  • no

@magento-engcom-team magento-engcom-team added the Issue: Format is valid Gate 1 Passed. Automatic verification of issue format passed label Feb 3, 2020
@magento-deployment-service
Copy link

Thanks for opening this issue!

@NathMorgan
Copy link
Member Author

Currently working on a fix

@NathMorgan NathMorgan self-assigned this Feb 4, 2020
@m2-assistant
Copy link

m2-assistant bot commented Feb 4, 2020

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: 👇

  • 1. Verify that issue has all the required information. (Preconditions, Steps to reproduce, Expected result, Actual result).

    DetailsIf the issue has a valid description, the label Issue: Format is valid will be added to the issue automatically. Please, edit issue description if needed, until label Issue: Format is valid appears.

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

  • 3. Add Component: XXXXX label(s) to the ticket, indicating the components it may be related to.

  • 4. Verify that the issue is reproducible on 2.4-develop branch

    Details- Add the comment @magento give me 2.4-develop instance to deploy test instance on Magento infrastructure.
    - If the issue is reproducible on 2.4-develop branch, please, add the label Reproduced on 2.4.x.
    - If the issue is not reproducible, add your comment that issue is not reproducible and close the issue and stop verification process here!


@Swahjak
Copy link
Contributor

Swahjak commented Feb 10, 2020

This seems to be specifically caused by 6900c38#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
Copy link
Contributor

Swahjak commented Feb 11, 2020

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
Copy link
Member Author

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

@romainruaud romainruaud added Component: Checkout Component: Quote Issue: Clear Description Gate 2 Passed. Manual verification of the issue description passed Component: Shipping Reproduced on 2.3.x The issue has been reproduced on latest 2.3 release Reproduced on 2.4.x The issue has been reproduced on latest 2.4-develop branch labels Mar 4, 2020
@amenk
Copy link
Contributor

amenk commented Mar 18, 2020

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

@amenk
Copy link
Contributor

amenk commented Mar 18, 2020

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

azambon commented Apr 16, 2020

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

amenk commented Apr 16, 2020

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

azambon commented Apr 16, 2020

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

amenk commented Apr 16, 2020

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

@azambon
Copy link
Contributor

azambon commented Apr 16, 2020

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

@ihor-sviziev ihor-sviziev added the Severity: S2 Major restrictions or short-term circumventions are required until a fix is available. label Apr 28, 2020
@sdzhepa sdzhepa added the Triage: Dev.Experience Issue related to Developer Experience and needs help with Triage to Confirm or Reject it label May 20, 2020
@ihor-sviziev
Copy link
Contributor

Hi,

This issue was already fixed in 2.4-develop branch in fcd1b58#diff-e2b9d1a5f4b9f3503a2f8dd072bc2d8aR360.

Additional test coverage will be also added in #27338.

@ihor-sviziev ihor-sviziev added the Fixed in 2.4.x The issue has been fixed in 2.4-develop branch label Jun 5, 2020
magento-engcom-team added a commit that referenced this issue Jun 13, 2020
#27338

 - Merge Pull Request #27338 from iMi-digital/magento2:fix/26682-no-overwriting-extension-attributes
 - Merged commits:
   1. ea70c0b
   2. dcb59d3
   3. bc18a8f
   4. 013bbc9
   5. 7d86388
   6. 517bc6e
   7. c29e323
   8. b60504e
   9. 65c7153
   10. 0753860
   11. f133bff
   12. c38d998
   13. 579712b
   14. 0518666
   15. 2aae8ae
@magento-engcom-team magento-engcom-team added the Reported on 2.3.4 Indicates original Magento version for the Issue report. label Nov 13, 2020
ngantrant pushed a commit to ngantrant/magento-2-open-source that referenced this issue May 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component: Checkout Component: Quote Component: Shipping Fixed in 2.4.x The issue has been fixed in 2.4-develop branch Issue: Clear Description Gate 2 Passed. Manual verification of the issue description passed Issue: Format is valid Gate 1 Passed. Automatic verification of issue format passed Reported on 2.3.4 Indicates original Magento version for the Issue report. Reproduced on 2.3.x The issue has been reproduced on latest 2.3 release Reproduced on 2.4.x The issue has been reproduced on latest 2.4-develop branch Severity: S2 Major restrictions or short-term circumventions are required until a fix is available. Triage: Dev.Experience Issue related to Developer Experience and needs help with Triage to Confirm or Reject it
Projects
None yet
8 participants