Symfony serializer cannot deserialize into DateTime

I‘m trying to deserialize a json with the Symfony Serializer into a DTO, but I got always the exception Symfony/Component/Serializer/Exception/MissingConstructorArgumentsException with message 'Cannot create an instance of DateTime from serialized data because its constructor requires parameter "time" to be present.'.

My DTO looks like this:

class ResponseDto
{
    /** @var DateTime */
    private $creationDate;


    /** @var NestedObject */
    private $result;


    public function __construct(
        DateTime $creationDate,
        NestedObject $result
    ) { /* … */ }

    public function getCreationDate(): DateTime
    {
        return $this->creationDate;
    }

    public function getResult(): NestedObject
    {
        return $this->result;
    }
}

with a json like this:

[
  {
    "result":{
      "operations":{
        "successful":319698,
        "failed":2,
        "total":319700
      }
    },
    "creationDate":"2020-02-10T03:01:53.357Z"
  },
  // …
]

The deserialize call is also straight forward:

$serializer->deserialize(
    $data,
    ResponseDto::class . '[]',
    'json'
);

And all necessary normalizer (and the symfony/property-info package) are installed/registered too:

$ bin/console debug:container serializer --show-arguments

Information for Service "serializer"
====================================

 Serializer serializes and deserializes data.

 ---------------- ----------------------------------------- 
  Option           Value                                    
 ---------------- ----------------------------------------- 
  Service ID       serializer                               
  Class            SymfonyComponentSerializerSerializer  
  Tags             -                                        
  Public           yes                                      
  Synthetic        no                                       
  Lazy             no                                       
  Shared           yes                                      
  Abstract         no                                       
  Autowired        no                                       
  Autoconfigured   no                                       
  Arguments        Array (9 element(s))                     
                   Array (5 element(s))                     
 ---------------- ----------------------------------------- 


$ bin/console debug:container --tag serializer.normalizer

Symfony Container Services Tagged with "serializer.normalizer" Tag
==================================================================

 ------------------------------------------------- ---------- --------------------------------------------------------------------------- 
  Service ID                                        priority   Class name                                                                 
 ------------------------------------------------- ---------- --------------------------------------------------------------------------- 
  fos_rest.serializer.form_error_normalizer         -10        FOSRestBundleSerializerNormalizerFormErrorNormalizer                   
  reporting.reader.serializer                                  SymfonyComponentSerializerSerializer                                    
  serializer.denormalizer.array                     -990       SymfonyComponentSerializerNormalizerArrayDenormalizer                  
  serializer.normalizer.constraint_violation_list   -915       SymfonyComponentSerializerNormalizerConstraintViolationListNormalizer  
  serializer.normalizer.data_uri                    -920       SymfonyComponentSerializerNormalizerDataUriNormalizer                  
  serializer.normalizer.dateinterval                -915       SymfonyComponentSerializerNormalizerDateIntervalNormalizer             
  serializer.normalizer.datetime                    -910       SymfonyComponentSerializerNormalizerDateTimeNormalizer                 
  serializer.normalizer.json_serializable           -900       SymfonyComponentSerializerNormalizerJsonSerializableNormalizer         
  serializer.normalizer.object                      -1000      SymfonyComponentSerializerNormalizerObjectNormalizer                   
 ------------------------------------------------- ---------- --------------------------------------------------------------------------- 

Why is the serializer not able to deserialize the data into a DateTime object? The DateTimeNormalizer::supportsDenormalization() is also called and returning true for the property, but DateTimeNormalizer::denormalize() is never called.

Fun fact: If I change the type hint to DateTimeInterface the serializer can deserialize the data, but produces DateTimeImmutable (unfortunately I need DateTime objects).

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *