2

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.

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Yeah @Mike thats what i was thinking. How will it be populated in swagger UI. Specially when its dynamic. How to insert addresses dynamically in swagger UI form. And also mike how can i map this model to my json structure. (Can you also add that part of swagger file too) Many Thanks for the help
Swagger UI will just show a reference to an array containing these objects. It doesn't matter if it's dynamic, the UI will not be showing the number of objects in the array or anything like that because it doesn't know until you make the request. Just expect it to show a reference to an array of the dynamic address objects. Also, model in this answer does not mean one of your models in code. Model is referring to a Swagger model. You will need to have an object in your Swagger JSON called definitions that includes the docs for each of your objects (aka: models).
I responded directly to your question in the google group--please make note of the fact that you're using a pretty old version of swagger-ui. Please upgrade to 2.1.x if possible.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.