Skip to content

Commit cd6c97b

Browse files
committed
Fix issues with recent PRs
This fixes some linting issues and restructures some code to be consistent with the code around it.
1 parent ecce16a commit cd6c97b

File tree

4 files changed

+44
-12
lines changed

4 files changed

+44
-12
lines changed

src/quart_schema/conversion.py

+10-3
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ class MsgSpecValidationError(Exception): # type: ignore
7373

7474
T = TypeVar("T", bound=Model)
7575

76-
JsonSchemaMode = Literal['validation', 'serialization']
76+
JsonSchemaMode = Literal["validation", "serialization"]
7777

7878

7979
def convert_response_return_value(
@@ -186,9 +186,16 @@ def model_load(
186186
raise exception_class(error)
187187

188188

189-
def model_schema(model_class: Type[Model], *, preference: Optional[str] = None, schema_mode: JsonSchemaMode = "validation") -> dict:
189+
def model_schema(
190+
model_class: Type[Model],
191+
*,
192+
preference: Optional[str] = None,
193+
schema_mode: JsonSchemaMode = "validation",
194+
) -> dict:
190195
if _use_pydantic(model_class, preference):
191-
return TypeAdapter(model_class).json_schema(ref_template=PYDANTIC_REF_TEMPLATE, mode=schema_mode)
196+
return TypeAdapter(model_class).json_schema(
197+
ref_template=PYDANTIC_REF_TEMPLATE, mode=schema_mode
198+
)
192199
elif _use_msgspec(model_class, preference):
193200
_, schema = schema_components([model_class], ref_template=MSGSPEC_REF_TEMPLATE)
194201
return list(schema.values())[0]

src/quart_schema/validation.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -145,10 +145,13 @@ async def wrapper(*args: Any, **kwargs: Any) -> Any:
145145
if source == DataSource.JSON:
146146
data = await request.get_json()
147147
else:
148-
data = (await request.form).to_dict(flat=False)
149-
for key, value in data.items():
150-
if len(value) == 1:
151-
data[key] = value[0]
148+
data = {}
149+
form = await request.form
150+
for key in form:
151+
if len(form.getlist(key)) > 1:
152+
data[key] = form.getlist(key)
153+
else:
154+
data[key] = form[key]
152155
if source == DataSource.FORM_MULTIPART:
153156
files = await request.files
154157
for key in files:

tests/test_openapi.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
from typing import Dict, List, Optional, Tuple, Type
2-
from typing_extensions import Annotated
32

43
import pytest
5-
from pydantic import BaseModel, Field, computed_field
4+
from pydantic import BaseModel, computed_field, Field
65
from pydantic.dataclasses import dataclass
7-
from pydantic.functional_serializers import PlainSerializer
86
from quart import Quart
97

108
from quart_schema import (
@@ -273,13 +271,13 @@ class EmployeeWithComputedField(BaseModel):
273271
first_name: str
274272
last_name: str
275273

276-
@computed_field
274+
@computed_field # type: ignore[misc]
277275
@property
278276
def full_name(self) -> str:
279277
return f"{self.first_name} {self.last_name}"
280278

281279

282-
async def test_response_model_with_computed_field():
280+
async def test_response_model_with_computed_field() -> None:
283281
"""
284282
Test that routes returning a response model that has one or more computed fields have the
285283
appropriate properties in the generated JSON schema.

tests/test_validation.py

+24
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,30 @@ async def item(data: Any) -> ResponseReturnValue:
135135
assert response.status_code == status
136136

137137

138+
class MultiItem(BaseModel):
139+
multi: List[int]
140+
single: int
141+
142+
143+
async def test_request_form_validation_multi() -> None:
144+
app = Quart(__name__)
145+
QuartSchema(app)
146+
147+
@app.route("/", methods=["POST"])
148+
@validate_request(MultiItem, source=DataSource.FORM)
149+
async def item(data: MultiItem) -> MultiItem:
150+
return data
151+
152+
test_client = app.test_client()
153+
response = await test_client.post(
154+
"/",
155+
data=b"multi=1&multi=2&single=2",
156+
headers={"Content-Type": "application/x-www-form-urlencoded"},
157+
)
158+
assert response.status_code == 200
159+
assert await response.get_json() == {"multi": [1, 2], "single": 2}
160+
161+
138162
async def test_request_file_validation() -> None:
139163
app = Quart(__name__)
140164
QuartSchema(app)

0 commit comments

Comments
 (0)