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

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

Closed
astorije opened this issue Sep 15, 2017 · 4 comments
Closed

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

astorije opened this issue Sep 15, 2017 · 4 comments
Labels

Comments

@astorije
Copy link
Contributor

astorije commented Sep 15, 2017

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?

@rnubel
Copy link
Contributor

rnubel commented Sep 15, 2017

Looks like a bug to me. Default-value population is probably not looking at the given constraints.

@dblock
Copy link
Member

dblock commented Nov 1, 2018

@darren987469 another one? :)

@darren987469
Copy link
Contributor

No problem, I will try it. 😄

@darren987469
Copy link
Contributor

@dblock close this?

@dblock dblock closed this as completed Nov 7, 2018
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

4 participants