Skip to content

Commit

Permalink
Changes Routes to prefer to map parameters to body first and then to …
Browse files Browse the repository at this point in the history
…query if it is a GET or DELETE request, and then on path only if it belongs to Routes::$prefixingParameterNames

Also assuming type as string when not defined. This helps to get the api right with the bare minimal setup
  • Loading branch information
Arul- committed Apr 13, 2014
1 parent 85847e6 commit 61c57fc
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions Routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
*/
class Routes
{
public static $prefixingParameterNames = array(
'id'
);
protected static $routes = array();

protected static $models = array();
Expand Down Expand Up @@ -91,6 +94,9 @@ public static function addAPIClass($className,
if (isset($metadata['return']['type']) && $qualified = Scope::resolve($metadata['return']['type'], $scope)) {
list($metadata['return']['type'], $metadata['return']['children']) =
static::getTypeAndModel(new ReflectionClass($qualified), $scope);
} else {
//assume return type is array
$metadata['return']['type'] = 'array';
}
foreach ($params as $param) {
$children = array();
Expand Down Expand Up @@ -130,7 +136,7 @@ public static function addAPIClass($className,
if (isset($type)) {
$m['type'] = $type;
}
$m ['children'] = $children;
$m['children'] = $children;

if ($m['name'] == Defaults::$fullRequestDataName) {
$from = 'body';
Expand All @@ -143,13 +149,16 @@ public static function addAPIClass($className,
if (!isset($type)) {
$type = $m['type'] = 'array';
}
} elseif ($m['required']) {
} elseif ($m['required'] && in_array($m['name'], static::$prefixingParameterNames)) {
$from = 'path';
} else {
$from = 'query';
$from = 'body';
}
}
$m[CommentParser::$embeddedDataName]['from'] = $from;
if (!isset($m['type'])) {
$type = $m['type'] = 'string';
}

if ($allowAmbiguity || $from == 'path') {
$pathParams [$position] = true;
Expand Down Expand Up @@ -201,9 +210,12 @@ public static function addAPIClass($className,
strpos($url, '{' . $p['name'] . '}') ||
strpos($url, ':' . $p['name']);
if ($inPath) {
$copy['metadata']['param'][$i]['from'] = 'path';
} elseif ((!isset($p['from']) || $p['from'] == 'path')) {
$copy['metadata']['param'][$i]['from'] =
$copy['metadata']['param'][$i][CommentParser::$embeddedDataName]['from'] = 'path';
} elseif (
!isset($p[CommentParser::$embeddedDataName]['from']) ||
$p[CommentParser::$embeddedDataName]['from'] == 'path'
) {
$copy['metadata']['param'][$i][CommentParser::$embeddedDataName]['from'] =
$httpMethod == 'GET' ||
$httpMethod == 'DELETE'
? 'query'
Expand Down Expand Up @@ -251,7 +263,7 @@ function ($matches) use ($call) {
if ($from == 'body' && ($httpMethod == 'GET' ||
$httpMethod == 'DELETE')
) {
$from = $metadata['param'][$position]['from']
$from = $metadata['param'][$position][CommentParser::$embeddedDataName]['from']
= 'query';
}
if (!isset($pathParams[$position]))
Expand Down Expand Up @@ -441,7 +453,7 @@ protected static function populate(array $call, $data)
$lastBodyParamIndex = -1;
$lastM = null;
foreach ($call['metadata']['param'] as $k => $m) {
if ($m['from'] == 'body') {
if ($m[CommentParser::$embeddedDataName]['from'] == 'body') {
$bodyParamCount++;
$lastBodyParamIndex = $k;
$lastM = $m;
Expand Down

0 comments on commit 61c57fc

Please sign in to comment.