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.57k stars 9.32k forks source link

Bug in DI generation: array arguments got replaced instead of merging when modified from different area #34464

Open ilnytskyi opened 3 years ago

ilnytskyi commented 3 years ago

Preconditions (*)

  1. Create a configurable object or virtual type e.g. composite
  2. It's impossible to add new items into array argument for different area.
  3. Related problem was reported here: https://stackoverflow.com/questions/34635615/magento-2-how-to-add-new-value-to-the-array-on-di-xml

Other should areas extend di configuration from global, or override only changed items/arguments. But in case with array arguments the DI compilation process overrides entire array instead of merging its items.

See the code in methods:

\Magento\Framework\ObjectManager\Config\Config::_mergeConfiguration L248
\Magento\Framework\ObjectManager\Config\Config::_collectConfiguration L210

Selection_318 the arguments will be replaced Selection_319

Steps to reproduce (*)

  1. Configure an object in global area
    GLOBAL area di.xml
    <virtualType name="SomeCompositeHandler" type="Magento\Payment\Gateway\Response\HandlerChain">
    <arguments>
        <argument name="handlers" xsi:type="array">
            <item name="some_handler_global" xsi:type="string">SomeHandlerGlobal</item>
        </argument>
    </arguments>
    </virtualType>
  2. Modify the object in adminhtml area
    ADMINHTML area di.xml
    <virtualType name="SomeCompositeHandler">
    <arguments>
        <argument name="handlers" xsi:type="array">
            <item name="some_handler_for_admin" xsi:type="string">SomeHandlerForAdmin</item>
        </argument>
    </arguments>
    </virtualType>

Expected result (*)

  1. The final object in code contains handlers from both areas as if it was defined like this in di.xml
    Expected result for ADMINHTML area:
    <virtualType name="SomeCompositeHandler">
    <arguments>
        <argument name="handlers" xsi:type="array">
            <item name="some_handler_global" xsi:type="string">SomeHandlerGlobal</item>
            <item name="some_handler_for_admin" xsi:type="string">SomeHandlerForAdmin</item>
        </argument>
    </arguments>
    </virtualType>

Actual result (*)

  1. The handlers array argument got overridden instead of merged and works only the way defined in admin.
    Actual result for ADMINHTML area:
    <virtualType name="SomeCompositeHandler">
    <arguments>
        <argument name="handlers" xsi:type="array">
            <item name="some_handler_for_admin" xsi:type="string">SomeHandlerForAdmin</item>
        </argument>
    </arguments>
    </virtualType>

    So global handler is lost.

WORKAROUND:

Add all missed arguments from global area if you want to add additional ones for target area.

Might be also related to: https://github.com/magento/magento2/issues/34394 https://github.com/magento/magento2/issues/28193 https://github.com/magento/magento2/issues/31468 https://github.com/magento/magento2/pull/15042

The issue is applicable also for cases when we follow guides like this: https://devdocs.magento.com/guides/v2.4/inventory/source-selection-algorithms.html#configure-dixml But what is I want to add new algorithm that is available only in certain area? Then all other algorithms added by modules will be removed, it's unexpected behavior!!


Please provide Severity assessment for the Issue as Reporter. This information will help during Confirmation and Issue triage processes.

m2-assistant[bot] commented 3 years ago

Hi @ilnytskyi. Thank you for your report. To speed up processing of this issue, make sure that you provided the following information:

Make sure that the issue is reproducible on the vanilla Magento instance following Steps to reproduce. To deploy vanilla Magento instance on our environment, Add a comment to the issue:

@magento give me 2.4-develop instance - upcoming 2.4.x release

For more details, review the Magento Contributor Assistant documentation.

Add a comment to assign the issue: @magento I am working on this

To learn more about issue processing workflow, refer to the Code Contributions.


:clock10: You can find the schedule on the Magento Community Calendar page.

:telephone_receiver: The triage of issues happens in the queue order. If you want to speed up the delivery of your contribution, join the Community Contributions Triage session to discuss the appropriate ticket.

:movie_camera: You can find the recording of the previous Community Contributions Triage on the Magento Youtube Channel

:pencil2: Feel free to post questions/proposals/feedback related to the Community Contributions Triage process to the corresponding Slack Channel

sezio commented 3 years ago

Can confirm issue : I met the same problem with the graphql and the global area. Arguments were replaced.

m2-assistant[bot] commented 3 years ago

Hi @engcom-Hotel. 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:

engcom-Hotel commented 2 years ago

Hi @ilnytskyi

Thank you for reporting the issue.

We are able to reproduce it on Magento 2.4-develop

Steps followed to reproduce this issue.

And image

Now use TestB class in any other class and set a breakpoint in Constructor. This $testObject should contains 3 values in $stringData arguments of Test Class. image

Thanks

github-jira-sync-bot commented 2 years ago

:white_check_mark: Jira issue https://jira.corp.magento.com/browse/AC-1903 is successfully created for this GitHub issue.

m2-assistant[bot] commented 2 years ago

:white_check_mark: Confirmed by @engcom-Hotel. Thank you for verifying the issue.
Issue Available: @engcom-Hotel, You will be automatically unassigned. Contributors/Maintainers can claim this issue to continue. To reclaim and continue work, reassign the ticket to yourself.

sidolov commented 2 years ago

Hi @ilnytskyi ! We just went through the issue and found that mentioned behavior is expected and documented on the DevDocs, please refer to https://devdocs.magento.com/guides/v2.4/extension-dev-guide/build/di-xml-file.html#argument-types page

Screen Shot 2021-12-02 at 11 16 53
ilnytskyi commented 2 years ago

@sidolov well, ok. But for some cases it's not suitable. In my case I faced this issue when wanted to add some additional handlers for payment method in adminhtml area, and noticed that original ones are not present. Would it be possible to improve DI with possibility to manage if new type should replace or extended/merge an array in different area? just like an attribute shared="true|false" and maybe possibility to remove array items as in layout xml. That would make DI a lot more flexible.

kandy commented 2 years ago

@ilnytskyi Can you provide example of configuration that does not work for you. I believe that if you add base configuration to your area configuration, all should work as expected.

Add an attribute that will define how array merging is behaving is a good idea, we also think about it, but it will require complex implementation/testing. Feel free to submit PR with implementation if you want

ilnytskyi commented 2 years ago

@kandy my case if the same as I described and what magento team confirmed as a bug.

I believe that if you add base configuration to your area configuration, all should work as expected. Yes, that exactly what I described in workaround section.

azambon commented 9 months ago

Just found a real-world case: a third party module wants to add an extensionAction to the product "read" action, but only for the webapi_rest environment in order to add some custom data to what that API produces. As a result, all the default extensionActions defined by Magento (such as the ones that save configurable products extension attributes) get wiped out.

atty31 commented 4 months ago

@azambon Tell me that you were referring to the Amasty PriceHistory module ?

azambon commented 4 months ago

@azambon Tell me that you were referring to the Amasty PriceHistory module ?

@atty31 Yep, that's the one. I didn't mention which module it was because any module that tries to add an extensionAction would encounter the same problem, so naming one would not be useful in solving the issue.