custom type not suppot multiple types, raise `Grape::Exceptions::ValidationErrors`
Nobody has claimed this yet.
- Dominant language
- Ruby
- Stars
- 10k
- Forks
- 1.2k
- Avg merge
- 14h 38m
- Merged PRs (30d)
- 92
Description
My project use a custom type like this.
module Types
class Stage
def self.parse(value)
"#{value}haha"
end
def self.parsed?(value)
value.is_a? String
end
end
end
And defined params like this.
params do
optional :stage, types: [Array[::Types::Stage], ::Types::Stage]
optional :province_id, types: [Array[Integer], Integer]
end
get :list do
p declared(params)
end
There are no problem when I send these requests.
http GET 127.0.0.1:3000/api/list province_id[]==1 # {"stage"=>nil, "province_id"=>[1]}
http GET 127.0.0.1:3000/api/list province_id==1 # {"stage"=>nil, "province_id"=>1}
http GET 127.0.0.1:3000/api/list stage[]==1 # {"stage"=>["1haha"], "province_id"=>nil}
But when I send this request, raise a Grape::Exceptions::ValidationErrors
http GET 127.0.0.1:3000/api/list stage==1
# ~/.rbenv/versions/3.2.2/gemsets/gems/grape-2.0.0/lib/grape/endpoint.rb:363:in `run_validators'
# ~/.rbenv/versions/3.2.2/gemsets/gems/grape-2.0.0/lib/grape/endpoint.rb:258:in `block in run'
I think there is a problem with the call method of lib/grape/validations/types/custom_type_collection_coercer.rb.
def call(value)
coerced = value.map do |item|
coerced_item = super(item)
return coerced_item if coerced_item.is_a?(InvalidValue)
coerced_item
end
@set ? Set.new(coerced) : coerced
end
When use multiple types, there is a possibility that value is not an array.
Maybe should determine unless value.is_a?(Array) and return InvalidValue.new, like DryTypeCoercer#call
If this is correct, I can create a PR to fix it.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in lib/grape/validations/types/custom_type_collection_coercer.rb at call and compare its handling with DryTypeCoercer#call. Reproduce the stage==1 request and verify how a non-array value is handled when multiple types are declared. Done means the scalar custom type no longer raises Grape::Exceptions::ValidationErrors while array input continues to work.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- ruby
- Domain
- api
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 45/100