Forum Discussion

KinnarChowdhury's avatar
KinnarChowdhury
New Contributor
5 years ago

How do i define a nested class json body in a swagger file version 3.0.1

Sample of a response body

Response body
{ "admn_user_id": 55, "admindetails": { "userdetail_id": 02, "firstname": "example1", "lastname": "example2", "contact_phone": "7777788888", "admn_partner_id": 12, "updated_on": "2020-01-20T10:54:51Z", "email_id": "abc@foo.com", "admn_user": 55, "region": 2, "region_name": "At02" }, "user_type": 2, "username": "sou1", "user_status": 3, "role_id": 2, "role_name": "ABC", "user_status_name": "Active" },

How do I design a swagger components/schema for the above response body for a post method.
Give me any link to understand the problem.
Can I pass a schema model inside another using $ref?


  • KinnarChowdhury wrote:

    Can I pass a schema model inside another using $ref?


    Yes! Here's how:

    components:
      schemas:
    ParentSchema: type: object properties: admn_user_id: type: object example: 55 admindetails: $ref: '#/components/schemas/AdminDetails' # <------- ...
    AdminDetails: type: object properties: userdetail_id: type: integer example: 2 firstname: type: string ...
  • HKosova's avatar
    HKosova
    SmartBear Alumni (Retired)

    KinnarChowdhury wrote:

    Can I pass a schema model inside another using $ref?


    Yes! Here's how:

    components:
      schemas:
    ParentSchema: type: object properties: admn_user_id: type: object example: 55 admindetails: $ref: '#/components/schemas/AdminDetails' # <------- ...
    AdminDetails: type: object properties: userdetail_id: type: integer example: 2 firstname: type: string ...
    • KinnarChowdhury's avatar
      KinnarChowdhury
      New Contributor

      Thanks for the solution, I understood the logic in this case. I appreciate your help.