Repository navigation
Keep what a mounted API's superclass declares - #3111
Merged
Merged
Conversation
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
force-pushed
the
fix/mounted-subclass-keeps-superclass
branch
from
November 27, 2026 15:59
af3ef08 to
1ace0ef
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1820.
What #1820 hit
A
Grape::APIsubclass 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:Sensorsas the Rack appGET /v2/sensors(.json)GET /sensors(.:format)GET /v2/sensors(.json)beforeskipped, thenNameErroroncurrent_userusemiddleware,rescue_fromIt 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_settingsre-parented the mounted instance'stop_level_settingonto 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:
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_instancenow 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_inandmounted_in=go inNON_OVERRIDABLEand are forwarded to the base instance, asbaseandbase=are.Behaviour
prefixcombines with a mount path as one the subclass declares itself would:mount Sensors => '/x'serves/x/v2/....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.🤖 Generated with Claude Code