Fix for actually sub-classable controllers
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 same NinjaExtraAPI causes 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
- Define a base controller with a
@route-decorated method (for example,@route.generic("", methods=["GET"]) def report(self, request)). - Create two concrete subclasses, each decorated with
@api_controller, inheriting the basereportimplementation unchanged. - Register both subclasses against the same
NinjaExtraAPIinstance. - Resolve the URL for the first controller’s base route.
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 report method (and any other inherited routed methods). api_controller/Ninja Extra mutates that function object during registration, attaching metadata such as api_controller and operation. 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:
@route.generic("", methods=["GET", "POST"], response=None, url_name="html")
def report_html(self, request):
return super().report_html(request)
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.
:warning: Please install the to ensure uploads and comments are reliably processed by Codecov.
Codecov Report
:x: Patch coverage is 96.15385% with 2 lines in your changes missing coverage. Please review.
:white_check_mark: Project coverage is 97.82%. Comparing base (6a592c1) to head (a773825).
:warning: Report is 127 commits behind head on master.
| Files with missing lines | Patch % | Lines |
|---|---|---|
| ninja_extra/controllers/base.py | 93.33% | 1 Missing :warning: |
| ninja_extra/testing/client.py | 92.30% | 1 Missing :warning: |
| :exclamation: Your organization needs to install the Codecov GitHub app to enable full functionality. |
Additional details and impacted files
@@ Coverage Diff @@
## master #319 +/- ##
==========================================
- Coverage 98.01% 97.82% -0.20%
==========================================
Files 68 67 -1
Lines 2770 2847 +77
==========================================
+ Hits 2715 2785 +70
- Misses 55 62 +7
:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.
:rocket: New features to boost your workflow:
- :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
✅ ~this is no good as-is, will work on it some more and reopen~
@anentropic Thanks for putting together this PR. I think your problem and solution is similar to this PR.
I will see how best to merge these solutions.