Query
default value cannot be set in Annotated
when using APIRouter
#9279
-
First Check
Commit to Help
Example Code# main.py
from fastapi import FastAPI
from router import router
app = FastAPI()
app.include_router(router)
# router.py
from typing import Annotated
from fastapi import APIRouter, Query
router = APIRouter()
@router.get("/")
async def root(*, n: Annotated[float, Query()] = 50):
return {"n": n} DescriptionWhen starting the Uvicorn server using
Operating SystemLinux Operating System Details
FastAPI Version0.95.0 Python VersionPython 3.10.6 Additional ContextThis should work according to the documentation about default values. Also, it works when not using |
Beta Was this translation helpful? Give feedback.
Replies: 18 comments 38 replies
-
I have the same problem this happens also when the time is date and enum those endpoints are also in a router. I have this error message in both cases for the moment the solution is not using Annotated:
Working code
Failing code
Working code
Failing code
|
Beta Was this translation helpful? Give feedback.
-
I have the same problem, it depends on the ordering: If everything is in one file, the router can be added before defining the endpoint. from typing import Annotated
from fastapi import FastAPI, Header, APIRouter
app = FastAPI()
@app.get("/app")
async def read_items(user_agent: Annotated[str | None, Header()] = None):
return {"User-Agent": user_agent}
test_router = APIRouter()
# if the router is added before defining the route, it works fine
# app.include_router(test_router)
@test_router.get("/router")
async def read_items(user_agent: Annotated[str | None, Header()] = None):
return {"User-Agent": user_agent}
# breaks
app.include_router(test_router) |
Beta Was this translation helpful? Give feedback.
-
I am facing the same problem here. This only happens when endpoints use |
Beta Was this translation helpful? Give feedback.
-
Just want to put my 2 cents into this topic. Those dependencies don't work either: # function dependency
def common_parameters(
q: Annotated[str | None, Query()] = 'asd',
skip: Annotated[int, Query()] = 4,
limit: Annotated[int, Query()] = 5
):
return {"q": q, "skip": skip, "limit": limit}
# class as dependency
class CommonQueryParams:
def __init__(
self,
q: Annotated[str | None, Query()] = None,
skip: Annotated[int, Query()] = 0,
limit: Annotated[int, Query()] = 100
):
self.q = q
self.skip = skip
self.limit = limit
# dataclass as dependency (in order to get pregenerated __init__)
@dataclass
class CommonQueryParamsDC:
q: Annotated[str | None, Query()] = None
skip: Annotated[int, Query()] = 0
limit: Annotated[int, Query()] = 100 The same error message is returned. |
Beta Was this translation helpful? Give feedback.
-
Could be unrelated issue, I'm using @dataclass(config=CustomBaseModel.Config)
class PaginationGet:
page: int = Query(1, gt=0)
per_page: PerPage = Query(PerPage.VALUE_025)
all_items: bool = Query(False) This code does not work: @dataclass(config=CustomBaseModel.Config)
class PaginationGet:
page: Annotated[int, Query(gt=0)]
per_page: Annotated[PerPage, Query()]
all_items: Annotated[bool, Query()] The error is this: |
Beta Was this translation helpful? Give feedback.
-
Why this is in discussions when it seems a bug on the apirouter of latest fastapi ? |
Beta Was this translation helpful? Give feedback.
-
this is error issues. @tiangolo |
Beta Was this translation helpful? Give feedback.
-
This should solve it: #9315 |
Beta Was this translation helpful? Give feedback.
-
This issue also affects the non-annotated approach, probably limited to the decorator dependencies. Minimal example: from typing import Annotated
from fastapi import FastAPI, Header, Depends, APIRouter
app = FastAPI()
router = APIRouter()
def validate_header(x_test: Annotated[str | None, Header()] = None):
if x_test != "test":
raise Exception("Error")
@router.get("/", dependencies=[Depends(validate_header)])
async def root():
return {"message": "Hello World"}
app.include_router(router) From debugging it is caused by |
Beta Was this translation helpful? Give feedback.
-
Could this be related to pydantic/pydantic#4067? Maybe it’s worth trying to update Pydantic? |
Beta Was this translation helpful? Give feedback.
-
Is there a workaround in the meantime the merge comes? I am having the same issue I believe, specifically I get this error:
Thank you in advance and regards |
Beta Was this translation helpful? Give feedback.
-
I'm seeing this as well. The docs provide an example that causes the error. Just try to run the example here: https://fastapi.tiangolo.com/tutorial/path-params-numeric-validations/#import-path |
Beta Was this translation helpful? Give feedback.
-
Running into this as well. Definitely seems like a bug. |
Beta Was this translation helpful? Give feedback.
-
Can you all try your use cases with the fix from #9279 (comment)? If it works, confirm on this thread and if it doesn't note that too |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
It's been really long now. Is this problem even considered an issue? The docs of v0.95.0 make it seem like this is definitely expected to work but it doesn't. It would be great if either the proposed solution #9315 gets reviewed and merged, or docs get updated explicitly stating this limitation of the "Annotated approach". In case of latter, please remove the tip |
Beta Was this translation helpful? Give feedback.
-
I have been bitten by this as well, worst of all is that I was preparing a POC to introduce FastAPI in my company so that we could adopt it across the whole Python teams :/ I think having the docs with those directions and not working like this can be a put off for tech leads assessing the adoption of the library. |
Beta Was this translation helpful? Give feedback.
-
Hey, It seems this still isn't working. Using this code snippet I don't want to use current workaround Any suggestions how to solve this issue? FastAPI Version Python Version |
Beta Was this translation helpful? Give feedback.
This should solve it: #9315