Litestar supports query parameters that are validated as Pydantic models. As seen in the code under "Basic Example", validation of query parameters do indeed work as per the constraints set in the Pydantic model. While the route works as intended, the OpenAPI schema generated has no query parameters.
"paths": {
"/": {
"get": {
"summary": "Foo",
"operationId": "Foo",
"responses": {
"200": {
"description": "Request fulfilled, document follows",
"headers": {},
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/EvenOdd"
}
}
}
}
},
"deprecated": false
}
}
}
from typing import Annotated
from litestar import Litestar, get
from pydantic import BaseModel, Field
class EvenOdd(BaseModel):
even: Annotated[str, Field(regex=r"^\d*[02468]$")]
odd: Annotated[str, Field(regex=r"^\d*[13579]$")]
@get()
async def foo(query: EvenOdd) -> EvenOdd:
return query
app = Litestar([foo])
from litestar.testing import TestClient
with TestClient(app) as test:
data = {'even': 0, 'odd': 1}
response = test.get('', params=data)
# query parameter validation works as expected
assert response.status_code == 200
data = {'even': 1, 'odd': 0}
response = test.get('', params=data)
# query parameter validation fails as expected
assert response.status_code == 400
assert 'Validation failed for GET' in response.json()['detail']
Users who are migrating from other frameworks could expect the same behavior.
There is no "parameters"
present in the schema. Is this intended behavior, or can the schema generation be changed so that "pydantic models as query parameters" work?
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too