Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[QueryParam] Nullable array with validation #960

Closed
eliecharra opened this issue Feb 4, 2015 · 9 comments · Fixed by #963
Closed

[QueryParam] Nullable array with validation #960

eliecharra opened this issue Feb 4, 2015 · 9 comments · Fixed by #963

Comments

@eliecharra
Copy link
Contributor

In 1.4.2 I had the following behaviour to allow an optional array param and array item validation when array is not null :

use FOS\RestBundle\Controller\Annotations\QueryParam;

     /*
      * @QueryParam(array=true, name="category_ids", requirements="\d+", strict=true, nullable=true, description="Array of container categories ids : category_ids[]=x&category_ids[]=x")
      */

Now in 1.5.0 when I submit an empty param I get

{
  "code": 400,
  "message": "Query parameter value of 'category_ids' is not an array"
}

Seems like #910 is breaking this in 1.5.0.
I would like to allow an empty array param but dont wanna loose the validation.
If I set strict to false i'm loosing array items validation.

Easy to reproduce with the above annotation.

Is my initial annotation badly crafted or a BC break ?
I have not looked into the code, but it seems like nullable param for an array is not considered.

@eugef
Copy link
Contributor

eugef commented Feb 5, 2015

If parameter is array then strict is used to determine if parameter is optional or not.
Probably this is wrong and nullable should be used instead (for now nullable is not used at all for array parameter validation - see

if ($config->array) {
).

If we use both strict and nullable options then following configurations are possible that cover all cases:

  • nullable=true and strict=true - array parameter is optional, items validation is strict
  • nullable=false and strict=true - array parameter is required, items validation is strict
  • nullable=true and strict=false - array parameter is optional, items validation is not strict
  • nullable=false and strict=false - array parameter is required, items validation is not strict

@lsmith77 , what do you think?

@eliecharra
Copy link
Contributor Author

nullable=true and strict=true - array parameter is optional, items validation is strict
nullable=false and strict=true - array parameter is required, items validation is strict
nullable=true and strict=false - array parameter is optional, items validation is not strict
nullable=false and strict=false - array parameter is required, items validation is not strict

@eugef @lsmith77 This behavior is better.
strict param shoud be user for validation only.
and nullable param shoud be user for optional purpose

@lsmith77
Copy link
Member

lsmith77 commented Feb 5, 2015

+1

@ruscon
Copy link

ruscon commented Feb 5, 2015

+1
Can you add this hotfix to the current release ?

@eugef
Copy link
Contributor

eugef commented Feb 5, 2015

@eliecharra , at line

if ($config->array && (null !== $default || !$strict)) {
strict also should be replaced with nullable

And probably tests should be adjusted for the new behavior.

@lsmith77
Copy link
Member

lsmith77 commented Feb 5, 2015

if someone sends a PR I can quickly release

@eliecharra
Copy link
Contributor Author

@lsmith77 PR #963 in progress, i'm fixing tests

@lsmith77
Copy link
Member

lsmith77 commented Feb 6, 2015

can you all test the PR as well? want to make sure I do not get another regression and I do not have a good code base I can use for testing this beyond our tests.

@eliecharra
Copy link
Contributor Author

Just tried to add a test to cover #910 break but couldn't make it work, if someone has some time ...

lsmith77 pushed a commit that referenced this issue Feb 9, 2015
lsmith77 pushed a commit that referenced this issue Feb 9, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants