FastAPI后台开发基础(4):FastAPI官网文档错误、编码错误以及注意要点记录
设置查询参数是否为必填 使用Annotated装饰查询参数 不设置default值 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 @app.get("/validation") async def async_root(str_param: Annotated[str | None, Query(min_length = 3, max_length = 20, pattern = '^[a-zA-Z0-9]+$', description = 'This is a string parameter')]): """ 如果不设置 str_param = None, 则 str_param 为必填 """ ret = {"str_list": str_param} if str_param is not None: h = hashlib.sha3_512(str_param.encode('utf-8')).hexdigest() ret.update({"sha3_512": h}) else: ret.update({"uuid": uuid.uuid4().hex}) return ret 不设置默认值 生成的文档中参数为必填 ...