Skip to content

Commit f9f5dac

Browse files
committed
Fix list with default value
1 parent 457c8db commit f9f5dac

File tree

3 files changed

+50
-3
lines changed

3 files changed

+50
-3
lines changed

flask_openapi3/request.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def _validate_query(query: Type[BaseModel], func_kwargs):
4141
value = request_args.getlist(v.alias or k) or request_args.getlist(k)
4242
else:
4343
value = request_args.get(v.alias or k) or request_args.get(k) # type:ignore
44-
if value is not None:
44+
if value is not None and value != []:
4545
query_dict[k] = value
4646
func_kwargs["query"] = query.model_validate(obj=query_dict)
4747

@@ -69,7 +69,7 @@ def _validate_form(form: Type[BaseModel], func_kwargs):
6969
value = json.loads(_value) # type:ignore
7070
except (JSONDecodeError, TypeError):
7171
value = _value # type:ignore
72-
if value is not None:
72+
if value is not None and value != []:
7373
form_dict[k] = value
7474
func_kwargs["form"] = form.model_validate(obj=form_dict)
7575

tests/test_list_with_default_value.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# -*- coding: utf-8 -*-
2+
# @Author : llc
3+
# @Time : 2024/9/29 10:36
4+
from typing import List
5+
6+
import pytest
7+
from pydantic import BaseModel
8+
9+
from flask_openapi3 import OpenAPI
10+
11+
app = OpenAPI(__name__)
12+
app.config["TESTING"] = True
13+
14+
15+
@pytest.fixture
16+
def client():
17+
client = app.test_client()
18+
19+
return client
20+
21+
22+
class BookQuery(BaseModel):
23+
age: List[int] = [1, 2]
24+
25+
26+
class BookForm(BaseModel):
27+
age: List[float] = [3, 4]
28+
29+
30+
@app.get("/query")
31+
def api_query(query: BookQuery):
32+
assert query.age == [1, 2]
33+
return {"code": 0, "message": "ok"}
34+
35+
36+
@app.post("/form")
37+
def api_form(form: BookForm):
38+
assert form.age == [3, 4]
39+
return {"code": 0, "message": "ok"}
40+
41+
42+
def test_query(client):
43+
client.get("/query")
44+
45+
46+
def test_form(client):
47+
client.post("/form")

tests/test_request.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ def api_raw(raw: BookRaw):
106106

107107

108108
def test_query(client):
109-
r = client.get("/query")
109+
r = client.get("/query?age=1")
110110
print(r.json)
111111
assert r.status_code == 200
112112

0 commit comments

Comments
 (0)