Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to document a flask-restplus response with list of strings

I am documenting my response models, and need to show the api returns list of string.

["user1","user2"]

but the model requires a dictionary(json) format, as follow:

ns.response(200,'Success', NS.model("my_get_model",[{
    "name": fields.String(example="user1"),
}]))

I have tried following codes but none of them work:

ns = Namespace('My Apis')


ns.response(200,'Success', [ns.model("my_get_model",
    fields.String(example="user1")
)])

or

ns.response(200,'Success', ["user1"])

or

ns.response(200,'Success', ns.model("my_get_model",fields.List(fields.String(example="user1"))))

Please advice.

like image 980
user3375740 Avatar asked Aug 02 '19 19:08

user3375740


People also ask

Is FastAPI better than Flask?

FastAPI surpasses Flask in terms of performance, and it is one of the fastest Python web frameworks. Only Starlette and Uvicorn are faster. Because of ASGI, FastAPI supports concurrency and asynchronous code by declaring the endpoints. For concurrent programming, Python 3.4 introduced Async I/O.

What is Marshal_with in Flask?

marshal_with() is a convenience decorator, that is functionally equivalent to: class Todo(Resource): def get(self, **kwargs): return marshal(db_get_todo(), model), 200. The @api. marshal_with decorator add the swagger documentation ability.

What is namespace in Flask-RESTPlus?

namespace is from the Flask-RESTPlus and Blueprint is from flask to organize your app. the namespaces modules (specific to Flask-RESTPlus) are reusable namespaces designed like you would do with Flask's Blueprint.


1 Answers

I think this is what you need: https://github.com/python-restx/flask-restx/issues/65

Try simply passing in fields.List without wrapping a model.

ns.response(200, 'Success', fields.List(fields.String(example="user1")))

FYI, flask-restx is a fork of flask-restplus and it should have more updated features since flask-restplus is no longer being maintained due to the developers no able to contact flask-restplus's project owner.

like image 189
Pinyi Wang Avatar answered Sep 25 '22 20:09

Pinyi Wang