magento2 icon indicating copy to clipboard operation
magento2 copied to clipboard

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

Open ilnytskyi opened this issue 4 years ago • 11 comments

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

  • [ ] Severity: S0 - Affects critical data or functionality and leaves users without workaround.
  • [ ] Severity: S1 - Affects critical data or functionality and forces users to employ a workaround.
  • [ ] Severity: S2 - Affects non-critical data or functionality and forces users to employ a workaround.
  • [ ] Severity: S3 - Affects non-critical data or functionality and does not force users to employ a workaround.
  • [ ] Severity: S4 - Affects aesthetics, professional look and feel, “quality” or “usability”.

ilnytskyi avatar Oct 28 '21 10:10 ilnytskyi

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

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

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.


:warning: According to the Magento Contribution requirements, all issues must go through the Community Contributions Triage process. Community Contributions Triage is a public meeting.

: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

m2-assistant[bot] avatar Oct 28 '21 10:10 m2-assistant[bot]

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

sezio avatar Nov 17 '21 10:11 sezio

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:

  • [ ] 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!

  • [ ] 5. Add label Issue: Confirmed once verification is complete.

  • [ ] 6. Make sure that automatic system confirms that report has been added to the backlog.

m2-assistant[bot] avatar Nov 24 '21 01:11 m2-assistant[bot]

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.

  • Create a custom module.

  • Create a global area di.xml. image

  • Create di.xml in adminhtml area. image

  • Create respective classes defined in di.xml image

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

engcom-Hotel avatar Nov 30 '21 14:11 engcom-Hotel

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

github-jira-sync-bot avatar Nov 30 '21 14:11 github-jira-sync-bot

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

m2-assistant[bot] avatar Nov 30 '21 14:11 m2-assistant[bot]

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

sidolov avatar Dec 02 '21 17:12 sidolov

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

ilnytskyi avatar Dec 02 '21 19:12 ilnytskyi

@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

kandy avatar Dec 02 '21 20:12 kandy

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

ilnytskyi avatar Dec 03 '21 14:12 ilnytskyi

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.

azambon avatar Feb 26 '24 16:02 azambon

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

atty31 avatar Jul 04 '24 11:07 atty31

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

azambon avatar Jul 04 '24 12:07 azambon