【问题标题】:Using multiple Swagger definitions at root of object在对象的根部使用多个 Swagger 定义
【发布时间】:2016-12-24 10:55:28
【问题描述】:

我正在尝试创建一个如下所示的响应对象:

(例如简化)

{
"data1": "1234"
"data2" : "445"
}

我有两个定义:

 obj1: 
  type: object 
  properties: 
   data1:
    type: string

 obj2:
  type: object 
  properties: 
   data2:
    type: string

然后是第三个定义,如下所示:

  Main:
    type: object
    allOf:
      - $ref: "#/definitions/ob1"
      - $ref: "#/definitions/obj2"

我不确定这是否是在 Main 对象的基础上合并 Obj1 和 Obj 2 的正确方法

Swagger UI 显示以下内容

Responses

Code    Description Schema
200  Success ⇄  
Main {
all of:
obj1 { }
obj2 { }
}

我不清楚的是它是否会将这些对象设置为根,或者它的声明 2 个对象是否会在响应中???

我假设它是正确的,希望有人可以确认。

【问题讨论】:

标签: yaml swagger


【解决方案1】:

Swagger allows combining and extending model definitions using the allOf property of JSON Schema, in effect offering model composition. allOf takes in an array of object definitions that are validated independently but together compose a single object.

据此 - Swagger UI 显示了两个对象,它们实际上组合成一个具有组合属性的对象。你可以自己试试:

StringObj:
  type: object
  properties:
    stringId:
      type: string
IntegerObj:
  type: object
  properties:
    integerId:
      type: integer
Composed:
  description: A representation of a dog
  allOf:
  - $ref: '#/definitions/StringObj'
  - $ref: '#/definitions/IntegerObj'

如果您在方法的主体中使用Composed 并使用try this operation Swagger 创建 json:

{
  "stringId": "aaa",
  "integerId": 123
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-15
    • 1970-01-01
    • 2017-03-20
    • 1970-01-01
    • 1970-01-01
    • 2018-03-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多