Skip to content

A param of type Hash is converted to Array in declared(params) #1239

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

Closed
arempe93 opened this issue Jan 6, 2016 · 6 comments
Closed

A param of type Hash is converted to Array in declared(params) #1239

arempe93 opened this issue Jan 6, 2016 · 6 comments
Labels

Comments

@arempe93
Copy link
Contributor

arempe93 commented Jan 6, 2016

Spec describing the issue

it 'declared Hash groups should be a Hash' do

    app.params do
        optional :group, type: Hash do
            optional :foo, type: String
        end
    end
    app.get :group do
        declared(params).to_json
    end

    get '/group'
    json = JSON.parse(last_response.body)

    puts json['group'].class.name
    expect(json['group']).to be_kind_of(Hash)
end

This produces the following output

Array
Failure/Error: expect(json['group']).to be_kind_of(Hash)
       expected [] to be a kind of Hash

Setting a default on the param is a workaround

it 'declared Hash groups should be a Hash' do

    app.params do
        optional :group, type: Hash, default: {} do
            optional :foo, type: String
        end
    end
    app.get :group do
        declared(params).to_json
    end

    get '/group'
    json = JSON.parse(last_response.body)

    puts json['group']
    expect(json['group']).to be_kind_of(Hash)
end

This spec passes without error with the expected output

{ "foo" => nil }

Using Grape v0.14.0, running specs on master

@dblock dblock added the bug? label Jan 6, 2016
@dblock
Copy link
Member

dblock commented Jan 6, 2016

Looks like a definite bug, I would expect group not to be in the response at all without a default, right?

@arempe93
Copy link
Contributor Author

arempe93 commented Jan 6, 2016

That could be an expectation, yes. Personally I like having it still be there and showing the values of the nested parameters because of the way I use / think about them as groups. But in either case, I don't think an Array is an expected value

@ochagata
Copy link
Contributor

ochagata commented Jan 6, 2016

@arempe93
Copy link
Contributor Author

Is this an intended behavior, as set forth by this spec in specs/grape/endpoint_spec.rb?. This tests pretty much the exact opposite of the spec from the issue description

@dblock
Copy link
Member

dblock commented Jan 26, 2016

I think that spec ignores the fact that there's an explicit type.

arempe93 added a commit to arempe93/grape that referenced this issue Feb 3, 2016
arempe93 added a commit to arempe93/grape that referenced this issue Feb 3, 2016
@dnesteryuk
Copy link
Member

@kadotami fixed this issue in #2043 🎆

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