ruby-grape / ruby-grape/grape

given and declared do not work together

Open
#1,641 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

I have the following params definition:

        module FooParams
          extend Grape::API::Helpers

          params :bar_params do
            requires :a, type: String
          end

          params :baz_params do
            requires :b, type: String
          end

          params :foo_params do
            requires :type, type: String
            given type: -> (val) { val == 'Bar' } do
              requires :foo, type: Hash do
                use :bar_params
              end
            end
            given type: -> (val) { val == 'Baz' } do
              requires :foo, type: Hash do
                use :baz_params
              end
            end
          end
        end

The foo_params are used in this post:

        resource :foos do
          helpers FooParams

          params do
            use :foo_params
          end

          post do
            if params[:type] == 'Bar'
              puts "original a: '#{params[:foo][:a]}'"
              puts "declared a: '#{declared(params)[:foo][:a]}'"
            end
            {}
          end
        end

Now when I call POST /foos with {type: 'Bar', {foo: {a: 'letter A'}}}, I get the following output:

original a: 'letter A'
declared a: ''

If I remove the latter given definition (val == 'Baz'), it prints:

original a: 'letter A'
declared a: 'letter A'

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

Reproduce the issue with the shown FooParams definitions and POST /foos request. Start by tracing how the given and declared parameter handling uses multiple conditional branches. Done means declared(params) retains foo.a when type is Bar even when the Baz branch is also defined, with a regression test covering both branches.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.