简体   繁体   中英

How to write schema for nested objects and array while using swagger

I am trying to write a schema for swagger api docs which have nested objects and arrays. the output does give error but " unknow type: ".

The schema i have in my node models.js file

图式

The swagger code: `


  @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'
  
  

`

The Output on Swagger ui

swagger UI 上的输出

i want to display the proper types in nested fields.

So after playing around i found out, the indentation were wrong,if you are having same problem try that to indent properly

secondly,

type: Object and type: object are different type: object is valid same with String -> string(valid) Number -> number(valid)

correctly indented code

 @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

在此处输入图像描述

the swagger Ui output: 输出

在此处输入图像描述

I think the part you have messed up is the case you have used for type declaration. Use "number" instead of "Number" and "string" instead of "String"

components:
  schemas:
    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 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM