django-ninja icon indicating copy to clipboard operation
django-ninja copied to clipboard

Make request object optional on view functions

Open stephenrauch opened this issue 3 years ago • 1 comments

A proposal (in the form of a Pull Request) for allowing request view params to be optional.

Unchanged Behavior

  • params in the first position, named "request" are not in the Schema Docs and are passed the request.

Current Behavior (to be changed from):

  • params in the first position, not named "request", are in the Schema Docs, but are actually passed the request, and if a param with a matching name is actually passed, causes an error.
  • params not in the first position, named "request" are not in the Schema Docs, but are passed the param value named request.

Proposed Behavior (to be changed to):

  • params in the first position, not named "request", but annotated with HttpRequest, will be passed the request.
  • params in the first position, not named "request" and not annotated with HttpRequest, are in the Schema Docs, and will be passed the matching param value.
  • params not in the first position, named "request" are in the Schema Docs, and are passed the param value named request.

stephenrauch avatar Oct 27 '21 02:10 stephenrauch

Hi @stephenrauch

I've been thinking about this for some time... I like it - but I would add more flexibility by allowing also (request=2 or request = Body(...) type of cases)

I merged your request into separate branch and adapted it to cover these as well see #282 Please let me know what you think

vitalik avatar Nov 12 '21 16:11 vitalik

Hi @stephenrauch

Thank you for contribution - but this cannot be introduced - there are lot of existing django ecosystem decorators that people use along with django-ninja - and all those decorators expect 1st param to be always a request object

vitalik avatar Dec 15 '22 13:12 vitalik