Skip to content
Advertisement

flask restful: how to document response body with fields.Dict()?

In flask-restplus, I want to model the response body which has nested list strucure, so whenever make api call, response body will be returned what I expected. In responce body, it has a nested structure, I don’t know how to document that. Am I gonna use fields.Dict()? can anyone point me out here how to make this happen in flask-restplus?

response body:

JavaScript

my partial solution:

here is my partial solution

JavaScript

in my attempted code, I couldn’t come up with a solution of how to model used_features dictionary. Is there any way to fix the defect of above attempt? can anyone point me out how to make this work where I can model the response body correctly? Am I gonna use Dict or Nested in my code? any further thought? thanks

Advertisement

Answer

Use @ns.marshal_with(payload).

The decorator marshal_with() is what actually takes your data object and applies the field filtering. The marshalling can work on single objects, dicts, or lists of objects. Marshalling Resource Link: https://flaskrestplus.readthedocs.io/en/stable/marshalling.html

And to model used_features use fields.Nested. I have shown how to use it in the following code.

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement