swagger - Open API 3 - 如何描述一个整数数组但可以是单个项目的查询参数?
问题描述
使用 Open API 3.0.1,我试图描述一个“整数”类型的查询ids
参数,假设我们称之为它,它可以是单独的,也可以是一个数组。
例如:
/my-endpoint?ids=111
或者
/my-endpoint?ids=111&ids=222
我确实尝试过:
- name: ids
in: query
required: false
schema:
type: array
items:
type: integer
我明白这一点style: form
并且explode: true
是默认的。
但是当我使用实际请求验证这一点时(我使用express-openapi-validate,它是Ajv的包装器),我得到了这些错误:
/my-endpoint?ids=111
"Error while validating request: request.query.ids should be array"
/my-endpoint?ids=111&ids=222&ids=333
"Error while validating request: request.query.ids[0] should be integer"
如果我使用“字符串”而不是“整数”:
- name: ids
in: query
required: false
schema:
type: array
items:
type: string
/my-endpoint?ids=111
"Error while validating request: request.query.ids should be array"
/my-endpoint?ids=111&ids=222&ids=333
Valid!
我应该如何描述这个ids
参数,它必须是整数值?
更新:我现在明白,任何查询参数string
在被 Express 服务器(我使用)反序列化时都将是 a。但我仍然无法让单个元素数组工作!
解决方案
在@Helen 发表评论后,我确实尝试了另一个验证库express-openapi-validator,现在它适用于:
- name: ids
in: query
required: false
style: form
explode: true
schema:
type: array
items:
type: integer
使用express-openapi-validate,我能够使其工作的唯一方法是使用:
- name: ids
in: query
required: false
schema:
anyOf:
- type: array
items:
type: string
pattern: '^\d+$'
- type: string
pattern: '^\d+$'
所以我建议你在 Express 服务器上使用express-openapi-validator 。
推荐阅读
- html - 即使有边框,所有选项卡也保持相同的高度
- android-studio-3.1.3 - 检测到缺少的 Android 平台:“android-27”
- regex - 正则表达式引擎如何处理重复后的文字(即“。*”)?
- python - AttributeError:模块 'copy' 没有属性 'deepcopy'
- http - 为什么浏览器不尊重 Cache-Control 标头?
- spring - SpringCloud-Bus 不使用 webhook 客户端刷新
- android - 如何在 nativescript 中设置条纹样式背景
- python - 为什么 BS4(来自 BeautifulSoup)在使用 .select() 时只返回 []?
- mysql - MySQL - 如何在 select 语句中设置变量值?
- python - python矩阵优化