Angelo Giuffredi Angelo Giuffredi - 1 month ago 16x
PHP Question

FOSRestBundle - QueryParam - map and default value

I switched from version 1.* to version 2.* and wondered how it could be given an array as the default value to a

marked as map.

for example:

* @QueryParam(name="activity_filters", map=true, requirements="user_opened_resource", default="user_opened_resource")

when i try to get the param:

$activityFilters = $paramFetcher->get('activity_filters');
var_dump($activityFilters); die;

the result is:

string 'user_opened_resource' (length=20)

In previous versions of FOSRestBundle I correctly received an array as a result of my get:

array (size=1)
0 => string 'user_opened_resource' (length=20)

Is it possibile to achieve the same result in 2.* versions?


It looks like your issue is changes in the ParamFetcher from this in 1.8 to this in 2.0. In the first version (1.8) the param fetcher checks whether the params is an array and then casts the result to an array if necessary. In the second version (2.0) the default is passed to the ResolverTrait::resolveValue with no reference to what the original value type was supposed to be.

That all being said, I would assume the way you would handle this with the new way of working (2.0+) would be use an array with a single value as your default rather than a string like...

 * @QueryParam(
 *     name="activity_filters",
 *     map=true,
 *     requirements="user_opened_resource",
 *     default={"user_opened_resource"}
 * )