by Anonymous » 16 Apr 2025, 09:21
Ich habe einen Endpunkt, der ein pydantisches Modell nimmt, Foo als Abfrageparameter.
Code: Select all
from typing import Annotated
import uvicorn
from fastapi import FastAPI, Query
from pydantic import BaseModel
app = FastAPI()
class Foo(BaseModel):
bar: str
baz: dict[str, str]
@app.get("/")
def root(foo: Annotated[Foo, Query()]):
return foo
if __name__ == "__main__":
uvicorn.run("test:app")
< /code>
Ich definiere meine Abfrageparameter mit Swagger, sodass die Codierung korrekt sein sollte. Ich weiß, dass die Baz
Param -Syntax überflüssig aussieht, weil ich ein Wörterbuch verschachtelt habe, aber das Parsen auch ohne Nesting fällt. Endpunkt ... < /p>
Code: Select all
curl -X 'GET' \
'http://127.0.0.1:8000/?bar=sda&baz=%7B%22abc%22%3A%22def%22%7D' \
-H 'accept: application/json'
Fastapi scheint in foo.baz korrekt zu lesen, Rückgabe
Code: Select all
{
"detail": [
{
"type": "dict_type",
"loc": [
"query",
"baz"
],
"msg": "Input should be a valid dictionary",
"input": "{\"abc\":\"def\"}"
}
]
}
Ich habe ähnliche Fragen gelesen und ich weiß, dass ich das Wörterbuch durch den Zugriff auf DICT (Request.Query_params) aufnehmen kann, aber dies umgeht Fastapis Validierung und ich würde es vorziehen, das Endpunkt einfach und konsistent mit dem Rest meines Codebases zu halten, indem ich das Parammodell als Pydantic -Modell aufnehme. Abfrageparam?
Ich habe einen Endpunkt, der ein pydantisches Modell nimmt, Foo als Abfrageparameter.[code]from typing import Annotated
import uvicorn
from fastapi import FastAPI, Query
from pydantic import BaseModel
app = FastAPI()
class Foo(BaseModel):
bar: str
baz: dict[str, str]
@app.get("/")
def root(foo: Annotated[Foo, Query()]):
return foo
if __name__ == "__main__":
uvicorn.run("test:app")
< /code>
Ich definiere meine Abfrageparameter mit Swagger, sodass die Codierung korrekt sein sollte. Ich weiß, dass die Baz [/code] Param -Syntax überflüssig aussieht, weil ich ein Wörterbuch verschachtelt habe, aber das Parsen auch ohne Nesting fällt. Endpunkt ... < /p>
[code]curl -X 'GET' \
'http://127.0.0.1:8000/?bar=sda&baz=%7B%22abc%22%3A%22def%22%7D' \
-H 'accept: application/json'
[/code]
Fastapi scheint in foo.baz korrekt zu lesen, Rückgabe
[code]{
"detail": [
{
"type": "dict_type",
"loc": [
"query",
"baz"
],
"msg": "Input should be a valid dictionary",
"input": "{\"abc\":\"def\"}"
}
]
}
[/code]
Ich habe ähnliche Fragen gelesen und ich weiß, dass ich das Wörterbuch durch den Zugriff auf DICT (Request.Query_params) aufnehmen kann, aber dies umgeht Fastapis Validierung und ich würde es vorziehen, das Endpunkt einfach und konsistent mit dem Rest meines Codebases zu halten, indem ich das Parammodell als Pydantic -Modell aufnehme. Abfrageparam?