ruby-grape / ruby-grape/grape

Grape::Endpoint proxy scope breaks OOP inheritance

Open
#1,431 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

If my understanding is correct, when processing a request through grape, the request appears to be proxied using an anonymous instance of Grape::Endpoint, which breaks common assumptions of class inheritance.

For example:

class B < A
  get :action do 
    should_inherit # throws: NoMethodError (undefined method 'should_inherit' for <#<Class:0x...>:0x...>)
  end
end

class A < Grape::API
  def should_inherit
    "This should be available in class B when handling a request."
  end
end

When called from outside of the context of a grape API request, should_inherit resolves as expected. But when handling a request, a NoMethodError is thrown as the superclasses are not reflected in the ancestry chain of the anonymous Grape::Endpoint instance that is created to serve the request.

This presents an obvious "gotcha" when attempting to create a base class that all endpoints extend from without actually modifying the Grape::API class itself. I know it's possible to work around this issue by using modules and "helpers", but doing so is prohibitively verbose and violates the DRY principle when having to include them in every single endpoint.

If possible, could grape be modified to follow normal class inheritance when serving requests?

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

Start with lib/grape/endpoint.rb around line 17 and reproduce the example using a base Grape::API subclass and an inherited endpoint. Trace how the anonymous endpoint instance is created and determine whether request handling can preserve the superclass ancestry; done means should_inherit resolves normally during the request without requiring per-endpoint helpers.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
api, backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.