FOSRestBundle icon indicating copy to clipboard operation
FOSRestBundle copied to clipboard

When setting a default parameter to null, '' is set instead

Open Ovski4 opened this issue 8 years ago • 13 comments

With the query param as follow:

@QueryParam(
    name="param1",
    strict=false,
    nullable=true,
    default=null,
    allowBlank=false,
    requirements="11|12|13|14"
)

When I format the url as such: /rest/v0/myroute?param1=, I expect to get param1 to null in my controller. But I got an empty string instead

It's because of the code at line 268 of ParamFetcher.php

return null === $default ? '' : $default;

Is there any way to avoid that? I just would like to understand why this behavior was chosen instead of simply returning a null value. If not possible, I'll just set the strict mode.

Thank you!

Ovski4 avatar Apr 07 '16 13:04 Ovski4

I guess that's because we don't know if your default value was manually set or not (null is the default value of php).

GuilhemN avatar Apr 07 '16 14:04 GuilhemN

Is there a reason it should not be as simple as :

return $default;

in that case? If not, I don't understand why.

Ovski4 avatar Apr 07 '16 15:04 Ovski4

This issue is causing me some trouble too.

I'd like to second the suggestion that this can just return $default;

Otherwise there's currently no way you can configure ParamFetcher to return null (which is what I want in my use case), but if you just return $default, then you can configure it to return '' if you want by directly specifying that as the default, so it allows the most flexibility.

angrygreenfrogs avatar May 19 '16 12:05 angrygreenfrogs

I agree. WDYT @lsmith77 @xabbuh ?

GuilhemN avatar May 19 '16 14:05 GuilhemN

Sounds reasonable to.

@Ener-Getick As you did that in #1097, do you remember why you did it this way?

xabbuh avatar May 26 '16 15:05 xabbuh

@xabbuh in fact it was introduced by @Seldaek here.

GuilhemN avatar May 26 '16 16:05 GuilhemN

@xabbuh Apparently the reason was bc (see this comment) so we can safely change it.

GuilhemN avatar May 26 '16 16:05 GuilhemN

Yeah, let's do that and document it in the upgrade document.

xabbuh avatar May 26 '16 16:05 xabbuh

Can confirm the issue since upgrading to v2.0.

What is the state of the issue?

soullivaneuh avatar Jul 05 '16 10:07 soullivaneuh

+1 .. that being said, since we did the stable release it is technically a BC break but we can call it a bug fix, since we didn't intend to carry over BC hacks from 1.x

lsmith77 avatar Jul 05 '16 10:07 lsmith77

Any news?

alegout avatar Oct 25 '17 18:10 alegout

This is still an issue, any news?

solazs avatar Jun 14 '18 11:06 solazs

+1 it seems to me that we should be able to have null as a default value. Make a lot more sense to me than ''

chriskaya avatar Jul 17 '18 12:07 chriskaya