Refactor URL parameter replacement logic in OpenAPI3 and Swagger2 to ensure accurate matching of path parameters by checking for trailing characters.

This commit is contained in:
ArnabChatterjee20k
2026-05-08 14:19:51 +05:30
parent 5cfaa0807d
commit 07973dee2d
2 changed files with 28 additions and 2 deletions
@@ -814,7 +814,20 @@ class OpenAPI3 extends Format
}
foreach ($pathAliases as $pathAlias) {
$url = \str_replace(':' . $pathAlias, '{' . $name . '}', $url);
$pathNeedle = ':' . $pathAlias;
$replacement = '{' . $name . '}';
$offset = 0;
while (false !== ($position = \strpos($url, $pathNeedle, $offset))) {
$nextChar = $url[$position + \strlen($pathNeedle)] ?? '';
if ($nextChar === '' || $nextChar === '/') {
$url = \substr($url, 0, $position) . $replacement . \substr($url, $position + \strlen($pathNeedle));
$offset = $position + \strlen($replacement);
continue;
}
$offset = $position + 1;
}
}
}
@@ -785,7 +785,20 @@ class Swagger2 extends Format
}
foreach ($pathAliases as $pathAlias) {
$url = \str_replace(':' . $pathAlias, '{' . $name . '}', $url);
$pathNeedle = ':' . $pathAlias;
$replacement = '{' . $name . '}';
$offset = 0;
while (false !== ($position = \strpos($url, $pathNeedle, $offset))) {
$nextChar = $url[$position + \strlen($pathNeedle)] ?? '';
if ($nextChar === '' || $nextChar === '/') {
$url = \substr($url, 0, $position) . $replacement . \substr($url, $position + \strlen($pathNeedle));
$offset = $position + \strlen($replacement);
continue;
}
$offset = $position + 1;
}
}
}