Magento2: DateTime type not working with Web API

Created on 30 Sep 2019  路  8Comments  路  Source: magento/magento2


When trying to request Web API end-point which has an interface with methods returning DateTime Magento throws the following error:

Each getter must have a doc block. See DateTime::format()

Preconditions (*)

  1. Magento version 2.3-develop
  2. PHP v7.2

Steps to reproduce (*)

  1. Create a new interface as shown below.
interface TestInterface
{
    /**
     * Gets the current date.
     * 
     * @return \DateTime
     */
    public function getDate() : \DateTime;
}
  1. Implement the interface and simply return an a new DateTime object.
class TestApi implements TestInterface
{
    /**
     * Gets the current date.
     * 
     * @return \DateTime
     */
    public function getDate() : \DateTime {
        return new \DateTime();
    }
}
  1. Create an entry in webapi.xml pointing to the service.
    <route method="GET" url="/V1/test">
        <service class="TestInterface" method="getDate"/>
        <resources>
            <resource ref="anonymous"/>
        </resources>
    </route>
  1. Call the service using curl.

Expected result (*)

  1. The DateTime should be correctly serialized.
  2. When using JSON serializer, the output should be in ISO8601

Actual result (*)

  1. An error is thrown as shown below.
Each getter must have a doc block. See DateTime::format()
Webapi Clear Description Confirmed Format is valid Ready for Work done Reproduced on 2.3.x Dev.Experience

All 8 comments

Hi @zingix. 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.3-develop instance - upcoming 2.3.x release

For more details, please, review the Magento Contributor Assistant documentation.

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

  • [ ] yes
  • [ ] no

Hi @engcom-Charlie. 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.3-develop branch

    Details- Add the comment @magento give me 2.3-develop instance to deploy test instance on Magento infrastructure.
    - If the issue is reproducible on 2.3-develop branch, please, add the label Reproduced on 2.3.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.

Hi @engcom-Charlie , do I need to do anything? Can I help with providing more infomration?

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

Issue Available: @engcom-Charlie, _You will be automatically unassigned. Contributors/Maintainers can claim this issue to continue. To reclaim and continue work, reassign the ticket to yourself._

Hello @zingix ! No, thanks. I reproduced this issue.

Hi @kkorushkin. Thank you for working on this issue.
Looks like this issue is already verified and confirmed. But if you want to validate it one more time, please, go though the following instruction:

  • [ ] 1. Add/Edit Component: XXXXX label(s) to the ticket, indicating the components it may be related to.
  • [ ] 2. Verify that the issue is reproducible on 2.3-develop branch

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

  • [ ] 3. If the issue is not relevant or is not reproducible any more, feel free to close it.


Hi @zingix, thanks for reporting this, I believe the reason for not having a serializer for the DateTime class is even though there is an ISO for standardization, the format of the DateTime still may need to be different depending on country or client.

We recommend to your getDate method to return a string formatted on the way need.

I will be closing this issue since there are no plans to implement such a feature.

Hi @gabrieldagama, I do not agree that the date should be formatted as string. Using this method, you cannot directly load date types from the database and use the Magento default tools to have it properly mapped - except you store the dates as strings as well.

I added this issue for the API not Magento in general. The API should exactly be client independent and close to standard as possible. It is way easier to e. g. in front-end use libraries like moment.js for bringing the date into the clients preferred format. Think of multi-country / multi-language sites. In your scenario you would need to have an endpoint for each config.

To be honest, after waiting more than one year to get a focus on this issue I also do not expect it to be solved.

Was this page helpful?
0 / 5 - 0 ratings