Skip to content

[Yaml] deprecate the !str tag #23288

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

Merged
merged 1 commit into from
Jul 3, 2017
Merged
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
2 changes: 2 additions & 0 deletions UPGRADE-3.4.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,7 @@ Validator
Yaml
----

* Support for the `!str` tag is deprecated, use the `!!str` tag instead.

* Using the non-specific tag `!` is deprecated and will have a different
behavior in 4.0. Use a plain integer or `!!float` instead.
2 changes: 2 additions & 0 deletions UPGRADE-4.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,8 @@ Workflow
Yaml
----

* Support for the `!str` tag was removed, use the `!!str` tag instead.

* Starting an unquoted string with a question mark followed by a space
throws a `ParseException`.

Expand Down
2 changes: 2 additions & 0 deletions src/Symfony/Component/Yaml/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ CHANGELOG
3.4.0
-----

* Support for the `!str` tag is deprecated, use the `!!str` tag instead.

* Deprecated using the non-specific tag `!` as its behavior will change in 4.0.
It will force non-evaluating your values in 4.0. Use plain integers or `!!float` instead.

Expand Down
4 changes: 4 additions & 0 deletions src/Symfony/Component/Yaml/Inline.php
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,11 @@ private static function evaluateScalar($scalar, $flags, $references = array())
case $scalar[0] === '!':
switch (true) {
case 0 === strpos($scalar, '!str'):
@trigger_error('Support for the !str tag is deprecated since version 3.4. Use the !!str tag instead.', E_USER_DEPRECATED);

return (string) substr($scalar, 5);
case 0 === strpos($scalar, '!!str '):
return (string) substr($scalar, 6);
Copy link
Contributor

Choose a reason for hiding this comment

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

While doing this, can't we use parseScalar here?

Copy link
Member

Choose a reason for hiding this comment

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

Maybe it's better to do that in a separate PR to not mix too many things in the same PR.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok, fine for me :)

Copy link
Member

Choose a reason for hiding this comment

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

if we use parseScalar, we may end up parsing the value as a YAML timestamp for instance, which will break the usage of the tag (see not-date: !!str 2002-04-28 in the tests for instance)

Copy link
Contributor

Choose a reason for hiding this comment

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

Fortunately parseScalar has a $evaluate argument :)

case 0 === strpos($scalar, '! '):
@trigger_error('Using the non-specific tag "!" is deprecated since version 3.4 as its behavior will change in 4.0. It will force non-evaluating your values in 4.0. Use plain integers or !!float instead.', E_USER_DEPRECATED);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ test: Various explicit families
todo: true
spec: 2.23
yaml: |
not-date: !str 2002-04-28
not-date: !!str 2002-04-28
picture: !binary |
R0lGODlhDAAMAIQAAP//9/X
17unp5WZmZgAAAOfn515eXv
Expand Down Expand Up @@ -932,7 +932,7 @@ deprecated: Using the non-specific tag "!" is deprecated since version 3.4 as it
yaml: |
integer: 12
also int: ! "12"
string: !str 12
string: !!str 12
php: |
array( 'integer' => 12, 'also int' => 12, 'string' => '12' )
---
Expand Down Expand Up @@ -964,7 +964,7 @@ documents: 2
test: Type family under yaml.org
yaml: |
# The URI is 'tag:yaml.org,2002:str'
- !str a Unicode string
- !!str a Unicode string
php: |
array( 'a Unicode string' )
---
Expand Down Expand Up @@ -1351,7 +1351,7 @@ yaml: |

second: 12 ## This is an integer.

third: !str 12 ## This is a string.
third: !!str 12 ## This is a string.

span: this contains
six spaces
Expand Down Expand Up @@ -1420,7 +1420,7 @@ yaml: |
# The following scalars
# are loaded to the
# string value '1' '2'.
- !str 12
- !!str 12
- '12'
- "12"
- "\
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ php: |
test: Forcing Strings
brief: >
Any YAML type can be forced into a string using the
explicit !str method.
explicit !!str method.
yaml: |
date string: !str 2001-08-01
number string: !str 192
date string: !!str 2001-08-01
number string: !!str 192
php: |
array(
'date string' => '2001-08-01',
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 @@ -755,4 +755,13 @@ public function getNotPhpCompatibleMappingKeyData()
'float' => array('{0.25: "foo"}', array('0.25' => 'foo')),
);
}

/**
* @group legacy
* @expectedDeprecation Support for the !str tag is deprecated since version 3.4. Use the !!str tag instead.
*/
public function testDeprecatedStrTag()
{
$this->assertSame(array('foo' => 'bar'), Inline::parse('{ foo: !str bar }'));
}
}