-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
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
Conversation
There was a problem hiding this 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 :)
I try with my SF 4.3 version and 4 \ erase a exception. With 2 like my
request it's working... Stranger
…On Fri, 18 Oct 2019, 17:58 Wouter J, ***@***.***> wrote:
***@***.**** requested changes on this pull request.
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
<#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 :)
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#12513?email_source=notifications&email_token=AALEW4QDOWLCHRGJG4GQLLTQPHMKTA5CNFSM4JCJBPDKYY3PNVWWK3TUL52HS4DFWFIHK3DMKJSXC5LFON2FEZLWNFSXPKTDN5WW2ZLOORPWSZGOCIPHOGI#pullrequestreview-303986457>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AALEW4UU725HOKLJ4N6WYQ3QPHMKTANCNFSM4JCJBPDA>
.
|
My first thought was, it was wrong, but we only need 4 when using it as regex pattern AFAIK. @nicolas-grekas can you please give your 2 cents here please? Thanks 🙏 |
This might work :) |
So I ran a small test:
argument: "'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']));
var_dump($lang->compile($file['argument'])) And this produces:
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! |
Okay I found the problem ! You're right but so am I. Show this small test :
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 :
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? |
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:
For some reason, double backslashes only work with Yaml I always favor single quotes in Yaml (as yaml double quotes are magic), so should we use double backslashes here with |
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:
|
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. |
I added a comment in yaml to explain the usage with the double quote. Does this change seem good for you ? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good!
Thank you for your patience! 🙏 |
…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
No description provided.