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

Cannot distinguish empty Array from missing parameters #1984

Open
AaronBaldwin opened this issue Jan 22, 2020 · 3 comments
Open

Cannot distinguish empty Array from missing parameters #1984

AaronBaldwin opened this issue Jan 22, 2020 · 3 comments
Labels

Comments

@AaronBaldwin
Copy link

  optional :my_field, type: Array

When this optional field is set to [] (a valid value), Grape API removes it from the parameters. I cannot find any way to make this work in 0.14.0. I've tried using the coerce_with specifier, but that does not get called in either case of nil or [].

References:
#1316
#707

I've tried the strategies posted on these other issues, but as mentioned, the coerce_with does not work and there is not anything in the docs about this behavior and how to fix it.

@dblock dblock added the bug? label Jan 23, 2020
@dblock
Copy link
Member

dblock commented Jan 23, 2020

Try writing a spec for what you intend to see @AaronBaldwin ?

@dm1try
Copy link
Member

dm1try commented Jan 23, 2020

When this optional field is set to [] (a valid value), Grape API removes it from the parameters

Could you provide an example how the value was set to this field?(has json payload used?) and why do you think grape removes it?

@dnesteryuk
Copy link
Member

In case of Grape 1.3.1, the field is kept, however, the value "mutates" 🙂

require 'grape'

class API < Grape::API
  prefix :api
  version 'v1', using: :path
  params do
    optional :collection, type: Array
  end
  post '/' do
    declared = declared(params)

    puts declared.inspect
  end
end

options = {
  method: 'POST',
  params: {
    collection: []
  }
}

env = Rack::MockRequest.env_for('/api/v1', options)
API.call env

Output:

{"collection"=>nil}

If collection=nil, the output would be {"collection"=>[]} 😳

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants