Skip to content

Conversation

arjandepooter
Copy link

Summary

Adds support for _ninja_ignore_args attribute to enable dependency injection patterns in Django Ninja view functions.

Problem

When using decorators that inject dependencies as function arguments, Django Ninja tries to process these injected arguments as API parameters, causing them to appear in the OpenAPI schema and parameter validation. This breaks clean dependency injection patterns.

Solution

  • Add _ninja_ignore_args attribute support in ViewSignature processing
  • Arguments listed in this attribute are skipped during signature analysis
  • Add ignore_args() utility function for convenience
  • Maintains full backward compatibility

Example Usage

def inject_service(func):
    @wraps(func)
    def wrapper(*args, **kwargs):
        kwargs["service"] = MyService()
        return func(*args, **kwargs)
    
    wrapper._ninja_ignore_args = ["service"]
    return wrapper

@api.get("/endpoint")
@inject_service  
def my_view(request, param: str, service: MyService):
    return service.process(param)

The service parameter is injected by the decorator but ignored by Django Ninja's parameter processing.

Testing

Added integration test test_ninja_ignore_args_integration() that demonstrates the complete flow including dependency injection, runtime functionality, and OpenAPI schema validation.

Enables decorators to inject dependencies as function arguments while
excluding them from Django Ninja's parameter processing and OpenAPI schema.

See `test_ninja_ignore_args_integration` in `tests/test_decorators.py` for usage example.
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.

1 participant