Skip to content

Commit 3bb2be2

Browse files
authored
Merge pull request #191 from Aribros/reduce-dots-current-dir
Reduce dots current dir
2 parents 69f6f85 + 159cbf6 commit 3bb2be2

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

src/ReferenceContext.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,18 @@ private function reduceDots($path)
121121
unset($parts[$i]);
122122
continue;
123123
}
124-
if ($i > 0 && $parts[$i] === '..' && $parts[$i - $parentOffset] !== '..') {
125-
unset($parts[$i - $parentOffset]);
124+
125+
if ($i > 0 && $parts[$i] === '..') {
126+
$parent = $i - $parentOffset;
127+
//Make sure parent exists, if not, check the next parent etc
128+
while($parent >= 0 && empty($parts[$parent])){
129+
$parent--;
130+
}
131+
//Confirm parent is valid
132+
if(!empty($parts[$parent]) && $parts[$parent] !== '..'){
133+
unset($parts[$parent]);
134+
}
126135
unset($parts[$i]);
127-
$parentOffset += 2;
128136
}
129137
}
130138
return '/'.implode('/', $parts);

tests/ReferenceContextTest.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,14 +179,22 @@ public function normalizeUriProvider()
179179
'/var/www/api/../definitions.yaml',
180180
'file:///var/www/definitions.yaml',
181181
],
182+
[
183+
'/./definitions.yaml',
184+
'file:///definitions.yaml',
185+
],
182186
[
183187
'/var/www/api/schema/../../definitions.yaml',
184188
'file:///var/www/definitions.yaml',
185189
],
190+
[
191+
'/var/www/api/schema/./../data/./../definitions.yaml',
192+
'file:///var/www/api/definitions.yaml',
193+
],
186194
[
187195
'/var/www/api/schema/./../definitions.yaml',
188196
'file:///var/www/api/definitions.yaml',
189-
],
197+
],
190198
[
191199
'/var/www/api/../definitions.yaml#/components/Pet',
192200
'file:///var/www/definitions.yaml#/components/Pet',

0 commit comments

Comments
 (0)