Skip to content

Commit 686895c

Browse files
authored
Merge pull request #1568 from FriendsOfSymfony/Ener-Getick-patch-1
[ParamFetcher] Fix default resolving
2 parents 1839894 + daeda0a commit 686895c

File tree

4 files changed

+16
-20
lines changed

4 files changed

+16
-20
lines changed

Request/ParamFetcher.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,17 +101,19 @@ public function get($name, $strict = null)
101101
/** @var ParamInterface $param */
102102
$param = $params[$name];
103103
$default = $param->getDefault();
104+
$default = $this->resolveValue($this->container, $default);
104105
$strict = (null !== $strict ? $strict : $param->isStrict());
105106

106107
$paramValue = $param->getValue($this->getRequest(), $default);
107108

108-
return $this->cleanParamWithRequirements($param, $paramValue, $strict);
109+
return $this->cleanParamWithRequirements($param, $paramValue, $strict, $default);
109110
}
110111

111112
/**
112113
* @param ParamInterface $param
113114
* @param mixed $paramValue
114115
* @param bool $strict
116+
* @param mixed $default
115117
*
116118
* @throws BadRequestHttpException
117119
* @throws \RuntimeException
@@ -120,11 +122,8 @@ public function get($name, $strict = null)
120122
*
121123
* @internal
122124
*/
123-
protected function cleanParamWithRequirements(ParamInterface $param, $paramValue, $strict)
125+
protected function cleanParamWithRequirements(ParamInterface $param, $paramValue, $strict, $default)
124126
{
125-
$default = $param->getDefault();
126-
$default = $this->resolveValue($this->container, $default);
127-
128127
$this->checkNotIncompatibleParams($param);
129128
if ($default !== null && $default === $paramValue) {
130129
return $paramValue;

Tests/Functional/Bundle/TestBundle/Controller/ParamFetcherController.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ class ParamFetcherController extends FOSRestController
2424
{
2525
/**
2626
* @RequestParam(name="raw", requirements=@IdenticalTo({"foo"="raw", "bar"="foo"}), default="invalid")
27-
* @RequestParam(name="map", map=true, requirements=@IdenticalTo({"foo"="map", "foobar"="foo"}), default="%invalid% %%")
28-
* @RequestParam(name="bar", map=true, requirements="%foo% foo", strict=true)
27+
* @RequestParam(name="map", map=true, requirements=@IdenticalTo({"foo"="map", "foobar"="foo"}), default="%invalid2% %%")
28+
* @RequestParam(name="bar", map=true, requirements="%bar% foo", strict=true)
2929
*/
3030
public function paramsAction(ParamFetcherInterface $fetcher)
3131
{
@@ -34,7 +34,7 @@ public function paramsAction(ParamFetcherInterface $fetcher)
3434

3535
/**
3636
* @QueryParam(name="foo", default="invalid")
37-
* @RequestParam(name="bar", default="foo")
37+
* @RequestParam(name="bar", default="%foo%")
3838
*/
3939
public function testAction(Request $request, ParamFetcherInterface $fetcher)
4040
{

Tests/Functional/app/ParamFetcher/config.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ imports:
22
- { resource: ../config/default.yml }
33

44
parameters:
5-
foo: bar
6-
invalid: invalid2
5+
bar: bar
6+
foo: foo
7+
invalid2: invalid2
78

89
framework:
910
serializer:

Tests/Request/ParamFetcherTest.php

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,6 @@ public function testDefaultReplacement()
138138
public function testReturnBeforeGettingConstraints()
139139
{
140140
$param = $this->getMockBuilder(\FOS\RestBundle\Controller\Annotations\ParamInterface::class)->getMock();
141-
$param
142-
->expects($this->once())
143-
->method('getDefault')
144-
->willReturn('default');
145141
$param
146142
->expects($this->never())
147143
->method('getConstraints');
@@ -150,7 +146,7 @@ public function testReturnBeforeGettingConstraints()
150146

151147
$this->assertEquals(
152148
'default',
153-
$method->invokeArgs($fetcher, array($param, 'default', null))
149+
$method->invokeArgs($fetcher, array($param, 'default', null, 'default'))
154150
);
155151
}
156152

@@ -161,7 +157,7 @@ public function testReturnWhenEmptyConstraints()
161157

162158
$this->assertEquals(
163159
'value',
164-
$method->invokeArgs($fetcher, array($param, 'value', null))
160+
$method->invokeArgs($fetcher, array($param, 'value', null, null))
165161
);
166162
}
167163

@@ -198,7 +194,7 @@ public function testNoValidationErrors()
198194
->with('value', array('constraint'))
199195
->willReturn(array());
200196

201-
$this->assertEquals('value', $method->invokeArgs($fetcher, array($param, 'value', null)));
197+
$this->assertEquals('value', $method->invokeArgs($fetcher, array($param, 'value', null, null)));
202198
}
203199

204200
public function testValidationErrors()
@@ -218,7 +214,7 @@ public function testValidationErrors()
218214
->with('value', ['constraint'])
219215
->willReturn($errors);
220216

221-
$this->assertEquals('default', $method->invokeArgs($fetcher, array($param, 'value', false)));
217+
$this->assertEquals('default', $method->invokeArgs($fetcher, array($param, 'value', false, 'default')));
222218
}
223219

224220
public function testValidationException()
@@ -238,7 +234,7 @@ public function testValidationException()
238234
->willReturn($errors);
239235

240236
try {
241-
$method->invokeArgs($fetcher, array($param, 'value', true));
237+
$method->invokeArgs($fetcher, array($param, 'value', true, 'default'));
242238
$this->fail(sprintf('An exception must be thrown in %s', __METHOD__));
243239
} catch (InvalidParameterException $exception) {
244240
$this->assertSame($param, $exception->getParameter());
@@ -272,7 +268,7 @@ public function testValidationErrorsInStrictMode()
272268
->with('value', array('constraint'))
273269
->willReturn($errors);
274270

275-
$method->invokeArgs($fetcher, array($param, 'value', true));
271+
$method->invokeArgs($fetcher, array($param, 'value', true, null));
276272
}
277273

278274
protected function getFetcherToCheckValidation($param, array $constructionArguments = null)

0 commit comments

Comments
 (0)