Skip to content

Commit 705e082

Browse files
Aribroscebe
authored andcommitted
Fixed issues with relative path reference parsing
1 parent 2b26ea1 commit 705e082

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

src/ReferenceContext.php

Lines changed: 12 additions & 4 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]);
126-
unset($parts[$i]);
127-
$parentOffset += 2;
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+
unset($parts[$i]);
135+
}
128136
}
129137
}
130138
return '/'.implode('/', $parts);

tests/ReferenceContextTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,10 +183,14 @@ public function normalizeUriProvider()
183183
'/var/www/api/schema/../../definitions.yaml',
184184
'file:///var/www/definitions.yaml',
185185
],
186+
[
187+
'/var/www/api/schema/./../data/./../definitions.yaml',
188+
'file:///var/www/api/definitions.yaml',
189+
],
186190
[
187191
'/var/www/api/schema/./../definitions.yaml',
188192
'file:///var/www/api/definitions.yaml',
189-
],
193+
],
190194
[
191195
'/var/www/api/../definitions.yaml#/components/Pet',
192196
'file:///var/www/definitions.yaml#/components/Pet',

0 commit comments

Comments
 (0)