Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion ninja/params/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,11 @@ def get_request_data(
for name, annotation in cls.__ninja_body_params__.items():
if name in request.POST:
data = request.POST[name]
if annotation is str and data[0] != '"' and data[-1] != '"':
if (
annotation is str
and not data.startswith('"')
and not data.endswith('"')
):
data = f'"{data}"'
req.body = data.encode()
results[name] = get_request_data(req, api, path_params)
Expand Down
22 changes: 21 additions & 1 deletion tests/test_forms_and_files.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django.core.files.uploadedfile import SimpleUploadedFile

from ninja import File, Form, NinjaAPI, UploadedFile
from ninja import Body, File, Form, NinjaAPI, UploadedFile
from ninja.testing import TestClient

api = NinjaAPI()
Expand All @@ -16,6 +16,15 @@ def str_and_file(
return {"title": title, "data": file.read().decode()}


@api.post("/str_body_and_file")
def str_body_and_file(
request,
title: str = Body(...),
file: UploadedFile = File(...),
):
return {"title": title, "data": file.read().decode()}


client = TestClient(api)


Expand Down Expand Up @@ -57,3 +66,14 @@ def test_files():
},
"required": True,
}


def test_empty_multipart_body_field():
file = SimpleUploadedFile("test.txt", b"data123")
response = client.post(
"/str_body_and_file",
FILES={"file": file},
POST={"title": ""},
)
assert response.status_code == 200
assert response.json() == {"title": "", "data": "data123"}