Skip to content

Use double \ instead of four \ in expression service #12513

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
Nov 1, 2019

Conversation

jbdelhommeau
Copy link
Contributor

No description provided.

Copy link
Member

@wouterj wouterj left a comment

Choose a reason for hiding this comment

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

I believe this is wrong, see https://symfony.com/doc/current/components/expression_language/syntax.html#supported-literals :

A backslash (\) must be escaped by 4 backslashes (\\\\) in a string and 8 backslashes (\\\\\\\\) in a regex:

The reason is that one backslash (leaving \\) is removed by PHP and the expression language needs another escape of the backslash.

Exactly this line was changed in #8347

However, I'm sure you didn't put time into submitting a fix without a reason: So did you test this and came to the conclusion that 2 backslashes were enough? Because then we have to reconsider the ExpressionLanguage documentation :)

@jbdelhommeau
Copy link
Contributor Author

jbdelhommeau commented Oct 18, 2019 via email

@OskarStark
Copy link
Contributor

My first thought was, it was wrong, but we only need 4 when using it as regex pattern AFAIK.
I didn’t test this but, 2 looks ok to me. Anyone else who can confirm this?

@nicolas-grekas can you please give your 2 cents here please? Thanks 🙏

@nicolas-grekas
Copy link
Member

This might work :)

@OskarStark OskarStark changed the title User double \ instead of four \ in expression service Use double \ instead of four \ in expression service Oct 18, 2019
@wouterj
Copy link
Member

wouterj commented Oct 18, 2019

So I ran a small test:

expr.yaml

argument: "'App\\Mail\\MailerConfiguration'"

expr.php

<?php

require 'vendor/autoload.php';

use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
use Symfony\Component\Yaml\Yaml;

$lang = new ExpressionLanguage();
$file = Yaml::parseFile('expr.yaml');

var_dump($lang->evaluate($file['argument']));
var_dump($lang->compile($file['argument']))

And this produces:

$ php expr.php
string(26) "AppMailMailerConfiguration"
string(28) ""AppMailMailerConfiguration""

So as far as I can see, we indeed do need the 4 backslashes in the configuration (fyi, if I test without the Yaml and with normal PHP strings, we also need 4 backslashes). So I'm going to close for now, but feel free to comment and share your insights @jbdelhommeau! And as always, thanks for taking the time to fix documentation!

@wouterj wouterj closed this Oct 18, 2019
@jbdelhommeau
Copy link
Contributor Author

jbdelhommeau commented Oct 19, 2019

Okay I found the problem ! You're right but so am I.

Show this small test :

expr.yaml

argument_1: '"App\\Mail\\MailerConfiguration"'                                  
argument_2: "'App\\Mail\\MailerConfiguration'"  
<?php
require 'vendor/autoload.php';

use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
use Symfony\Component\Yaml\Yaml;

$lang = new ExpressionLanguage();
$file = Yaml::parseFile('expr.yaml');

var_dump($lang->evaluate($file['argument_1']));
var_dump($lang->compile($file['argument_1']));

var_dump($lang->evaluate($file['argument_2']));
var_dump($lang->compile($file['argument_2']));

Tada and the result are :

$ php expr.php 
string(28) "App\Mail\MailerConfiguration"
string(32) ""App\\Mail\\MailerConfiguration""
string(26) "AppMailMailerConfiguration"
string(28) ""AppMailMailerConfiguration""

In Yaml quote or double quote on declaration of the expression alter expression parsing...

Stranger no ?

In my case, and I don't grant it to you in the documentation, I use a simple quote to declare my expression.

Wouldn't that still deserve a little note in the documentation?

@wouterj
Copy link
Member

wouterj commented Oct 19, 2019

Thanks for coming back to this! This is starting to blow my mind 😄 . First, let's reopen this PR as I think this is worth changing/clarifying.

I extended your script a bit more:

Raw YAML value dump
'App\Mail\MailerConfiguration'
"App\\Mail\\MailerConfiguration"


YAML	"'...'"
AppMailMailerConfiguration
"AppMailMailerConfiguration"

YAML	'"..."'
App\Mail\MailerConfiguration
"App\\Mail\\MailerConfiguration"

PHP	"'...'"
AppMailMailerConfiguration
"AppMailMailerConfiguration"

PHP	'"..."'
AppMailMailerConfiguration
"AppMailMailerConfiguration"

For some reason, double backslashes only work with Yaml '"..."' quotes. @xabbuh as you're the Yaml hero, can you shed some light on this?

I always favor single quotes in Yaml (as yaml double quotes are magic), so should we use double backslashes here with '"..."' and add a line comment about it in the code example? And then, what does this mean for the XML and PHP examples? I think they should remain 4 backslashes?

@xabbuh
Copy link
Member

xabbuh commented Oct 30, 2019

This looks expected to me. Double-quoted strings in YAML support escape sequences. This requires us to escape backslashes that are not part of escape sequences to be doubled which is not the case in single-quoted strings:

The double-quoted style is specified by surrounding “"” indicators. This is the only style capable of expressing arbitrary strings, by using “\” escape sequences. This comes at the cost of having to escape the “\” and “"” characters.

see https://yaml.org/spec/1.2/spec.html#id2787109

@xabbuh
Copy link
Member

xabbuh commented Oct 30, 2019

So, what I would do here is to add a comment that explains that you need to escape backslashes when using double-quoted strings.

On a side note, I personally prefer single-quoted strings to not have to think about things like this.

@jbdelhommeau
Copy link
Contributor Author

I added a comment in yaml to explain the usage with the double quote.

Does this change seem good for you ?

Copy link
Member

@wouterj wouterj left a comment

Choose a reason for hiding this comment

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

Looks good!

@OskarStark
Copy link
Contributor

Thank you for your patience! 🙏

OskarStark added a commit that referenced this pull request Nov 1, 2019
…delhommeau)

This PR was merged into the 4.3 branch.

Discussion
----------

Use double \ instead of four \ in expression service

<!--

If your pull request fixes a BUG, use the oldest maintained branch that contains
the bug (see https://symfony.com/roadmap for the list of maintained branches).

If your pull request documents a NEW FEATURE, use the same Symfony branch where
the feature was introduced (and `master` for features of unreleased versions).

-->

Commits
-------

6c60285 User double \ instead of four \ in expression service
@OskarStark OskarStark merged commit 6c60285 into symfony:4.3 Nov 1, 2019
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.

7 participants