failed NO_PATCH NO_PATCH UNSUBMITTED empty_patch(None) · None tool calls · 0 s · fastapi/fastapi
🐛 Fix using `Json[list[str]]` type (issue #10997)
Fixes Form() not working with Pydantic `Json[T]` types when T is a sequence type (list, set, tuple).
Closes #10997
When using `Form()` with `Json[list[str]]`, FastAPI incorrectly treated it as a sequence field and used `getlist()`, wrapping the JSON string in a list. This caused Pydantic to fail parsing with:
```
TypeError: JSON input should be string, bytes or bytearray
```
**Before fix:**
```python
# Client sends
data = {"items": '["abc", "def"]'} # JSON string
# FastAPI incorrectly does
value = values.getlist("items") # ['["abc", "def"]'] ❌
```
**After fix:**
```python
# FastAPI correctly does
value = values.get("items") # '["abc", "def"]' ✅
```
This fix enables legitimate use cases like file uploads with JSON metadata:
```python
@app.post("/upload")
def upload(
file: UploadFile,
tags: Annotated[Json[list[str]], Form()]
):
return {"file": file.filename, "tags": tags}
```
Pydantic v1 support for `Json[T]` with `Form()` is not included in this PR because `metadata` attribute does not exist in Pydantic v1's `FieldInfo`.
## Related
- Issue #10997| # | Tool | Arguments | Result |
|---|---|---|---|
| No trace captured. | |||
F
=================================== FAILURES ===================================
_____________________________ test_form_json_list ______________________________
def test_form_json_list():
response = client.post(
"/form-json-list", data={"items": json.dumps(["abc", "def"])}
)
> assert response.status_code == 200, response.text
E AssertionError: {"detail":[{"type":"json_type","loc":["body","items"],"msg":"JSON input should be string, bytes or bytearray","input":["[\"abc\", \"def\"]"]}]}
E assert 422 == 200
E + where 422 = <Response [422 Unprocessable Entity]>.status_code
tests/test_json_type.py:38: AssertionError
=============================== warnings summary ===============================
../../../../../../../Users/jp/repos/kaggle-gemini-coding-agent-post-training/.envs/overlays/starlette-0.50.0-py3-none-any/starlette/testclient.py:45
/Users/jp/repos/kaggle-gemini-coding-agent-post-training/.envs/overlays/starlette-0.50.0-py3-none-any/starlette/testclient.py:45: DeprecationWarning: The anyio.abc.BlockingPortal alias is deprecated, use anyio.from_thread.BlockingPortal instead.
_PortalFactoryType = Callable[[], AbstractContextManager[anyio.abc.BlockingPortal]]
-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
!!!!!!!!!!!!!!!!!!!!!!!!!! stopping after 1 failures !!!!!!!!!!!!!!!!!!!!!!!!!!!
1 failed, 1 warning in 0.59s