Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

route helper bug fix

  • Loading branch information...
commit bcbbefaa5f1f1260f0826723c1de6c35dde05ac5 1 parent 245a23f
Mashape authored
Showing with 40 additions and 20 deletions.
  1. +1 −1  api.xml
  2. +39 −19 mashape/methods/call/helpers/routeHelper.php
View
2  api.xml
@@ -4,5 +4,5 @@
<method http="GET" name="sayHello" >
<result type="simple" name="helloWorldMessage" />
</method>
-
+
</api>
View
58 mashape/methods/call/helpers/routeHelper.php
@@ -34,7 +34,7 @@ function findRoute($requestUri, &$routeParameters, $httpRequestMethod, $serverKe
$configuration = RESTConfigurationLoader::loadConfiguration($serverKey);
$methods = $configuration->getMethods();
-
+
for ($i=0;$i<count($methods);$i++) {
if ($methods[$i]->getHttp() != $httpRequestMethod) {
unset($methods[$i]);
@@ -57,25 +57,27 @@ function findRoute($requestUri, &$routeParameters, $httpRequestMethod, $serverKe
$route = $method->getRoute();
if (!empty($route)) {
$routeParts = Explode("/", substr($route, 1));
- $backwardIndex = count($routeParts) - (count($requestUriParts) - $i);
- if ($backwardIndex >= 0) {
- if ($routeParts[$backwardIndex] == $requestUriParts[$i]) {
- if (!ArrayUtils::existKey($methodName, $likelyMethods)) {
- $likelyMethods[$methodName] = array();
- }
- } else if (RouteUtils::isRoutePlaceholder($routeParts[$backwardIndex])) {
- $foundParameters;
- $placeHolder = RouteUtils::getRoutePlaceholder($routeParts[$backwardIndex]);
- if (!ArrayUtils::existKey($methodName, $likelyMethods)) {
- $foundParameters = array();
+ if (count($routeParts) <= count($requestUriParts)) {
+ $backwardIndex = count($routeParts) - (count($requestUriParts) - $i);
+ if ($backwardIndex >= 0) {
+ if ($routeParts[$backwardIndex] == $requestUriParts[$i]) {
+ if (!ArrayUtils::existKey($methodName, $likelyMethods)) {
+ $likelyMethods[$methodName] = array();
+ }
+ } else if (RouteUtils::isRoutePlaceholder($routeParts[$backwardIndex])) {
+ $foundParameters;
+ $placeHolder = RouteUtils::getRoutePlaceholder($routeParts[$backwardIndex]);
+ if (!ArrayUtils::existKey($methodName, $likelyMethods)) {
+ $foundParameters = array();
+ } else {
+ $foundParameters = $likelyMethods[$methodName];
+ }
+ $foundParameters[$placeHolder] = $requestUriParts[$i];
+ $likelyMethods[$methodName] = $foundParameters;
} else {
- $foundParameters = $likelyMethods[$methodName];
+ array_push($excludedMethods, $methodName);
+ unset($likelyMethods[$methodName]);
}
- $foundParameters[$placeHolder] = $requestUriParts[$i];
- $likelyMethods[$methodName] = $foundParameters;
- } else {
- array_push($excludedMethods, $methodName);
- unset($likelyMethods[$methodName]);
}
}
}
@@ -83,6 +85,25 @@ function findRoute($requestUri, &$routeParameters, $httpRequestMethod, $serverKe
}
if (count($likelyMethods) > 1) {
+ // Get the route with the highest matches
+ $maxRouteSize = 0;
+ foreach ($likelyMethods as $key => $value) {
+ $route = RESTConfigurationLoader::getMethod($key, $serverKey)->getRoute();
+ $routeSize = count(Explode("/", substr($route, 1)));
+ if ($routeSize > $maxRouteSize) {
+ $maxRouteSize = $routeSize;
+ }
+ }
+ foreach ($likelyMethods as $key => $value) {
+ $route = RESTConfigurationLoader::getMethod($key, $serverKey)->getRoute();
+ $routeSize = count(Explode("/", substr($route, 1)));
+ if ($routeSize < $maxRouteSize) {
+ unset($likelyMethods[$key]);
+ }
+ }
+ }
+
+ if (count($likelyMethods) > 1) {
$ambiguousMethods = "";
foreach ($likelyMethods as $key => $value) {
$ambiguousMethods .= "\"" . $key . "\", ";
@@ -91,7 +112,6 @@ function findRoute($requestUri, &$routeParameters, $httpRequestMethod, $serverKe
throw new MashapeException(sprintf(EXCEPTION_AMBIGUOUS_ROUTE, $ambiguousMethods), EXCEPTION_SYSTEM_ERROR_CODE);
}
-
// Get the first item (just one or none item can exist)
foreach ($likelyMethods as $key => $value) {
$routeMethod = RESTConfigurationLoader::getMethod($key, $serverKey);
Please sign in to comment.
Something went wrong with that request. Please try again.