nilan59
nilan59

Reputation: 1166

Supporting Complex Objects in Swagger

Your help in this will be highly appreciated.

I am using json and swagger version 2.0.39. If my rest service demand a dynamic complex array like following

eg:- 
addressList:[
{
lane:abc
city:colombo
},
{
lane:def
city:colarado
}
]

addressList can contain one or many (dynamic) addresses

How can I write my swagger file to support this structure. How will it get populated if I use swagger UI. I think there should be a predefined swagger way to support something like this. Isn't it? I tried various alternatives and only option I have is customization which I don't believe will be a easy task for me.

Your help will be highly appreciated. Thank you.

Upvotes: 2

Views: 763

Answers (1)

Mike
Mike

Reputation: 11545

Swagger does support this functionality. You will need to define the addressList field in your model similar to this:

"addressList":{
    "type":"array",
    "items":{
         "$ref": "#/definitions/dynamic_addresses",
     }
 }

Where #/definitions/dynamic_addresses references a model that describes your dynamic addresses model in the addressList array.

Take a look at the Swagger Docs to understand more about Swagger model specs and nested definitions.

Upvotes: 2

Related Questions