ruby-grape / ruby-grape/grape

Parameters with multiple types fail validation

Open
#1,332 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

I'm seeing validation fail on Grape 0.15.0 for parameters which allow multiple types, as shown by the test case below. Replacing [Integer,String] with simply String or Integer works when provided the relevant format.

require 'grape'

class TestAPI < Grape::API
  params do
    # We originally saw the problem on a JSON parameter, so ensure it applies both on
    # that and a normal parameter.
    requires :message, type: JSON do
      requires :id, type: [Integer,String]
    end
    requires :id, type: [Integer,String]
  end
  post do
    status 200
    { message_id: params[:message][:id], id: params[:id] }
  end
end

run TestAPI

This request will return message[id] is invalid, id is invalid.

POST  HTTP/1.1
Host: localhost:9292
Content-Type: application/json
Cache-Control: no-cache

{
    "message": "{ \"id\": \"12\" }",
    "id": "12"
}

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 by reproducing the supplied Grape API example with both top-level and nested parameters, then trace the parameter validation path for type arrays. Done means values matching either Integer or String are accepted in both contexts without invalid-parameter errors.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
api, backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.