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

Given does not use proper nesting in declared(params) #1219

Closed
JanStevens opened this issue Dec 8, 2015 · 5 comments · Fixed by #1225
Closed

Given does not use proper nesting in declared(params) #1219

JanStevens opened this issue Dec 8, 2015 · 5 comments · Fixed by #1225

Comments

@JanStevens
Copy link

When using given block inside a nested requires block the validation works properly. But when trying to access these required params they are not nested in the requires block. Following spec (spec/grape/validations/params_scope_spec.rb) illustrate the problem

 it 'includes the nested parameter within #declared(params)' do
      subject.params do
        requires :bar, type: Hash do
          optional :a
          given :a do
            requires :b
          end
        end
      end
      subject.get('/nested') { declared(params).to_json }

      get '/nested', bar: { a: true, b: 'yes' }
      expect(JSON.parse(last_response.body)).to eq('bar' => { 'a' => true, 'b' => 'yes'})
    end

This fails with following message:

expected: {"bar"=>{"a"=>true, "b"=>"yes"}}
     got: {"b"=>nil, "bar"=>{"a"=>"true"}}

(compared using ==)

Diff:
@@ -1,2 +1,3 @@
-"bar" => {"a"=>true, "b"=>"yes"},
+"b" => nil,
+"bar" => {"a"=>"true"},

I tried different options (repeating the requires block in the given block) but nothing seems to work. I also cannot find the root of the problem but I guess it has to do with the following MR: #1047

@dblock dblock added the bug? label Dec 8, 2015
@stjhimy
Copy link
Contributor

stjhimy commented Dec 8, 2015

Add optional :b:

  requires :bar, type: Hash do
    optional :a
    optional :b
    given :a do
      requires :b
    end
  end
subject.get('/nested') { declared(params).to_json }
get '/nested', bar: { a: true, b: 'yes' }
=>  {"b":null,"bar":{"a":"true","b":"yes"}}

The declared method returns only declared params?
"b":null outside bar is caused by including_missing: true here: /~https://github.com/ruby-grape/grape/blob/master/lib/grape/dsl/inside_route.rb#L28 not sure why.

@JanStevens
Copy link
Author

@stjhimy Ha thanks that seems to solve the issue.

But the syntax is not really clear, why do I need the extra optional :b? Now I have defined :b two times.

I thought (and from the documentation) that I should only need the requires :b

@dblock
Copy link
Member

dblock commented Dec 9, 2015

I think that given :a requires :b in this case should automatically cause an optional :b. I wonder whether there's any case where we would not want that? If not this is a valid improvement / feature request.

@JanStevens
Copy link
Author

I still think this is a bug and not really a feature request. Following example right from the specs

subject.params do
  optional :a
  given :a do
    requires :b
  end
end
subject.get('/test') { declared(params).to_json }

Why does this work out of the box without the optional :b where the nested example does require the optional: b to work? Clearly thats a bug to me and not a feature request.

I think that given :a requires :b implies that we ONLY require :b when a is provided, In this case I could use the given as a switch for my params. It's also possible that I only want to optional :b when a is provided. I don't want to allow optional :b as long as a is not provided!

If its implemented that given :a requires :b always implies an optional :b then the following should be invalid: given :a optional :b since its implied already.

Anyway quoting from the docs:

Suppose some of your parameters are only relevant if another parameter is given; Grape allows you to express this relationship through the given method in your parameters block, like so:

This implies for me the behaviour given :a requires :b without the implicit optional :b

@dblock
Copy link
Member

dblock commented Dec 9, 2015

Ok I am with you wrt bug. Would love a fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants