在使用 swagger2 (openAPI) 构建 rest api 时,我希望允许查询参数 station_id 支持以下内容:
- ?station_id=23(返回站 23)
- ?station_id=23,45(返回站 23 和 45)
- ?station_id=[3:14](返回站 3 到 14)
- ?station_id=100%(%s 充当通配符,因此返回 1001、10049 等内容)
我使用以下招摇定义(字符串数组)来尝试完成此操作:
parameters:
- name: station_id
in: query
description: filter stations by station_id
required: false
type: array
items:
type: string
使用此定义,除了 ?station_id=23 之外,所有前面的示例都可以工作,因为 swagger 验证失败并显示以下消息:
{
"message": "Validation errors",
"errors": [
{
"code": "INVALID_REQUEST_PARAMETER",
"errors": [
{
"code": "INVALID_TYPE",
"params": [
"array",
"integer"
],
"message": "Expected type array but found type integer",
"path": [],
"description": "filter stations by station_id"
}
],
"in": "query",
"message": "Invalid parameter (station_id): Value failed JSON Schema validation",
"name": "station_id",
"path": [
"paths",
"/stations",
"get",
"parameters",
"0"
]
}
]
}
请注意,如果我引用 station_id 像 ?station_id='23' 验证通过,我会得到正确的响应。但我真的不想使用引号。像联合类型这样的东西可以帮助解决这个问题,但据我所知,它们不受支持。
我还有另一个端点 /stations/{id} 可以处理单个 id 的情况,但仍然有许多其他(非主键)数字字段我想以上面指定的方式过滤。例如 station_latitude。
任何解决方法的想法 - 也许我可以以某种方式使用模式(正则表达式)?如果在 swagger 定义中没有解决方法,是否有办法调整或绕过验证器?这是一个使用swagger-node的 nodejs 项目,我已将swagger-express-mw的版本升级到 0.7.0。