Skip to content

Commit d68b4d5

Browse files
committed
Merge pull request #649 from entering/request-param-optional-origin-name
Add a option to specify the request original name
2 parents 37bd3e1 + 2a4e145 commit d68b4d5

File tree

4 files changed

+23
-2
lines changed

4 files changed

+23
-2
lines changed

Controller/Annotations/Param.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ abstract class Param
2222
/** @var string */
2323
public $name;
2424
/** @var string */
25+
public $key = null;
26+
/** @var string */
2527
public $requirements = '';
2628
/** @var mixed */
2729
public $default = null;
@@ -33,4 +35,12 @@ abstract class Param
3335
public $array = false;
3436
/** @var boolean */
3537
public $nullable = false;
38+
39+
/**
40+
* @return string
41+
*/
42+
public function getKey()
43+
{
44+
return $this->key ?: $this->name;
45+
}
3646
}

Request/ParamFetcher.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ public function get($name, $strict = null)
8181
throw new \InvalidArgumentException(sprintf("No @QueryParam/@RequestParam configuration for parameter '%s'.", $name));
8282
}
8383

84+
/** @var Param $config */
8485
$config = $this->params[$name];
8586
$nullable = $config->nullable;
8687
$default = $config->default;
@@ -94,9 +95,9 @@ public function get($name, $strict = null)
9495
}
9596

9697
if ($config instanceof RequestParam) {
97-
$param = $this->request->request->get($name, $default);
98+
$param = $this->request->request->get($config->getKey(), $default);
9899
} elseif ($config instanceof QueryParam) {
99-
$param = $this->request->query->get($name, $default);
100+
$param = $this->request->query->get($config->getKey(), $default);
100101
} else {
101102
$param = null;
102103
}

Resources/doc/annotations-reference.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use FOS\RestBundle\Controller\Annotations\QueryParam;
1111
/**
1212
* @QueryParam(
1313
* name="",
14+
* key=null,
1415
* requirements="",
1516
* default=null,
1617
* description="",
@@ -29,6 +30,7 @@ use FOS\RestBundle\Controller\Annotations\RequestParam;
2930
/**
3031
* @RequestParam(
3132
* name="",
33+
* key=null,
3234
* requirements="",
3335
* default=null,
3436
* description="",

Tests/Request/ParamFetcherTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ public function setup()
7676

7777
$annotations['biz'] = new QueryParam;
7878
$annotations['biz']->name = 'biz';
79+
$annotations['biz']->key = 'business';
7980
$annotations['biz']->requirements = '\d+';
8081
$annotations['biz']->default = null;
8182
$annotations['biz']->nullable = true;
@@ -330,4 +331,11 @@ public function testExceptionOnNonConfiguredParameter()
330331
$queryFetcher->setController($this->controller);
331332
$queryFetcher->get('none', '42');
332333
}
334+
335+
public function testKeyPrecedenceOverName()
336+
{
337+
$queryFetcher = $this->getParamFetcher(array('business' => 5));
338+
$queryFetcher->setController($this->controller);
339+
$this->assertEquals(5, $queryFetcher->get('biz'));
340+
}
333341
}

0 commit comments

Comments
 (0)