Skip to content

[YAML] Fixed parsing problem with nested DateTime lists #19029

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed

[YAML] Fixed parsing problem with nested DateTime lists #19029

wants to merge 2 commits into from

Conversation

jkphl
Copy link
Contributor

@jkphl jkphl commented Jun 11, 2016

Q A
Branch? master
Bug fix? no
New feature? no
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets
License MIT
Doc PR

Without this fix, DateTime-aware parsing of a YAML source containing nested lists of dates result in an error. Consider this:

$data = ['date' => ['annivesary' => new \DateTime('now')]];
$yaml = Yaml::dump($data);
var_dump($yaml);

$parsed = Yaml::parse($yaml, Yaml::PARSE_DATETIME);
print_r($parsed);

Everything is fine, result is:

string(48) "date:
    annivesary: 2016-06-11T11:26:30+02:00
"
Array
(
    [date] => Array
        (
            [annivesary] => DateTime Object
                (
                    [date] => 2016-06-11 11:26:30.000000
                    [timezone_type] => 1
                    [timezone] => +02:00
                )

        )

)

But making the anniversary a list of dates

$data = ['date' => ['annivesary' => [new \DateTime('now')]]];
$yaml = Yaml::dump($data);
var_dump($yaml);

$parsed = Yaml::parse($yaml, Yaml::PARSE_DATETIME);
print_r($parsed);

will result in:

string(50) "date:
    annivesary: [2016-06-11T12:00:05+02:00]
"
PHP Warning:  strpos() expects parameter 1 to be string, object given in [...]\vendor\symfony\yaml\Inline.php on line 382
PHP Catchable fatal error:  Object of class DateTime could not be converted to string in [...]\vendor\symfony\yaml\Inline.php on line 386

(I didn't capture the error messages with the most recent master branch, so line numbers differ somewhat)

@@ -398,7 +398,7 @@ private static function parseSequence($sequence, $flags, &$i = 0, $references =
$value = self::parseScalar($sequence, $flags, array(',', ']'), array('"', "'"), $i, true, $references);

// the value can be an array if a reference has been resolved to an array var
if (!is_array($value) && !$isQuoted && false !== strpos($value, ': ')) {
if (!is_array($value) && !$value instanceof \DateTimeInterface && !$isQuoted && false !== strpos($value, ': ')) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about using !is_object() here instead? That would also cover the case when an unserialized object was returned before.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That might also do the job, of course, but I don't really overlook the consequences. Running strpos on an object will most likely fail in most of the situations, so it's probably reasonable, yes. But as I said, I don't overlook every aspect — are there any kinds of serializable objects allowed? — so I wanted to keep it as specific as possible. Feel free to loosen this in case is_object() doesn't break anything I don't see ...

@xabbuh
Copy link
Member

xabbuh commented Jun 11, 2016

Can you please add a test for this to avoid regressions?

Status: Needs work

@fabpot
Copy link
Member

fabpot commented Jun 16, 2016

Thank you @jkphl.

@fabpot fabpot closed this in 6f05632 Jun 16, 2016
fabpot added a commit that referenced this pull request Jun 17, 2016
…kphl, xabbuh)

This PR was merged into the 3.1 branch.

Discussion
----------

[YAML] Fixed parsing problem with nested DateTime lists

| Q             | A
| ------------- | ---
| Branch?       | 3.1
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #19029
| License       | MIT
| Doc PR        |

The new handling for `DateTimeInterface` instances was introduced in Symfony 3.1.

Commits
-------

0f47712 parse embedded mappings only if value is a string
4f13a76 [YAML] Fixed parsing problem with nested DateTime lists
@fabpot fabpot mentioned this pull request Oct 27, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants