Magento2: Adding new Deserializer to Rest API removes other Deserializers

Created on 17 Jan 2020  ยท  17Comments  ยท  Source: magento/magento2

Preconditions (*)

  1. Magento 2.2.x / Magento 2.3.x both CE / EE & 2.4-develop

Steps to reproduce (*)

  1. Create a new module which will add new webapi route
  2. Create a new deserializer using di.xml in your module



type="Coinpayments\Withdrawals\Model\Ipn" />

<type name="Magento\Framework\Webapi\Rest\Request\DeserializerFactory">
    <arguments>
        <argument name="deserializers" xsi:type="array">
            <item name="text_plain" xsi:type="array">
                <item name="type" xsi:type="string">text/plain</item>
                <item name="model" xsi:type="string">Custom\Module\Webapi\Rest\Request\Deserializer\Plain</item>
            </item>
        </argument>
    </arguments>
</type>

Expected result (*)

  1. New content type accepted by REST API

Actual result (*)

  1. All CORE defined content types for API are no longer working and return 400 error
    image
  1. Only new defined content type is accepted

It was my understanding that DI.XML is used to extend CORE features, not OVERWRITE them.

CD Api Config Clear Description Confirmed Format is valid Ready for Work P2 ready for dev Reported on 2.2.x Reproduced on 2.4.x S1 Dev.Experience

Most helpful comment

Hi @gabrieldagama it does make sense to warn developers but, in my opinion, this is not a solution.
In my project I only had 1 module which adds a deserializer, what if there are 3 or 5 modules with different deserializers? It will be hard to maintain the code because developer or team od developers will have to remember to double or tripple check each module which adds a deserializer.
Imho this should be fixed to maintain Magento platform flexibility.
The easiest solution: It is worth considering adding new deserializers to the core.

All 17 comments

Hi @qsolutions-pl. 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.

@qsolutions-pl do you confirm that you were able to reproduce the issue on vanilla Magento instance following steps to reproduce?

  • [ ] yes
  • [ ] no

After checking content of
generated/metadata/global.php
DeserializerFactory has only 1 argument, defined in my module, others are missing.

'Magento\\Framework\\Webapi\\Rest\\Request\\DeserializerFactory' => array ( 'objectManager' => array ( '_i_' => 'Magento\\Framework\\ObjectManagerInterface', ), 'deserializers' => array ( '_v_' => array ( 'text_plain' => array ( 'type' => 'text/plain', 'model' => 'Custom\\Module\\Webapi\\Rest\\Request\\Deserializer\\Plain', ), ), ), ),

Its the same with etc/webapi_rest/di.xml

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.

Just an update from my end:
It seems like this is a general di.xml bug / (feature??)
If you try to add a new argument to an existing <type> in your own module, it will overwrite all arguments defined in CORE or 3rd party modules.
Its the same for all areas where you can define di.xml file
In order for this to work, you have to copy+paste arguments section to your own module

Hello @qsolutions-pl
This issue is a bug.

Thanks for collaboration!

Speculating here it could be related to 'vendor/magento/framework/Config/Dom/NodeMergingConfig.php'

    public function getIdAttribute($nodeXpath)
    {
        foreach ($this->idAttributes as $pathPattern => $idAttribute) {
            if ($this->nodePathMatcher->match($pathPattern, $nodeXpath)) {
                return $idAttribute;
            }
        }
        return null;
    }

which looks to be working in conjunction with
vendor/magento/framework/ObjectManager/Config/Reader/Dom.php

    protected $_idAttributes = [
        '/config/preference' => 'for',
        '/config/(type|virtualType)' => 'name',
        '/config/(type|virtualType)/plugin' => 'name',
        '/config/(type|virtualType)/arguments/argument' => 'name',
        '/config/(type|virtualType)/arguments/argument(/item)+' => 'name',
    ];

The order of the above idAttributes seems important here. It might get a result for /config/(type|virtualType)/arguments/argument before it gets to '/config/(type|virtualType)/arguments/argument(/item)+'

or it might go too deep.

:white_check_mark: Confirmed by @engcom-Hotel
Thank you for verifying the issue. Based on the provided information internal tickets MC-30627 were created

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

@magento I am working on this

Hi @Neo-Creo! :wave:
Thank you for collaboration. Only members of Community Contributors Team are allowed to be assigned to the issue. Please use @magento add to contributors team command to join Contributors team.

@magento add to contributors team

Hi @Neo-Creo! :wave:
Thank you for joining. Please accept team invitation :point_right: here :point_left: and add your comment one more time.

@magento I am working on this

Hi @qsolutions-pl , it's a known behavior of the configuration merge process, arguments defined in the โ€œinitialโ€ config file, app/etc/di.xml (see https://devdocs.magento.com/guides/v2.4/extension-dev-guide/build/di-xml-file.html ) are overridden by the arguments defined on the module level. The common solution in this case is to duplicate original arguments and add the new ones.

We had discussions on that several times and there is no simple solution to fix it.
Right now we are investigating the mentioned case and possible options on how to resolve it, most of the cases when this issue appears are caused by the wrong config location (webapi configuration probably should be in webapi module instead of global config).

@sidolov the same issue is when you add configuration in webapi, I've checked all scenarios

Hi @qsolutions-pl,

I believe the problem is related to the stages where the arguments are defined, as described on the DevDocs (https://devdocs.magento.com/guides/v2.4/extension-dev-guide/build/di-xml-file.html#areas-and-application-entry-points), we have three stages, if you define arguments on a higher stage (the stage that is loaded later), it will overwrite the stages defined on the lower stage.

I've created a pull request to add that information as a warning on the DevDocs, hopefully, that will make it a little bit more clear. https://github.com/magento/devdocs/pull/8180

Hope that makes sense.

Hi @gabrieldagama it does make sense to warn developers but, in my opinion, this is not a solution.
In my project I only had 1 module which adds a deserializer, what if there are 3 or 5 modules with different deserializers? It will be hard to maintain the code because developer or team od developers will have to remember to double or tripple check each module which adds a deserializer.
Imho this should be fixed to maintain Magento platform flexibility.
The easiest solution: It is worth considering adding new deserializers to the core.

Was this page helpful?
0 / 5 - 0 ratings