Thanks to visit codestin.com
Credit goes to github.com

Skip to content

DateIntervalNormalizer is throwing error for valid intervals #35820

Closed
@lucraraujo

Description

@lucraraujo

Symfony version(s) affected: 4.4.4

Description
The DateIntervalNormalizerm throws a validation error while denormalizing a valid Duration string.

How to reproduce

// First, run "composer require symfony/serializer"
// Then, execute this file:
<?php
require_once __DIR__.'/vendor/autoload.php';

use Symfony\Component\Serializer\Normalizer\DateIntervalNormalizer;

$dateIntervalNormalizer = new DateIntervalNormalizer();

$dateIntervalNormalizer->denormalize('PT12H34M', 'DateInterval');

Possible Solution
The validation on line L105 expects that the Duration string have the full duration pattern (eg. 'P1Y1M1DT1H1M1S'), but the DateInterval class accepts a string like 'PT12H34M' (12 hour, 34 minutes) as a valid input.
The ISO8601 says that elements with value equals to zero can be omitted.
Also, while testing, I found that the validation done in line L137 have the same problem for the same reasons explained above.
The other solution would be change the regex to validate the input correctly.
The solution, IMHO, is to create the DateInterval object right at the top of the method, since DateInterval already validates the input in the constructor.

Additional context
PHP: 7.4.2 for Windows
OS: Windows 10 Pro (1909)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions