Skip to content

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

@arempe93

Description

@arempe93

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions