ruby-grape / ruby-grape/grape

Accessing endpoint's params

Open
#2,022 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature request
Dominant language
Ruby
Stars
10k
Forks
1.2k
Avg merge
14h 38m
Merged PRs (30d)
92

Description

Goal

I want to test params of the endpoint without making additional request because it makes tests quicker and more readable.

Context

It would be nice to be able to access param list of the endpoint just by initializing the class. If we have a class:

class MyApi < Grape::API
  params do
    requires :id, type: Integer, positive_value: true # positive_value - is a custom validation
  end
  
  get '/foo' do
    # ...
  end
end

Let's consider very simple interface to make it obvious. There can be a method MyApi.new.action_params(:get, '/foo') and it returns a list:

{ 
  id: {type: Integer, required: true, custom_validations: [:positive_value]
} 

And specs may look like:

describe 'checking GET /foo params' do
  subject { MyApi.new.action_params(:get, '/foo') }
  it { is_expected[:id].to eq((:type: 'Integer', required: true, custom_validations: []) }
end
Suggestion

Return array of classes EndpointParam:

class EndpointParam < Dry::Struct
  attribute :name, Types::String
  attribute :required, Types::Bool
  attribute :type, Types::Any
  attribute :custom_validators, Types::Array.of(EndpointParam)
end

Matcher:

describe 'checking GET /foo params' do
  subject { MyApi.new.action_params(:get, '/foo')  } 
  it { is_expected.to have_param(:id).with_type(Types::String).being_required.with_custom_validators(:positive_value) }
end

It was discussed here https://groups.google.com/forum/#!topic/ruby-grape/uww_W6c7Obs and examples are got also from there

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

No files, tests, or entry points are named. Start by reviewing how Grape::API currently stores endpoint parameter declarations and read the linked ruby-grape discussion; define the public inspection interface and its parameter representation before adding coverage. Done means endpoint parameters, requiredness, types, and custom validations can be inspected without an additional request.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
api, backend-api-design
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.