首页 > 解决方案 > 如何定义具有一组固定有效值的数组?

问题描述

我在 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 中描述这样的字段的正确方法是什么?

标签: openapiswaggerhub

解决方案


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"
          ]
        }
      }
    }
  }


推荐阅读