如何用一组固定的有效值定义一个数组?

问题描述

我在OpenAPI中定义此字段时遇到麻烦。我有一个架构,该架构的字段可以包含零个或多个字符串的数组, 像这样{ "daysOfWeek": ["Monday","Wednesday","Friday"] } 或这个{ "daysOfWeek": ["Sunday","Monday","Tuesday","Wednesday"] } 或这个{ "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","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"
          ]
        }
      }
    }
  }