如何在OpenAPI中定义具有多个属性(包括对象数组)的Object?

时间:2020-08-12 17:24:47

标签: yaml openapi

我正在努力在OpenApi中定义请求正文。我想强制用户向我发送以下形式的数据:

{
    "test": {
        "a": "a",
        "b": "b",
        "abc": [
            {
                "type": "abc",
                "name": "name"
            },
            {
                "type": "abc",
                "name": "name"
            }
            ...more
        ]
    }
}

这是我尝试过的方法,但似乎不起作用:

 schemas:
    SampleRequest:
      required:
        - abc
      properties:
        abc: 
          $ref: "#/components/schemas/ABCType"

    ABCType:
      type: object
      required:
        - test
      properties: 
        test: 
          type: array 
          items:
            type: object
        properties: 
          type: 
            type: string
          name:
            type: string

任何帮助表示赞赏。谢谢

1 个答案:

答案 0 :(得分:2)

使用当前的json,我无法理解,但创建了它。这创建了一个复制您的json。

ABCType:
  type: object
  additionalProperties: false
  required: 
  - a
  - b
  - abc
  properties:
    a:
      description: A of ABCType
      type: string
    b:
      description: B of ABCType
      type: string
    abc:
      description: Array of ABCType
      type: array
      items:
        $ref: '#/components/schemas/ABCArrayValue'
ABCArrayValue:
  type: object
  additionalProperties: false
  required:
  - type
  - name
  properties:
    type:
      description: Type of this array value
      type: string
    name:
      description: Name of this array value
      type: string