Grape::Endpoint proxy scope breaks OOP inheritance
Nobody has claimed this yet.
- 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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