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

Skip to content

Commit e2c0239

Browse files
committed
bug #27927 [HttpFoundation] Suppress side effects in 'get' and 'has' methods of NamespacedAttributeBag (webnet-fr)
This PR was submitted for the 3.4 branch but it was merged into the 2.8 branch instead (closes #27927). Discussion ---------- [HttpFoundation] Suppress side effects in 'get' and 'has' methods of NamespacedAttributeBag | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #27912 | License | MIT | Doc PR | - As @Gemineye reported there was a bug in `get` and `has` methods of NamespacedAttributeBag. These methods accept composite names as an argument (like 'foo/bar' or 'foo/bar/baz') to reach the elements of stored arrays. Up to now these methods erroneously created entries (`->get('foo/bar')` created `['foo' => null]`, `->get('foo/bar/baz')` created `['foo' => ['bar' => null]]`). Commits ------- 5f59ad4 suppress side effects in 'get' or 'has' methods of NamespacedAttributeBag
2 parents 3b90bc7 + 5f59ad4 commit e2c0239

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

src/Symfony/Component/HttpFoundation/Session/Attribute/NamespacedAttributeBag.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,13 @@ protected function &resolveAttributePath($name, $writeContext = false)
124124

125125
foreach ($parts as $part) {
126126
if (null !== $array && !array_key_exists($part, $array)) {
127-
$array[$part] = $writeContext ? array() : null;
127+
if (!$writeContext) {
128+
$null = null;
129+
130+
return $null;
131+
}
132+
133+
$array[$part] = array();
128134
}
129135

130136
$array = &$array[$part];

src/Symfony/Component/HttpFoundation/Tests/Session/Attribute/NamespacedAttributeBagTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,17 @@ public function testHas($key, $value, $exists)
8282
$this->assertEquals($exists, $this->bag->has($key));
8383
}
8484

85+
/**
86+
* @dataProvider attributesProvider
87+
*/
88+
public function testHasNoSideEffect($key, $value, $expected)
89+
{
90+
$expected = json_encode($this->bag->all());
91+
$this->bag->has($key);
92+
93+
$this->assertEquals($expected, json_encode($this->bag->all()));
94+
}
95+
8596
/**
8697
* @dataProvider attributesProvider
8798
*/
@@ -96,6 +107,17 @@ public function testGetDefaults()
96107
$this->assertEquals('default', $this->bag->get('user2.login', 'default'));
97108
}
98109

110+
/**
111+
* @dataProvider attributesProvider
112+
*/
113+
public function testGetNoSideEffect($key, $value, $expected)
114+
{
115+
$expected = json_encode($this->bag->all());
116+
$this->bag->get($key);
117+
118+
$this->assertEquals($expected, json_encode($this->bag->all()));
119+
}
120+
99121
/**
100122
* @dataProvider attributesProvider
101123
*/

0 commit comments

Comments
 (0)