Update
You could now wrap the Query() in a Field(), which would allow you to define a Pydantic List field that will be interpreted as query parameter:
from fastapi import FastAPI, Query, Depends
from pydantic import BaseModel, Field
from typing import List, Optional
app = FastAPI()
class SortModel(BaseModel):
field: Optional[str]
directions: List[str] = Field(Query(...))
@app.get("/")
async def get_data(criteria: SortModel = Depends()):
return criteria
See this answer for more details and another working example.
Original answer
It is not, as yet, possible to use a GET request with Pydantic List field as query parameter. When you declare a List field in the Pydantic model, it is interpreted as a request body parameter, instead of a query one (regardless of using Depends()—you can check that through Swagger UI docs at http://127.0.0.1:8000/docs, for instance). Additionally, as you are using a GET request, even if you added the List of directions in the body and attempted sending the request, it wouldn't work, as a POST request would be required for that operation.
The way to do this is to either define the List of directions explicitly with Query as a separate parameter in your endpoint, or implement your query parameter-parsing in a separate dependency class, as described here. Remember again to define the List field explicitly with Query, so that directions can be interpreted as a query parameter and appear multiple times in the URL (in others words, to receive multiple values). Example:
from typing import List, Optional
from fastapi import FastAPI, Depends, Query
app = FastAPI()
class SortModel:
def __init__(
self,
field: Optional[str],
directions: List[str] = Query(...)
):
self.field = field
self.directions = directions
@app.get("/")
async def get_data(criteria: SortModel = Depends()):
return criteria
The above can be re-written using the @dataclass decorator, as shown below:
from typing import List, Optional
from fastapi import FastAPI, Depends, Query
from dataclasses import dataclass
app = FastAPI()
@dataclass
class SortModel:
field: Optional[str]
directions: List[str] = Query(...)
@app.get("/")
async def get_data(criteria: SortModel = Depends()):
return criteria