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

Skip to content

[Yaml] parse custom tags for scalars #23301

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 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/Symfony/Component/Yaml/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ CHANGELOG
4.0.0
-----

* Inlined scalars starting with `!` will no longer be parsed as plain strings.
Use the `Yaml::PARSE_CUSTOM_TAGS` flag to parse them as `TaggedValue` objects.
Otherwise, detecting such strings will throw a `ParseException`.
* The behavior of the non-specific tag `!` is changed and now forces
non-evaluating your values.
* complex mappings will throw a `ParseException`
Expand Down
9 changes: 8 additions & 1 deletion src/Symfony/Component/Yaml/Inline.php
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,14 @@ private static function evaluateScalar($scalar, $flags, $references = array())
case 0 === strpos($scalar, '!!binary '):
return self::evaluateBinaryScalar(substr($scalar, 9));
default:
@trigger_error(sprintf('Using the unquoted scalar value "%s" is deprecated since version 3.3 and will be considered as a tagged value in 4.0. You must quote it.', $scalar), E_USER_DEPRECATED);
$tagLength = strcspn($scalar, " \t\n", 1);
$tag = substr($scalar, 1, $tagLength);

if (Yaml::PARSE_CUSTOM_TAGS & $flags) {
return new TaggedValue($tag, substr($scalar, $tagLength + 2));
}

throw new ParseException(sprintf('Tags support is not enabled. You must use the `Yaml::PARSE_CUSTOM_TAGS` flag to use "!%s".', $tag), self::$parsedLineNumber + 1, $scalar);
}

// Optimize for returning strings.
Expand Down
9 changes: 9 additions & 0 deletions src/Symfony/Component/Yaml/Tests/InlineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -703,4 +703,13 @@ public function getNotPhpCompatibleMappingKeyData()
'float' => array('{0.25: "foo"}', array('0.25' => 'foo')),
);
}

/**
* @expectedException \Symfony\Component\Yaml\Exception\ParseException
* @expectedExceptionMessage Tags support is not enabled. You must use the `Yaml::PARSE_CUSTOM_TAGS` flag to use "!iterator" at line 1 (near "!iterator foo").
*/
public function testCustomTagsDisabled()
{
Inline::parse('!iterator foo');
}
}
13 changes: 4 additions & 9 deletions src/Symfony/Component/Yaml/Tests/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1566,6 +1566,10 @@ public function taggedValuesProvider()
- !quz {foo: bar, quz: !bar {one: bar}}
YAML
),
'tagged scalar' => array(
new TaggedValue('iterator', 'foo'),
'!iterator foo',
),
);
}

Expand All @@ -1578,15 +1582,6 @@ public function testCustomTagsDisabled()
$this->parser->parse('!iterator [foo]');
}

/**
* @group legacy
* @expectedDeprecation Using the unquoted scalar value "!iterator foo" is deprecated since version 3.3 and will be considered as a tagged value in 4.0. You must quote it.
*/
public function testUnsupportedTagWithScalar()
{
$this->assertEquals('!iterator foo', $this->parser->parse('!iterator foo'));
}

/**
* @expectedException \Symfony\Component\Yaml\Exception\ParseException
* @expectedExceptionMessage The built-in tag "!!foo" is not implemented.
Expand Down