如何在 FastAPI 中禁用模式检查?
How to disable schema checking in FastAPI?
我正在将服务从 Flask 迁移到 FastAPI 并使用 Pydantic 模型生成文档。但是,我对模式检查有点不确定。恐怕会有一些意外的数据(比如不同的字段格式)并且会 return 出错。
在 Pydantic 文档中,有一些方法可以在不检查架构的情况下创建模型:https://pydantic-docs.helpmanual.io/usage/models/#creating-models-without-validation
但是,由于这显然是由 FastAPI 本身实例化的,所以我不知道如何在从 FastAPI returning 时禁用此架构检查。
你可以 return Response directly, or with using custom responses for automatic convertion. In this case, response data is not validated against the response model. But you can still document it as described in Additional Responses in OpenAPI.
示例:
class SomeModel(BaseModel):
num: int
@app.get("/get", response_model=SomeModel)
def handler(param: int):
if param == 1: # ok
return {"num": "1"}
elif param == 2: # validation error
return {"num": "not a number"}
elif param == 3: # ok (return without validation)
return JSONResponse(content={"num": "not a number"})
elif param == 4: # ok (return without validation and conversion)
return Response(content=json.dumps({"num": "not a number"}), media_type="application/json")
FastAPI 不强制执行任何类型的验证,因此如果您不想要它,请不要使用 Pydantic 模型或类型提示。
app.get('/')
async def your_function(input_param):
return { 'param': input_param }
# Don't use models or type hints when defining the function params.
# `input_param` can be anything, no validation will be performed.
但是,正如@Tryph 正确指出的那样,由于您正在使用 Pydantic 生成文档,因此您可以像这样简单地使用 Any
类型:
from typing import Any
from pydantic import BaseModel
class YourClass(BaseModel):
any_value: Any
注意 Any
类型也接受 None
,实际上该字段是可选的。 (另请参阅 Pydantic docs 中的 typing.Any
)
您可以将请求模型设置为 typing.Dict
或 typing.List
from typing import Dict
app.post('/')
async def your_function(body: Dict):
return { 'request_body': body}
我正在将服务从 Flask 迁移到 FastAPI 并使用 Pydantic 模型生成文档。但是,我对模式检查有点不确定。恐怕会有一些意外的数据(比如不同的字段格式)并且会 return 出错。
在 Pydantic 文档中,有一些方法可以在不检查架构的情况下创建模型:https://pydantic-docs.helpmanual.io/usage/models/#creating-models-without-validation
但是,由于这显然是由 FastAPI 本身实例化的,所以我不知道如何在从 FastAPI returning 时禁用此架构检查。
你可以 return Response directly, or with using custom responses for automatic convertion. In this case, response data is not validated against the response model. But you can still document it as described in Additional Responses in OpenAPI.
示例:
class SomeModel(BaseModel):
num: int
@app.get("/get", response_model=SomeModel)
def handler(param: int):
if param == 1: # ok
return {"num": "1"}
elif param == 2: # validation error
return {"num": "not a number"}
elif param == 3: # ok (return without validation)
return JSONResponse(content={"num": "not a number"})
elif param == 4: # ok (return without validation and conversion)
return Response(content=json.dumps({"num": "not a number"}), media_type="application/json")
FastAPI 不强制执行任何类型的验证,因此如果您不想要它,请不要使用 Pydantic 模型或类型提示。
app.get('/')
async def your_function(input_param):
return { 'param': input_param }
# Don't use models or type hints when defining the function params.
# `input_param` can be anything, no validation will be performed.
但是,正如@Tryph 正确指出的那样,由于您正在使用 Pydantic 生成文档,因此您可以像这样简单地使用 Any
类型:
from typing import Any
from pydantic import BaseModel
class YourClass(BaseModel):
any_value: Any
注意 Any
类型也接受 None
,实际上该字段是可选的。 (另请参阅 Pydantic docs 中的 typing.Any
)
您可以将请求模型设置为 typing.Dict
或 typing.List
from typing import Dict
app.post('/')
async def your_function(body: Dict):
return { 'request_body': body}