Subject: | [PATCH] Better whitespace and quote handling in attribute parser |
The attribute params parser in ActionDispatch::Attributes is pretty
strict, and doesn't accept any variation from the syntax shown in the
docs.
This means no whitespace and no double quotes, both of which are fairly
standard syntax choices.
So, this works:
sub foo : Path('/foo') {}
But these do not:
sub foo : Path("foo") {}
sub foo : Path( '/foo' ) {}
sub foo : Path( "/foo" ) {}
When you stray from the expected syntax, the attribute is silently
thrown out and you are left scratching your head as to why a match
isn't being made.
Attached is a patch for more flexible attribute parsing, shamelessly
stolen from Catalyst's "_parse_attrs" method.
@@ -20,8 +20,11 @@
foreach (@attrs) {
# Parse the attribute string ex: Regex('^/foo/bar/(\d+)/').
- my($method, $params) = /^([a-z_]\w*)(?:[(](.*)[)])?$/is;
- $params =~ s/(^'|'$)//g if defined $params;
+ my($method, $params) = /^(.*?)(?:\(\s*(.+?)\s*\))?$/;
+
+ if ( defined $params ) {
+ ( $params =~ s/^'(.*)'$/$1/ ) || ( $params =~ s/^"(.*)"/$1/ )
+ }
I've also attatched new tests to verify the above patch.