class DateInterval is not a primitive type

Created on 7 October 2019, about 5 years ago
Updated 4 June 2024, 6 months ago

TLDR : creating a DateTime object from string data is not actually a type casting.

The duration properties of a Duration Field item value is a DateIntervalData data type (plugin id: php_date_interval).

This plugin extends PrimitiveBase and convert the string value to a DateInterval object in its getCastedData() method.

I think this does not respect what PrimitiveBase should be used for (representing primitive types only, which a DateInterval is not). As a result, it messes up the Serialization API, ending up with NULL values when a Duration Field value is serialized to a JSON:API response :

            "field_duration": {
                "seconds": 150,
                "duration": null
            },

This is because the normalizer of PrimitiveType only works for... primitive types (arrays, string, numbers) and object that have a __toString().

If we keep DateIntervalData a subclass of PrimitiveType, I think that getCastedData() should return the $value as is (as a string) and we add a getDateInterval() to DataIntervalInterface to accomodate existing code which need to retreive a DateInterval.

πŸ› Bug report
Status

Fixed

Version

2.0

Component

Code

Created by

πŸ‡¨πŸ‡¦Canada garphy

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡³πŸ‡±Netherlands Martijn de Wit πŸ‡³πŸ‡± The Netherlands

    Ran into this problem during a project. Where we use the JSON API to unlock Drupal fields to a decoupled front end.

    After installing the patch we get values back and the error message regarding sterilization is gone.

    Our error message:
    TypeError: Drupal\serialization\Normalizer\PrimitiveDataNormalizer::normalize(): Return value must be of type ArrayObject|array|string|int|float|bool|null, DateInterval returned in Drupal\serialization\Normalizer\PrimitiveDataNormalizer->normalize()

  • πŸ‡«πŸ‡·France GPZ

    Same problem here, With drupal 9.5 and the JSONAPI we had no problem (but the duration field was null).
    with drupal D10.1.5 we ran into the same prvious error message in the JSONAPI.
    And the patch fixed the error and also now the duration field is not null

                    "field_duration": {
                        "seconds": 3600,
                        "duration": "PT1H"
                    },
    
  • πŸ‡ΊπŸ‡ΈUnited States kdomenick Pennsylvania

    After upgrading to D10, we had the same issue and error with JSONAPI endpoints that contained Duration fields. Endpoints with Duration fields were unavailable and this error displayed in the error logs when trying to access them:
    TypeError: Drupal\serialization\Normalizer\PrimitiveDataNormalizer::normalize(): Return value must be of type ArrayObject|array|string|int|float|bool|null, DateInterval returned in Drupal\serialization\Normalizer\PrimitiveDataNormalizer->normalize() (line 36 of /var/www/html/web/core/modules/serialization/src/Normalizer/PrimitiveDataNormalizer.php).

    Just confirming that the patch resolved the issue for us.

  • Status changed to RTBC 12 months ago
  • πŸ‡«πŸ‡·France bakop Lille

    Same here, the patch #7 resolved the issue for me.
    I'm on a Drupal 10.1.6

  • Status changed to Needs review 10 months ago
  • πŸ‡ΊπŸ‡¦Ukraine sickness29

    Fixed minor issues with #7 patch and added Unit Test for normalizer

  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 10.2.1 + Environment: PHP 8.1 & MariaDB 10.3.22
    last update 10 months ago
    61 pass
  • Merge request !13Normalizer and test for it β†’ (Merged) created by sickness29
  • Pipeline finished with Skipped
    6 months ago
    #178094
  • Status changed to Fixed 6 months ago
  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024