openapi - 如何定义具有一组固定有效值的数组?
问题描述
我在 OpenAPI 中定义此字段时遇到问题。我有一个模式,其中的字段可以包含零个或多个字符串的数组,例如 this{ "daysOfWeek": ["Monday", "Wednesday", "Friday"] }
或 this{ "daysOfWeek": ["Sunday", "Monday", "Tuesday", "Wednesday"] }
或 this { "daysOfWeek": []}
。
以下架构定义在 SwaggerHub 中为每个枚举元素生成此警告:enum value should conform to its schema's type
。
"SampleSchema": {
"type": "object",
"properties": {
"daysOfWeek": {
"description": "An array of zero or more days of the week",
"type": "array",
"items": {
"type": "string"
},
"enum": [
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday"
]
}
}
}
更改items.type
为“数组”会产生相同的警告。
在 OpenAPI 中描述这样的字段的正确方法是什么?
解决方案
该enum
字段引用数组项,因此它应该是items
对象的一部分:
"SampleSchema": {
"type": "object",
"properties": {
"daysOfWeek": {
"description": "An array of zero or more days of the week",
"type": "array",
"items": {
"type": "string",
"enum": [
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday"
]
}
}
}
}
推荐阅读
- c# - 在来自 android 的 WCF 多部分数据图像中,特定图像未从服务器获得任何响应(已上传 20 张图像中的 15 张)
- python - sqlalchemy.exc.OperationalError:无法在 docker 中连接到 mysql
- if-statement - 如何使用主动下拉发布以过滤显示的数据?
- java - 如何使用“预测”Sgnature Def 在 Java 中加载 Tensorflow SavedModel?
- sql-server - 如何显示 DISTINCT 值 Group Wise
- c# - 在 .NET 核心中返回 HttpResponseMessage 时出错
- android - 更新到 androidx.appcompat:appcompat:1.1.0 后的语言更改问题
- hibernate - 在 Quarkus 中添加 Hibernate ORM Panache 时在测试中发生异常
- javascript - 设置动画持续时间的时刻
- unity3d - 统一为游戏视图添加分辨率