gpt4 book ai didi

python-3.x - Swagger/ flask : how to handle query parameters with dashes

转载 作者:行者123 更新时间:2023-12-04 10:02:43 27 4
gpt4 key购买 nike

我使用 YAML 定义文件使用 Swagger/OpenAPI 生成了一个 Flask 服务器。 GET 端点的摘录:

 parameters:
- in: query
name: project-name
required: true
schema:
type: string
description: name of the project
- in: query
name: file-name
required: true
schema:
type: string
description: name of the origin file of the dataset
- in: query
name: null-management
required: true
schema:
type: string
description: value to use to fill null values in datasets
- in: query
name: category-name
schema:
type: string
description: name of the first level category
- in: query
name: filter-multi-occurrence
schema:
type: boolean
description: if multi occurrences are to be filtered or not

问题是我不能用 Python/Flask Controller 映射它,因为变量应该是这样的
def dataset_add(project_name: str, file_name: str, null_management: str, first_level_category_name: str = None,
filter_multi_occurrence: str = None) -> str: # noqa: E501

... Python 的变量名中不允许有破折号。但我不想在 GET 查询中使用下划线……我如何在 Flask 中解决这个问题?

最佳答案

您实际上可以做到这一点,但需要使用一种解决方法。

首先你需要更新swagger/swagger.yaml在生成的代码中,并确保所有参数名称都有 -而不是 _
然后你需要更新你的函数并删除 _从参数名称

def dataset_add(projectname: str, filename: str, nullmanagement: str, firstlevelcategoryname: str = None,
filtermultioccurrence: str = None) -> str: # noqa: E501

Working fine

关于python-3.x - Swagger/ flask : how to handle query parameters with dashes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61748775/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com