Fix for actually sub-classable controllers #319
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.
My motivation for #311 was to be able to define routes on a base Controller, then sub-class the controller and override parts of the implementation in the sub-classes.
Url-namespacing was necessary for that, but it revealed further problems when trying to actually use it.
Basically it seemed like when I fetched a route from ControllerA, method on ControllerB was getting called. Likely in some way because ControllerB was registered last.
I'm busy at day job, so I'll say up-front a disclaimer that the fix and description below have mostly been written by GPT-5-Codex. (We first prepared a failing test, then fixed it, then cleaned up the fix.)
I'll be happy to refine this by hand according to any review feedback.
Summary
When a controller subclasses a base class that already defines Ninja Extra
@route-decorated methods, the decorated function object is reused for every subclass. Registering multiple subclasses against the sameNinjaExtraAPIcauses the second registration to overwrite the route metadata created for the first. As a result, a URL that should resolve to controller A ends up bound to controller B (typically the most recently registered subclass).Reproduction
@route-decorated method (for example,@route.generic("", methods=["GET"]) def report(self, request)).@api_controller, inheriting the basereportimplementation unchanged.NinjaExtraAPIinstance.Observed behaviour: Django resolves the route to the second controller’s method; the first controller is never invoked when the shared route is hit.
Root Cause
Python shares the exact same function object for the inherited
reportmethod (and any other inherited routed methods).api_controller/Ninja Extra mutates that function object during registration, attaching metadata such asapi_controllerandoperation. The second registration overwrites those attributes, so every subclass now points to the most recently registered controller.Workaround
Override the route methods in each subclass and redecorate them locally:
By creating a distinct function object per subclass, each controller retains its own route metadata and URL resolution works again.
Expected Behaviour
Registering multiple subclasses of a base controller (sharing a common routed method via inheritance) should keep their routes isolated. Each controller should own a separate
Operation/metadata record without needing boilerplate overrides.Suggested Fix
During controller registration, Ninja Extra should copy or clone the route function per controller rather than mutating the inherited function object in place. That would mirror how Django CBVs create per-class bound views and prevent later registrations from hijacking earlier ones.