-
First Check
Commit to Help
Example Codefrom typing import Annotated
from fastapi import Cookie, FastAPI
from pydantic import Field, RootModel
Key = RootModel[Annotated[str, Field(min_length=4)]]
app = FastAPI()
@app.post('/test/')
def test(key: Annotated[Key, Cookie()]):
passDescriptionI'm trying to use RootModel as a Cookie parameter, but I'm getting the following error: I'm guessing that it's not that easy to define what exactly a root model is, and it can't be used that way. Therefore, I will describe what I want to achieve. Our project has a certain str root model with many constraints, a simplified version is used in the code example. This root model is used as a nested model in several models, and now there is a need to use it in several fastapi endpoints. So my question is: can I reuse such a root model in both pydantic models and fastapi endpoints? And if this is not possible, is it possible to implement the same reusable entity without using the RootModel, or is the only solution to duplicate the code? Operating SystemLinux, Windows Operating System DetailsNo response FastAPI Version0.109.2 Pydantic Version2.5.1 Python VersionPython 3.12.0 Additional ContextNo response |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
|
Looks like this is currently an issue with all param types and root model |
Beta Was this translation helpful? Give feedback.
Looks like this is currently an issue with all param types and root model
#11101