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.47k stars 9.28k forks source link

\DateTime object should be usable in ExtensionAttributes or in relatd #34904

Closed MeCapron closed 2 years ago

MeCapron commented 2 years ago

Preconditions (*)

  1. Magento 2.4.2-p2

Steps to reproduce (*)

  1. Define DateTime extension attributes
  2. <?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd">
    <extension_attributes for="Magento\Customer\Api\Data\CustomerInterface">
        <attribute code="date" type="DateTime"/>
    </extension_attributes>
    </config>
  3. Error shows : 1 exception(s): Exception #0 (InvalidArgumentException): Each getter must have a doc block. See DateTime::format()

Expected result (*)

  1. DateTime built-in must be ignored as \Magento\Framework\Phrase is

Actual result (*)

  1. Error shows : 1 exception(s): Exception #0 (InvalidArgumentException): Each getter must have a doc block. See DateTime::format()

Possible solution

https://github.com/magento/magento2/blob/2.4.2-p2/lib/internal/Magento/Framework/Reflection/DataObjectProcessor.php#L114

we could replace this line by :

if (is_object($value) && (!($value instanceof Phrase) && !($value instanceof \DateTime))) {

and https://github.com/magento/magento2/blob/2.4.2-p2/lib/internal/Magento/Framework/Reflection/DataObjectProcessor.php#L120

if (is_object($singleValue) && (!($singleValue instanceof Phrase) && !($singleValue instanceof \DateTime))) {

We could also maybe add a di.xml field to allow such configuration or ignoring specific types. I can't measure the impact of the latest solution though.


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

m2-assistant[bot] commented 2 years ago

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

m2-assistant[bot] commented 2 years ago

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

m2-assistant[bot] commented 2 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:

m2-assistant[bot] commented 2 years ago

Hi @engcom-Echo. 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-Echo commented 2 years ago

Hi @MeCapron,

Thank you for reporting the issue.

However, we tried to reproduce it on Magento 2.4-dev but the issue is not reproducible.

Steps followed to reproduce issue:

  1. Installed the fresh Magento 2.4-dev instance.
  2. Added the following code to the extension_attributes.xml file.
    
    <?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd">
    <extension_attributes for="Magento\Customer\Api\Data\CustomerInterface">
        <attribute code="date" type="DateTime"/>
    </extension_attributes>
    </config>

Cleard cache and run di:compile to generate new files. bin/magento c:c bin/magento setup:di:compile

Following code generated after execution of di:compile under file “generated/code/Magento/Customer/Api/Data/CustomerExtensionInterface.php”

<?php namespace Magento\Customer\Api\Data;

/**

Screenshot 2022-01-13 at 7 56 16 PM

If the issue still persists at your end then please provide some more details or provide the exact steps, accordingly, I’ll try to reproduce it again.

Thanks

MeCapron commented 2 years ago

Hello, thanks for watching out this issue. I missed some information. Very sorry about this.

This issue still persists in this way :

Everything you did was correct, then you can try to use the buildOutputDataArray.

<?php

declare(strict_types=1);

namespace MyVendor\MyModule\Helper;

use Magento\Framework\Reflection\DataObjectProcessor;

class CustomerPlugin
{
    private DataObjectProcessor $dataObjectProcessor;

    private CustomerRepositoryInterface $customerRepository;

    public function __construct(
        DataObjectProcessor $dataObjectProcessor,
        CustomerRepositoryInterface $customerRepository
    ) {
        $this->dataObjectProcessor = $dataObjectProcessor;
        $this->customerRepository = $customerRepository;
    }

    public function testGetCustomer(): void
    {
        $customer = $this->customerRepository->getById(1);
        $customer->getExtensionAttributes()->setDate(new \DateTime());
        $this->dataObjectProcessor->buildOutputDataArray($customer, CustomerInterface::class);
    }
engcom-Echo commented 2 years ago

Hi @MeCapron,

I understood the issue now which you are getting but it seems it's not an actual issue, after debugging it some more I found that buildOutputDataArray($dataObject, $dataObjectType) needs an object and its type, and on the basis of object type it loads the interface. But in our case, we are using the standard PHP date time object for which Magento doesn’t have an interface available. To solve this issue we need to use Magento’s standard DateTime library.

Update your extension_attributes.xml with the following code:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd">
    <extension_attributes for="Magento\Customer\Api\Data\CustomerInterface">
        <attribute code="date" type="Magento\Framework\Stdlib\DateTime"/>
    </extension_attributes>
</config>

And while using, you have to use Stdlib\DateTime() instead the standard DateTime object. $customerDataObject->getExtensionAttributes()->setDate(new Stdlib\DateTime());

Hope it helps. Let me know if you are still face this issue or provide more relevant details if you think it is Magento core issue ?

Thanks

engcom-Hotel commented 2 years ago

Dear @MeCapron,

We have noticed that this issue has not been updated for a period of 14 Days. Hence we assume that this issue is fixed now, so we are closing it. Please raise a fresh ticket or reopen this ticket if you need more assistance on this.

Regards

MeCapron commented 2 years ago

Hi @MeCapron,

I understood the issue now which you are getting but it seems it's not an actual issue, after debugging it some more I found that buildOutputDataArray($dataObject, $dataObjectType) needs an object and its type, and on the basis of object type it loads the interface. But in our case, we are using the standard PHP date time object for which Magento doesn’t have an interface available. To solve this issue we need to use Magento’s standard DateTime library.

Update your extension_attributes.xml with the following code:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd">
    <extension_attributes for="Magento\Customer\Api\Data\CustomerInterface">
        <attribute code="date" type="Magento\Framework\Stdlib\DateTime"/>
    </extension_attributes>
</config>

And while using, you have to use Stdlib\DateTime() instead the standard DateTime object. $customerDataObject->getExtensionAttributes()->setDate(new Stdlib\DateTime());

Hope it helps. Let me know if you are still face this issue or provide more relevant details if you think it is Magento core issue ?

Thanks

Hi!

I do not really aggree about using Magento main DateTime. What about external objects that are using DateTime of any kind of PHP object? Would it mean that if the core function has no docblock, we will just skip it? Plus, you are blocking the use of external objects that are not part of Magento system (such as serialized DTOs from API calls)

I think that all of the native PHP objects should not be part of the reflection in this system, or should be handled without any doc block (which can not be done in some cases).

What do you think about this?

Melvin

MeCapron commented 2 years ago

Dear @MeCapron,

We have noticed that this issue has not been updated for a period of 14 Days. Hence we assume that this issue is fixed now, so we are closing it. Please raise a fresh ticket or reopen this ticket if you need more assistance on this.

Regards

Hello,

Can you reopen this issue please? This is still an issue.

Thanks