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

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

Open
huyvohcmc opened this issue Jan 14, 2021 · 5 comments
Open
Labels

Comments

@huyvohcmc
Copy link
Contributor

huyvohcmc commented Jan 14, 2021

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.

@dblock
Copy link
Member

dblock commented Jan 14, 2021

This should work. Are you POSTing content-type=JSON? Turn this into a spec otherwise so we can see what's going on.

@dblock dblock added the bug? label Jan 14, 2021
@huyvohcmc
Copy link
Contributor Author

Yes I'm posting with content-type = application/json

class Testing < Grape::API
  namespace :testing do
    params do
      requires :content, types: [String, Hash, Array]
    end
    post do
      declared(params)[:content]
    end
  end
end
curl --request POST \
  --url http://localhost:3000/api/testing \
  --header 'Content-Type: application/json' \
  --data '{
	"content": [{"type": "paragraph", "children": [{"text": "Hahaha"}]}]
}'
>>> {{"type"=>"paragraph", "children"=>[{"text"=>"Hahaha"}]}=>nil}

If I modify params to

requires :content, types: [Array, String, Hash]

Then with the same above request I get correct result:

[{"type"=>"paragraph", "children"=>[{"text"=>"Hahaha"}]}]

But when I request with String content:

curl --request POST \
  --url http://localhost:3000/api/testing \
  --header 'Content-Type: application/json' \
  --data '{
	"content": "abcd"
}'
>>> ["abcd"]

@dblock
Copy link
Member

dblock commented Jan 15, 2021

Sounds like a bug. Turn it into a spec, maybe try fixing it?

@jcagarcia
Copy link
Contributor

Hi 👋 I've just tested this potential bug with the last Grape version and it's working fine 👍

I'm always obtaining the expected response

[{"type"=>"paragraph", "children"=>[{"text"=>"Hahaha"}]}]

@dblock
Copy link
Member

dblock commented Nov 28, 2023

@jcagarcia Thanks. Before we close we should be sure that 1) it was broken when the bug was reported (whichever version shipped last by Jan 14, 2021), 2) that we can bisect it to a specific fix/pull request, 3) that we have a spec for it.

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

3 participants