Skip to content

Change the ordering of decorator application with add_decorator #1598

@way-dave

Description

@way-dave

Is your feature request related to a problem? Please describe.

In your docs for the new add_decorator functionality, you describe middleware as a suggested use-case.

Unfortunately, the order of decorator application is exactly the opposite of what I would expect to see for middleware!

For example, imagine the following definitions:

api = ninja.NinjaAPI()
api.add_decorator(decorator_1())
api.add_decorator(decorator_2())

router = ninja.Router()
router.add_decorator(decorator_3())
router.add_decorator(decorator_4())

api.add_router("/foo", router)

@router.get("/bar")
def my_op(...):
    ...

I would expect the code to be run in the following order:

  1. decorator_1
  2. decorator_2
  3. decorator_3
  4. decorator_4
  5. my_op

It is actually run in the following order:

  1. decorator_4
  2. decorator_3
  3. decorator_2
  4. decorator_1
  5. my_op

Describe the solution you'd like

I'd like it to run in declaration order, outside-in:

  1. decorator_1
  2. decorator_2
  3. decorator_3
  4. decorator_4
  5. my_op

It's currently running in reverse-declaration order, inside-out!

As someone used to how Django middleware works, this is extremely unintuitive. Worse, it means it's impossible to declare API-wide security-related decorators that are expected to run before any other decorators or operations.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions