【问题标题】:How to write schema for nested objects and array while using swagger如何在使用 swagger 时为嵌套对象和数组编写模式
【发布时间】:2022-11-21 20:17:56
【问题描述】:

我正在尝试为具有嵌套对象和数组的 swagger api 文档编写一个模式。输出确实给出错误,但“未知类型:”。

我的节点 models.js 文件中的架构

大摇大摆的代码: `


  @swagger
  components:
      schema:
           Buyer:
              type: object
              properties:
                   id:
                       type: string
                   Buyer_name:
                       type: string
                   Buyer_Delivery_Address: 
                       type: object
                       properties:
                           address_line: 
                               type: String 
                           City: 
                               type:String 
                           Postal_Code: 
                               type:Number 
                           Country: 
                               type: String
                   Buyer_Phone:
                       type: Number
                   Buyer_Cart:
                       type: object
                       properties:
                           Product_ID: 
                               type: Number 
                           Product_Name: 
                               type:String 
                           Product_quantity: 
                               type:Number 
                           Product_Price:
                               type:Number  
 


  @swagger
  /buyer:
   get:
       summary: The get data from database  
       description: displaying all data from database
       responses:
           200:
               description: success fullydisplaying all data from database
               content:
                   application/json:
                       schema:
                           type: array
                           items:
                               $ref: '#components/schema/Buyer'
  
  

`

Swagger ui 上的输出

我想在嵌套字段中显示正确的类型。

【问题讨论】:

    标签: javascript node.js express swagger swagger-ui


    【解决方案1】:

    所以在玩了之后我发现,缩进是错误的,如果你有同样的问题尝试正确缩进

    其次,

    type: Object 和 type: object 不同 type: object 是有效的 与 字符串 -> 字符串(有效) 数字 -> 数字(有效)

    正确缩进的代码

    @swagger
       components:
           schema:
               Buyer:
                   type: object
                   properties:
                       id:
                           type: string
                       Buyer_name:
                           type: string
                       Buyer_Delivery_Address:
                           type: object
                           properties:
                               address_line:
                                   type: string 
                               City:
                                   type: string
                               Postal_Code:
                                   type: number
                               Country:
                                   type: string 

    招摇的用户界面输出:

    【讨论】:

      猜你喜欢
      • 2022-01-07
      • 2012-06-04
      • 2018-05-20
      • 1970-01-01
      • 1970-01-01
      • 2022-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多