简体   繁体   English

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

[英]How to define Object with multiple properties including an array of objects in OpenAPI?

I am struggling to define a request body in OpenApi.我正在努力在 OpenApi 中定义请求正文。 I want to force the user to send me data in the following shape:我想强制用户向我发送以下形状的数据:

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

This is what I tried but it does not seem to work:这是我尝试过的,但似乎不起作用:

 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

Any help appreciated.任何帮助表示赞赏。 Thank you谢谢

With the currect json I couldn't make such sense but created this.使用正确的 json 我无法理解,但创建了这个。 This created a json which replicates yours.这创建了一个复制您的 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 OpenAPI 中定义嵌套数组? - How to define a nested array in OpenAPI? OpenApi 3.0:如何根据 object 中用作该数组的元素模式的键(属性)定义数组元素的唯一性? - OpenApi 3.0: How to define uniqueness of array elements based upon a key (property) in object that is used as an element schema for that array? 在 openAPI 3.1 中定义精确的自定义属性 - Define exact custom Properties in openAPI 3.1 如何定义对象数组作为参数? - How to define array-of-objects as parameter? 如何为多类型数组定义 json schma - How to define json schma for multiple type array 如何在Swagger中定义包含复杂对象数组的示例请求正文? - How to define an example request body containing an array of complex objects in Swagger? 在 OpenAPI 3.1.0 中枚举恰好 2 个(略有不同)对象的数组? - Enumerating an array of exactly 2 (slightly different) objects in OpenAPI 3.1.0? 如何组合(包括嵌套数组值)两个 serde_yaml::Value 对象? - how to combine (including nested array values) two serde_yaml::Value objects? 如何在 OpenAPI YAML 中为已定义组件的数组设置示例值? - How to set EXAMPLE value for array of a defined component in OpenAPI YAML? 如何在没有数组的情况下拥有多个无限对象 - Open API / Swagger - How to have multiple infinite objects without an array - Open API / Swagger
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM