From d303d6f807dd25d400afd9258d779aa84aec4cae Mon Sep 17 00:00:00 2001 From: ArnabChatterjee20k Date: Fri, 8 May 2026 14:39:26 +0530 Subject: [PATCH] Refactor path parameter detection in OpenAPI3 and Swagger2 by utilizing array flipping for improved performance and clarity in matching aliases. --- .../SDK/Specification/Format/OpenAPI3.php | 16 +++++----------- .../SDK/Specification/Format/Swagger2.php | 16 +++++----------- 2 files changed, 10 insertions(+), 22 deletions(-) diff --git a/src/Appwrite/SDK/Specification/Format/OpenAPI3.php b/src/Appwrite/SDK/Specification/Format/OpenAPI3.php index 004be99c5b..4f925b1811 100644 --- a/src/Appwrite/SDK/Specification/Format/OpenAPI3.php +++ b/src/Appwrite/SDK/Specification/Format/OpenAPI3.php @@ -756,19 +756,13 @@ class OpenAPI3 extends Format } $pathAliases = [$name, ...($param['aliases'] ?? [])]; + $pathAliasMap = \array_flip($pathAliases); $isPathParam = false; - foreach ($pathAliases as $pathAlias) { - $pathNeedle = ':' . $pathAlias; - $offset = 0; - while (false !== ($position = \strpos($url, $pathNeedle, $offset))) { - $nextChar = $url[$position + \strlen($pathNeedle)] ?? ''; - if ($nextChar === '' || $nextChar === '/') { - $isPathParam = true; - break 2; - } - - $offset = $position + 1; + foreach (\explode('/', $url) as $segment) { + if ($segment !== '' && $segment[0] === ':' && isset($pathAliasMap[\substr($segment, 1)])) { + $isPathParam = true; + break; } } diff --git a/src/Appwrite/SDK/Specification/Format/Swagger2.php b/src/Appwrite/SDK/Specification/Format/Swagger2.php index 2d7965df6a..c352154006 100644 --- a/src/Appwrite/SDK/Specification/Format/Swagger2.php +++ b/src/Appwrite/SDK/Specification/Format/Swagger2.php @@ -723,19 +723,13 @@ class Swagger2 extends Format } $pathAliases = [$name, ...($param['aliases'] ?? [])]; + $pathAliasMap = \array_flip($pathAliases); $isPathParam = false; - foreach ($pathAliases as $pathAlias) { - $pathNeedle = ':' . $pathAlias; - $offset = 0; - while (false !== ($position = \strpos($url, $pathNeedle, $offset))) { - $nextChar = $url[$position + \strlen($pathNeedle)] ?? ''; - if ($nextChar === '' || $nextChar === '/') { - $isPathParam = true; - break 2; - } - - $offset = $position + 1; + foreach (\explode('/', $url) as $segment) { + if ($segment !== '' && $segment[0] === ':' && isset($pathAliasMap[\substr($segment, 1)])) { + $isPathParam = true; + break; } }