ruby-grape / ruby-grape/grape

How to define multiple allowed types when one of them is array of hash?

Open
#2,150 5 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

bug?
Dominant language
Ruby
Stars
10k
Forks
1.2k
Avg merge
14h 38m
Merged PRs (30d)
92

Description

Currently I got an endpoint with params like this:

params do
  requires :content, types: [String, Hash]
end

When content can be array of hash also ([{"type": "paragraph", "children": [{"text": "test"}]}]), how are we gonna define it?

params do
  requires :content, types: [String, Hash, Array[Hash]]
end

with the code above, params[:content] will be

#<Hashie::Mash #<Hashie::Mash children=#<Hashie::Array [#<Hashie::Mash text="test">]>

which I don't think is correct? It looks like a hash of array. My expectation is params[:content] will be this:

[#<Hashie::Mash children=[#<Hashie::Mash text="test">] type="paragraph">]

because with that I can store params[:content] directly to a JSON column in the DB.

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

The issue names no files, tests, or entry points; start by locating Grape's parameter handling for the types: option and its conversion of array-of-hash values. Done means determining whether the requested declaration preserves the expected array of Hashie::Mash objects for JSON storage, and documenting or correcting that behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
api
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 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.