ruby-grape / ruby-grape/grape-entity

Sending an array of hash

Open
#344 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Ruby
Stars
729
Forks
154
PR merge metrics
No merged PRs in 30d

Description

When I try to send an array of hash, I have the error instance of ActiveSupport::HashWithIndifferentAccess

Here is my model :
expose :book, documentation: { type: Array[Hash] }
And in my controller I have nested attributes like this :
requires :collection, type: Hash do optional :book, type: Array do requires :id, type: String requires :price, type: BigDecimal end end

When I check in my console the params book, I have an array with a hash :
[{"id"=>"351a7eaf-cf93-4f89-9f3a-b2cce4c07a70", "amount"=>0.23e2}]
But it returns the error Collection::book(#11898309) expected, got {"id"=>"351a7eaf-cf93-4f89-9f3a-b2cce4c07a70", "amount"=>0.23e2} which is an instance of ActiveSupport::HashWithIndifferentAccess(#510440)
Do you know what's wrong here ?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start from the model's expose declaration and the controller's nested parameter definition, then reproduce the reported array-of-hash request and inspect the type mismatch involving ActiveSupport::HashWithIndifferentAccess. Done means identifying why the declared Array[Hash] does not accept the received element and confirming the corrected behavior with a focused test or console reproduction.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
api
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.