Skip to content

Conversation

@anentropic
Copy link
Contributor

@anentropic anentropic commented Nov 24, 2025

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

  1. Define a base controller with a @route-decorated method (for example, @route.generic("", methods=["GET"]) def report(self, request)).
  2. Create two concrete subclasses, each decorated with @api_controller, inheriting the base report implementation unchanged.
  3. Register both subclasses against the same NinjaExtraAPI instance.
  4. 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.

@codecov-commenter
Copy link

codecov-commenter commented Nov 24, 2025

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

❌ Patch coverage is 96.15385% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 97.82%. Comparing base (6a592c1) to head (a773825).
⚠️ Report is 59 commits behind head on master.

Files with missing lines Patch % Lines
ninja_extra/controllers/base.py 93.33% 1 Missing ⚠️
ninja_extra/testing/client.py 92.30% 1 Missing ⚠️
❗ 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     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@anentropic
Copy link
Contributor Author

anentropic commented Nov 24, 2025

this is no good as-is, will work on it some more and reopen

@anentropic anentropic closed this Nov 24, 2025
@anentropic anentropic reopened this Nov 24, 2025
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.

2 participants