Skip to content

[BUG] fix_request_files_middleware skips DRF request parsers #1550

@sephii

Description

@sephii

Describe the bug

Using ninja.compatibility.files.fix_request_files_middleware makes DRF request parsing fail, which means you can’t use django-ninja + DRF with a custom request parser on a project (which can be the case eg. if you’re migrating your API progressively).

What happens is that the django-ninja middleware does request._load_post_and_files(), and when the request comes into DRF it tries to do the same and in doing so it accesses the request body, which (silently!) raises an exception (django.http.request.RawPostDataException: You cannot access body after reading from request's data stream).

You can see there’s an escape hatch which returns the already parsed data, which is why it seems to work, but in reality it completely skips request parsing, which means if you’re using a custom request parser, such as djangorestframework-camelcase it will just silently not be applied.

Versions (please complete the following information):

  • Python version: 3.12
  • Django version: 4.2
  • Django-Ninja version: 1.4.1
  • Pydantic version: 2.11.4

Suggestions

Adding a setting to allow defining a URL prefix on which the middleware should apply. If you think that’s a good idea I can submit a PR.

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