noirbizarre / flask-restplus

Fully featured framework for fast, easy and documented API development with Flask
http://flask-restplus.readthedocs.org
Other
2.73k stars 507 forks source link

Swagger How to model input json payload to display list (array) that contains two dicts. Can only get one to show. #540

Open timothytierney opened 6 years ago

timothytierney commented 6 years ago

I have a networks_fields model that I'm trying to display in a sample swagger payload that displays 2 dicts in list that is nested (bottom has my ending result I'm looking for).

I can only get payload in swagger to display the nested list as a single entry: { "networks": [ { "dhcpEnable": true, "macaddress": "string", "networktype": "string", "ipaddress": "string" } ] }

How do I get the swagger payload to display two elements in this list, like below? { "networks": [ { "dhcpEnable": true, "macaddress": "string", "networktype": "string", "ipaddress": "string" }, { "dhcpEnable": true, "macaddress": "string", "networktype": "string", "ipaddress": "string" } ] }

networks_fields = api.model('networks', { 'ipaddress': fields.String, 'macaddress': fields.String, 'dhcpEnable': fields.Boolean(default=True), 'networktype': fields.String})

resource_test_fields = api.model('test', { 'networks': fields.List(fields.Nested(networks_fields))})

@vc.route('/test/api/v1.0/hosts/,') class add_test(Resource):

@api.expect(resource_test_fields)    
def post(self, param1, param1):
    pass

Thanks for looking.

timothytierney commented 6 years ago

I found a way to do this using the fields.Raw option.

Define your array:

networks_array = [ { 'ipaddress': "", 'macaddress': "", 'Enable': True, 'networktype': "" }, { 'ipaddress': "", 'macaddress': "", 'Enable': True, 'networktype': "" }
]

in my model, I defined a fields.Raw as

'networks': fields.Raw(example=networks_array, type=json)

and it displays

"networks": [ { "macaddress": "", "networktype": "", "Enable": true, "ipaddress": "" }, { "macaddress": "", "networktype": "", "Enable": true, "ipaddress": "" } ]

BostonKenne commented 4 years ago

Try to use enum like this:

resource_test_fields = api.model('test model', {
'networks': fields.String(description="your description", enum=your_custom_list)}
)