Skip to content

Keep what a mounted API's superclass declares - #3111

Merged
ericproulx merged 1 commit into
masterfrom
fix/mounted-subclass-keeps-superclass
Nov 29, 2026
Merged

ericproulx merged 1 commit into
masterfrom
fix/mounted-subclass-keeps-superclass

Conversation

@ericproulx

Copy link
Copy Markdown
Contributor

Fixes #1820.

What #1820 hit

A Grape::API subclass inherits what its superclass declares (#1665), but only when it is the Rack app itself. Mounted, it lost all of it. With the issue's layout:

class Base < Grape::API
  prefix 'v2'
  format :json
end

class AuthenticatedBase < Base
  helpers AuthenticationHelper
  before { authenticate! }
end

class Sensors < AuthenticatedBase
  namespace(:sensors) { get { current_user.sensors } }
end

class API < Grape::API
  mount Sensors
end
Sensors as the Rack app mounted, before mounted, after
route GET /v2/sensors(.json) GET /sensors(.:format) GET /v2/sensors(.json)
no token 401 before skipped, then NameError on current_user 401
no token, on a route that calls no helper 401 200 401
use middleware, rescue_from applied dropped applied

It is the same on 3.0.1, 4.1.0 and master, and has been since #1665 added the inheritance (1.0.1). The specs only ever called the subclass directly.

Cause

Instance.inherit_settings re-parented the mounted instance's top_level_setting onto the scope it was mounted in. Its parent until then was its superclass's settings, so the mount cut them off.

Fix

A mounted API now inherits a copy of its superclass's settings, between its own and those of the scope it is mounted in. The superclass's settings are shared with its other subclasses and mounts, so they are copied (point_in_time_copy) rather than re-parented.

Copying all of them applies a superclass twice in the common layout where the API doing the mounting inherits from the same base:

class API < Base
  mount Users # class Users < Base
end

Base's middleware and callbacks then ran twice. So the copy stops at the first superclass that an API the mounted one ends up in also inherits from: that one already brings what it declares, as it did on master. For the copy to know every API it ends up in, including one further out that mounts the API that mounts it, mount_instance now records the API it builds the instance for (mounted_in) before replaying its setup. An API mounted during that replay then sees the whole line.

mounted_in and mounted_in= go in NON_OVERRIDABLE and are forwarded to the base instance, as base and base= are.

Behaviour

  • An inherited prefix combines with a mount path as one the subclass declares itself would: mount Sensors => '/x' serves /x/v2/....
  • Where the mounting API inherits from the same superclass, the routes and what runs are the same as on master.
  • The copy is taken at the mount, so a superclass reopened after one of its subclasses is mounted doesn't reach that mount. It does reach the subclass called as the Rack app.

Routes move and formats change for anyone mounting subclasses today, so UPGRADING has an entry, and the README's Mounting section says what a mounted subclass keeps.

Checks

  • spec/grape/api/mounted_subclass_spec.rb, 9 examples: 7 fail on master. The 2 "applies once" examples pass on master, where the mounting API brings the superclass; they fail when the copy doesn't stop, as does the one for an intermediate superclass.
  • bundle exec rake: no offenses, 3389 examples, 0 failures.
  • The change runs at mount time only; the request path is untouched.

🤖 Generated with Claude Code

A Grape::API subclass inherited its superclass's settings only when it
was the Rack app itself. Mounting it re-parented its settings onto the
scope it was mounted in, which dropped the superclass's helpers,
callbacks, middleware, auth, rescue_from, prefix and format: a helper
call raised NameError, and a before callback that authenticated was
skipped.

A mounted API now inherits a copy of its superclass's settings, between
its own and those of the scope it is mounted in. The copy stops at a
superclass that an API it is mounted in also inherits from, which
already brings those settings, so they apply once.

Fixes #1820.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ericproulx
ericproulx force-pushed the fix/mounted-subclass-keeps-superclass branch from af3ef08 to 1ace0ef Compare November 27, 2026 15:59
@ericproulx
ericproulx requested a review from dblock November 27, 2026 16:09

@dblock dblock left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like a bug fix to me.

@ericproulx
ericproulx merged commit c1259c6 into master Nov 29, 2026
27 checks passed
@ericproulx
ericproulx deleted the fix/mounted-subclass-keeps-superclass branch November 29, 2026 11:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Still having issues with helper inheritance

2 participants