Skip to content

Commit 5ba8bd6

Browse files
committed
merged branch MaxVandervelde/fix/namespaced-parameter-issue (PR #7586)
This PR was merged into the 2.1 branch. Discussion ---------- [HttpFoundation] Fixed bug in key searching for NamespacedAttributeBag | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #7564 | License | MIT | Doc PR | N/A Fixed a bug in NamespacedAttributeBag causing a result to be falsely found when the last key of the attribute matched the last of the queried name regardless of if the key did not exist in the search. Added Tests to demonstrate the issue and resolved by setting keys to null when iterating through query and returning proper responses in the case that the given array does in fact not exist. Commits ------- 0f0c29c [HttpFoundation] Fixed bug in key searching for NamespacedAttributeBag
2 parents 4dfcbb4 + a7b8d8f commit 5ba8bd6

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

Session/Attribute/NamespacedAttributeBag.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ public function has($name)
4646
$attributes = $this->resolveAttributePath($name);
4747
$name = $this->resolveKey($name);
4848

49+
if (null === $attributes) {
50+
return false;
51+
}
52+
4953
return array_key_exists($name, $attributes);
5054
}
5155

@@ -57,6 +61,10 @@ public function get($name, $default = null)
5761
$attributes = $this->resolveAttributePath($name);
5862
$name = $this->resolveKey($name);
5963

64+
if (null === $attributes) {
65+
return $default;
66+
}
67+
6068
return array_key_exists($name, $attributes) ? $attributes[$name] : $default;
6169
}
6270

@@ -120,12 +128,8 @@ protected function &resolveAttributePath($name, $writeContext = false)
120128
unset($parts[count($parts)-1]);
121129

122130
foreach ($parts as $part) {
123-
if (!array_key_exists($part, $array)) {
124-
if (!$writeContext) {
125-
return $array;
126-
}
127-
128-
$array[$part] = array();
131+
if (null !== $array && !array_key_exists($part, $array)) {
132+
$array[$part] = $writeContext ? array() : null;
129133
}
130134

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

Tests/Session/Attribute/NamespacedAttributeBagTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,10 @@ public function attributesProvider()
160160
array('csrf.token/b', '4321', true),
161161
array('category', array('fishing' => array('first' => 'cod', 'second' => 'sole')), true),
162162
array('category/fishing', array('first' => 'cod', 'second' => 'sole'), true),
163+
array('category/fishing/missing/first', null, false),
163164
array('category/fishing/first', 'cod', true),
164165
array('category/fishing/second', 'sole', true),
166+
array('category/fishing/missing/second', null, false),
165167
array('user2.login', null, false),
166168
array('never', null, false),
167169
array('bye', null, false),

0 commit comments

Comments
 (0)