FOSRestBundle icon indicating copy to clipboard operation
FOSRestBundle copied to clipboard

Strip unprovided parameters from returned data ->all()

Open imbue opened this issue 7 years ago • 4 comments

FOSRestBundle version: 2.5.0

Is there a way to prevent that all QueryParam(s), which are not specifically provided, to be returned as value "" or null?

Lets say we have the following code snippet

/**
 * @QueryParam(name="valueX", description="Value X", nullable=true)
 * @QueryParam(name="valueY", description="Value Y")
 * @QueryParam(name="valueZ", description="Value Z")
 *
 * @param ParamFetcher $params
 *
 * @return View
 */
public function cgetAction(ParamFetcher $params): View
{
    dump($params->all());

    return ...;
}

Which the provided query string /endpoint.json?valueY=any_value&valueZ=any_other_value

Returns

array:3 [
  "valueX" => null,
  "valueY" => "any_value",
  "valueZ" => "any_other_value"
]

Is there a possibility to simply strip the valueX key value, since we have not provided it in the initial query anyways?

array:2 [
  "valueY" => "any_value",
  "valueZ" => "any_other_value"
]

Former versions of the rest bundle (~1.*) would indeed strip the empty key values. After upgrading to 2.5 some issues are faced due to using isset() in the codebase to check if a parameter was provided (whether that was a good idea or not...)

imbue avatar Jan 21 '19 11:01 imbue

@xabbuh will you realize this feature? If you have no time I can try to do that

gam6itko avatar Oct 24 '19 10:10 gam6itko

@gam6itko You said former versions of the bundle did not expose this behaviour. Did you manage to find out which commit exactly introduced the change in behaviour?

xabbuh avatar Oct 26 '19 07:10 xabbuh

@xabbuh I reckon you meant to tag me; I will try to track down which commit caused this behavior in the next few days.

imbue avatar Oct 31 '19 19:10 imbue

@imbue Oh I didn't realise that you didn't write the last comment. 🙈 Thank you!

xabbuh avatar Nov 01 '19 11:11 xabbuh