Skip to content

[BUG] DateTime denormalization fails with elasticsearch provider #3170

Closed
@devmaslov

Description

@devmaslov

Hi,

I've updated Api Platform to the 2.5 version and encountered one issue. I have an entity called User and it's populated into Elasticsearch index with FosElastica bundle. This entity has a DateTime field which is index like this: 2019-09-10T21:56:42+00:00. Elastic support is turned on for this entity and when request is sent I get the following error:

{
  "type": "https://tools.ietf.org/html/rfc2616#section-10",
  "title": "An error occurred",
  "detail": "Cannot create an instance of DateTime from serialized data because its constructor requires parameter \"time\" to be present.",
  "trace": [
    {
      "namespace": "",
      "short_class": "",
      "class": "",
      "type": "",
      "function": "",
      "file": "/var/www/html/vendor/symfony/serializer/Normalizer/AbstractNormalizer.php",
      "line": 505,
      "args": []
    },
    {
      "namespace": "Symfony\\Component\\Serializer\\Normalizer",
      "short_class": "AbstractNormalizer",
      "class": "Symfony\\Component\\Serializer\\Normalizer\\AbstractNormalizer",
      "type": "->",
      "function": "instantiateObject",
      "file": "/var/www/html/vendor/symfony/serializer/Normalizer/AbstractObjectNormalizer.php",
      "line": 231,
      "args": [
        [
          "array",
          [
            [
              "string",
              "2019-09-10T21:56:38+00:00"
            ]
          ]
        ],
        [
          "string",
          "DateTime"
        ],
        [
          "array",
          {
            "allow_extra_attributes": [
              "boolean",
              true
            ],
            "cache_key": [
              "string",
              "49699be3ed7cab8b271ee7c0939c6d8e"
            ]
          }
        ],
        [
          "object",
          "ReflectionClass"
        ],
        [
          "boolean",
          false
        ],
        [
          "string",
          "elasticsearch"
        ]
      ]
    },
    {
      "namespace": "Symfony\\Component\\Serializer\\Normalizer",
      "short_class": "AbstractObjectNormalizer",
      "class": "Symfony\\Component\\Serializer\\Normalizer\\AbstractObjectNormalizer",
      "type": "->",
      "function": "instantiateObject",
      "file": "/var/www/html/vendor/symfony/serializer/Normalizer/AbstractObjectNormalizer.php",
      "line": 330,
      "args": [
        [
          "array",
          [
            [
              "string",
              "2019-09-10T21:56:38+00:00"
            ]
          ]
        ],
        [
          "string",
          "DateTime"
        ],
        [
          "array",
          {
            "allow_extra_attributes": [
              "boolean",
              true
            ],
            "cache_key": [
              "string",
              "49699be3ed7cab8b271ee7c0939c6d8e"
            ]
          }
        ],
        [
          "object",
          "ReflectionClass"
        ],
        [
          "boolean",
          false
        ],
        [
          "string",
          "elasticsearch"
        ]
      ]
    },
    {
      "namespace": "Symfony\\Component\\Serializer\\Normalizer",
      "short_class": "AbstractObjectNormalizer",
      "class": "Symfony\\Component\\Serializer\\Normalizer\\AbstractObjectNormalizer",
      "type": "->",
      "function": "denormalize",
      "file": "/var/www/html/vendor/api-platform/core/src/Bridge/Elasticsearch/Serializer/ItemNormalizer.php",
      "line": 62,
      "args": [
        [
          "string",
          "2019-09-10T21:56:38+00:00"
        ],
        [
          "string",
          "DateTime"
        ],
        [
          "string",
          "elasticsearch"
        ],
        [
          "array",
          {
            "allow_extra_attributes": [
              "boolean",
              true
            ],
            "cache_key": [
              "string",
              "49699be3ed7cab8b271ee7c0939c6d8e"
            ]
          }
        ]
      ]
    },
    {
      "namespace": "ApiPlatform\\Core\\Bridge\\Elasticsearch\\Serializer",
      "short_class": "ItemNormalizer",
      "class": "ApiPlatform\\Core\\Bridge\\Elasticsearch\\Serializer\\ItemNormalizer",
      "type": "->",
      "function": "denormalize",
      "file": "/var/www/html/vendor/symfony/serializer/Serializer.php",
      "line": 191,
      "args": [
        [
          "string",
          "2019-09-10T21:56:38+00:00"
        ],
        [
          "string",
          "DateTime"
        ],
        [
          "string",
          "elasticsearch"
        ],
        [
          "array",
          {
            "allow_extra_attributes": [
              "boolean",
              true
            ],
            "cache_key": [
              "string",
              "49699be3ed7cab8b271ee7c0939c6d8e"
            ]
          }
        ]
      ]
    }
...

I figured it has something to do with this pull request #2921. It changed the priority for api_platform.elasticsearch.normalizer.item service to -890 which is higher then Symfony's serializer.normalizer.datetime (-910). So elasticsearch item normalizer tries to denormalize date time object now and fails.

I don't think api_platform.elasticsearch.normalizer.item should support DateTime serialization at all.

For now as a workaround I just lowered the priority for date time normalizer in my services.yml to fix the issue:

serializer.normalizer.datetime:
    class: Symfony\Component\Serializer\Normalizer\DateTimeNormalizer
    tags:
        - { name: serializer.normalizer,  priority: -889 }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions