Skip to content

[Serializer] Do not throw exception in the DateTimeNormalizer if it's not a date #27824

@lyrixx

Description

@lyrixx

Description
ATM, if the client send a malformated date (or something else) in a field mapped as DateTime, an exception occurs.

Symfony\Component\Serializer\Exception\NotNormalizableValueException:
DateTimeImmutable::__construct(): Failed to parse time string (2018-08-0111111) at position 14 (1): Unexpected character

  at vendor/symfony/symfony/src/Symfony/Component/Serializer/Normalizer/DateTimeNormalizer.php:116
  at Symfony\Component\Serializer\Normalizer\DateTimeNormalizer->denormalize('2018-08-0111111', 'DateTimeImmutable', 'json', array('cache_key' => '6de9eb2685609b8387658584987a05b0'))
     (vendor/symfony/symfony/src/Symfony/Component/Serializer/Serializer.php:182)
  at Symfony\Component\Serializer\Serializer->denormalize('2018-08-0111111', 'DateTimeImmutable', 'json', array('cache_key' => '6de9eb2685609b8387658584987a05b0'))

# ...

By default this Exception is not catched and leads to a 500
Even If we put some validation, the validation comes after ... So this is not related

Example
IMHO, it would be better to not throw an exception, then the validator (if exist) will add a violation on the property


Maybe this idea could be extended to others normalizer


ping @dunglas

Metadata

Metadata

Assignees

No one assigned

    Labels

    RFCRFC = Request For Comments (proposals about features that you want to be discussed)Serializer

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions