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

custom type not suppot multiple types, raise Grape::Exceptions::ValidationErrors #2391

Open
hsw15192617273 opened this issue Dec 20, 2023 · 1 comment
Labels

Comments

@hsw15192617273
Copy link

hsw15192617273 commented Dec 20, 2023

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.

@dblock
Copy link
Member

dblock commented Dec 21, 2023

Looks like a legit bug.

If this is correct, I can create a PR to fix it.

Please!

@dblock dblock added the bug? label Dec 21, 2023
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

2 participants