Home > Software engineering >  How to write schema for nested objects and array while using swagger
How to write schema for nested objects and array while using swagger

Time:11-21

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 schema

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

The output on swagger UI

i want to display the proper types in nested fields.

CodePudding user response:

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 

enter image description here

the swagger Ui output: the output

CodePudding user response:

enter image description here

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 
  • Related