ruby-grape / ruby-grape/grape

Unable to mount instances instead of class definitions

Open
#480 1 comment 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

Grape seems to mostly work when you mount an instance of a class instead of the class definition itself. However, it doesn't actually and uses the instance to find the class definition. This has the side effect that configured classes don't work correctly, and any properties set on them aren't set on the actual controller

As an example:

class DebugController < Grape::API
    format :json

    def initialize(name)
        @name = name
    end

    desc "No-op that confirms that the service is working"
    get '/ping' do
        {
            :ping => @name
        }
    end
end

class API < Grape::API
    format :json

    mount DebugController.new("Test") => "/api/debug"
end

In the above application, when the controller for "/api/debug/ping" is called, the value for "@name" is Nil and not "Test", because the DebugController instance that was passed to mount isn't actually the instance that is used.

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 by tracing Grape's mount handling for the DebugController.new("Test") instance and the /api/debug/ping request in the issue example. Verify which object handles the request and whether @name remains "Test"; done means mounted instances retain their instance state instead of being replaced by a new class-based controller.

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
Clearly specified
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.