Skip to content

default value gets used when given block evaluates to false #1681

@astorije

Description

@astorije

So, given this hypothetical scenario (using grape v1.0.1):

params do
  requires :foo, type: Symbol, values: [:a, :b]
  given foo: ->(val) { val == :a } do
    optional :bar, type: Symbol, default: :my_default
  end
end
put do
  binding.pry # Setting a breakpoint to highlight the following examples
end

I get the following when foo is :a, as expected:

> request.url
=> "http://example.com/api?foo=a"
> params
=> {"foo"=>:a, "bar"=>:my_default}

However, when foo is :b, this seems unexpected:

> request.url
=> "http://example.com/api?foo=b"
> params
=> {"foo"=>:b, "bar"=>:my_default}

Reason why I was surprised by this is that the parameters contract says that the default value of :my_default is only relevant when foo is a. Instead, I should be getting the following:

> params
=> {"foo"=>:b}

Is this a known case, or a bug?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions