ruby-grape / ruby-grape/grape

Array of hashes not failing correctly when array is empty

Open
#1,693 6 comments 0 reactions 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

grape: 1.0.1
ruby: 2.4.2

When you have:

params do
          requires :paired_values, type: Array do
            requires :name, allow_blank: false, type: String
            requires :semver, allow_blank: false, type: String, semver_constraint: true
          end
        end

And semver_constraint custom validator looks like

class SemverConstraint < Grape::Validations::Base
  def validate_param!(attr_name, params)
    Semverse::Constraint.new(params[attr_name])
  rescue Semverse::InvalidConstraintFormat => e
    raise Grape::Exceptions::Validation, params: [@scope.full_name(attr_name)], message: e.message
  end
end

When you call into the api_endpoint with params: { paired_values: [] } grape ends up calling validate_param! with attr_name being :semver and params being "", so this throws the exception TypeError: no implicit conversion of Symbol into Integer. Because these values are required shouldn't the grape validation for required fail out before this is even called?

I can easily work around this but before i upgraded to 1.0.0/1.0.1 this was not throwing this exception.

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 issue from the parameter declaration and custom SemverConstraint validator shown in the report with paired_values set to an empty array. Trace the validation order for the required fields and confirm that the required validation fails before the custom validator runs; done means the expected validation error is raised instead of the TypeError.

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
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.