Browse Source

🐛 Fix form field regression (#12194)

pull/12200/head
Nico Tonnhofer 7 months ago
committed by GitHub
parent
commit
88d4f2cb18
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 2
      fastapi/dependencies/utils.py
  2. 10
      tests/test_forms_single_model.py

2
fastapi/dependencies/utils.py

@ -788,7 +788,7 @@ async def _extract_form_body(
tg.start_soon(process_fn, sub_value.read) tg.start_soon(process_fn, sub_value.read)
value = serialize_sequence_value(field=field, value=results) value = serialize_sequence_value(field=field, value=results)
if value is not None: if value is not None:
values[field.name] = value values[field.alias] = value
for key, value in received_body.items(): for key, value in received_body.items():
if key not in values: if key not in values:
values[key] = value values[key] = value

10
tests/test_forms_single_model.py

@ -3,7 +3,7 @@ from typing import List, Optional
from dirty_equals import IsDict from dirty_equals import IsDict
from fastapi import FastAPI, Form from fastapi import FastAPI, Form
from fastapi.testclient import TestClient from fastapi.testclient import TestClient
from pydantic import BaseModel from pydantic import BaseModel, Field
from typing_extensions import Annotated from typing_extensions import Annotated
app = FastAPI() app = FastAPI()
@ -14,6 +14,7 @@ class FormModel(BaseModel):
lastname: str lastname: str
age: Optional[int] = None age: Optional[int] = None
tags: List[str] = ["foo", "bar"] tags: List[str] = ["foo", "bar"]
alias_with: str = Field(alias="with", default="nothing")
@app.post("/form/") @app.post("/form/")
@ -32,6 +33,7 @@ def test_send_all_data():
"lastname": "Sanchez", "lastname": "Sanchez",
"age": "70", "age": "70",
"tags": ["plumbus", "citadel"], "tags": ["plumbus", "citadel"],
"with": "something",
}, },
) )
assert response.status_code == 200, response.text assert response.status_code == 200, response.text
@ -40,6 +42,7 @@ def test_send_all_data():
"lastname": "Sanchez", "lastname": "Sanchez",
"age": 70, "age": 70,
"tags": ["plumbus", "citadel"], "tags": ["plumbus", "citadel"],
"with": "something",
} }
@ -51,6 +54,7 @@ def test_defaults():
"lastname": "Sanchez", "lastname": "Sanchez",
"age": None, "age": None,
"tags": ["foo", "bar"], "tags": ["foo", "bar"],
"with": "nothing",
} }
@ -100,13 +104,13 @@ def test_no_data():
"type": "missing", "type": "missing",
"loc": ["body", "username"], "loc": ["body", "username"],
"msg": "Field required", "msg": "Field required",
"input": {"tags": ["foo", "bar"]}, "input": {"tags": ["foo", "bar"], "with": "nothing"},
}, },
{ {
"type": "missing", "type": "missing",
"loc": ["body", "lastname"], "loc": ["body", "lastname"],
"msg": "Field required", "msg": "Field required",
"input": {"tags": ["foo", "bar"]}, "input": {"tags": ["foo", "bar"], "with": "nothing"},
}, },
] ]
} }

Loading…
Cancel
Save