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

declared transforms empty array to hash #1642

Closed
karhatsu opened this issue Jun 2, 2017 · 9 comments
Closed

declared transforms empty array to hash #1642

karhatsu opened this issue Jun 2, 2017 · 9 comments
Labels

Comments

@karhatsu
Copy link

karhatsu commented Jun 2, 2017

I am using 0.19.2 and I have the following endpoint:

        resource :foos do
          params do
            requires :foo, type: Hash do
              requires :name, type: String
              optional :arr, type: Array do
                requires :param1, type: String
                requires :param2, type: String
              end
            end
          end

          post do
            puts "original: #{params.to_hash['foo']}"
            puts "declared: #{declared(params).to_hash['foo']}"
            {}
          end
        end

When I call it with

    {
        foo: {
            name: 'test',
            arr: []
        }
    }

I get this output:

original: {"name"=>"test"}
declared: {"name"=>"test", "arr"=>{"param1"=>nil, "param2"=>nil}}

In other words, for some reason the empty arr array has become a hash containing arr's required fields.

@dblock
Copy link
Member

dblock commented Jun 5, 2017

How do you call it? Are you posting JSON with the correct content-type?

@dblock dblock added the bug? label Jun 5, 2017
@karhatsu
Copy link
Author

karhatsu commented Jun 6, 2017

Yes, it's application/json.

@dblock
Copy link
Member

dblock commented Jun 6, 2017

Try to build a spec for this next.

@karhatsu
Copy link
Author

Unfortunately I'm not able to write a failing test for this. I tried both with master and v0.19.2.

@hwaterke
Copy link

I am having the same issue after updating Grape to the latest version.

params :instance_params do
  requires :name, type: String
  optional :locations, type: Array do
    requires :latitude, type: Float
    requires :longitude, type: Float
  end
end

When sending something without the locations key (as it is optional), the array is transformed into a Hash.

Sending

{
  "name": "FooBar",
  "locations": []
}

Works fine

But sending:

{
  "name": "FooBar"
}

Results in the following declared(params):

{
  "name" => "FooBar",
  "locations" => {
    "latitude" => nil,
    "longitude" => nil
  }
}

jeradphelps added a commit to jeradphelps/grape that referenced this issue Aug 28, 2017
@jeradphelps
Copy link
Contributor

jeradphelps commented Aug 28, 2017

I experienced this issue when attempting to upgrade to 19.2 as well. It looks like there's some spec detailing this functionality for hashes but not for arrays.

This looks to have changed here: #1575. There's some brief discussion in this ticket. From the sounds of it, the expectation is that we'd keep the functionality of nested arrays being populated as an array with declared.

Here's a diff with a failing spec that demos this issue. I can work on a PR if we confirm the expectation:

https://github.com/ruby-grape/grape/compare/master...jeradphelps:jeradphelps/issue-1642?expand=1

@dblock
Copy link
Member

dblock commented Aug 28, 2017

Spec looks legit, PR with a fix? ;)

@pablonahuelgomez
Copy link
Contributor

Gentlemen, I wrote the #1710 PR to solve this problem.

@dblock
Copy link
Member

dblock commented Nov 22, 2017

Closed via #1710.

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

5 participants