Swagger API 文档和 flask-restplus:如何在请求正文中用键表示对象

Swagger API documentation and flask-restplus: How to represent an object with a key in the body of a request

我在 API:

的 PUT 请求正文中有此参数示例
{
  "id": "string",
  "closed_date": "2018-11-20T18:42:58.946Z",
  "contact": "string",
  "description": "string",
  "status": "Open"
}

为了在我的 Swagger 端点文档中显示它,我这样做了:

@api.doc(body=card_change_fields)
    def put(self, card_id, *args, **kwargs):

其中:

card_change_fields = api.model('card modification', {

        'id': fields.String(description='id', required=True),
        'closed_date': fields.DateTime(description='Closed date'),
        'contact': fields.String(description='Contact'),
        'description': fields.String(description='Description'),
        'status': fields.String(description='Status', required=True,
                                enum=["Open", "Closed"])
})

然而我想要的其实是这个:

{  card : {
  "id": "string",
  "closed_date": "2018-11-20T18:42:58.946Z",
  "contact": "string",
  "description": "string",
  "status": "Open" }
}

如何在我的 flask-restplus swagger 文档中执行此操作? 我尝试了子模型和父模型,但没有成功

谢谢, DT

您需要使用fields.Nested来使用一个模型作为另一个模型的输入。检查下面的代码:

card_change_fields = api.model('card modification', {
                'id': fields.String(description='id', required=True),
                'closed_date': fields.DateTime(description='Closed date'),
                'contact': fields.String(description='Contact'),
                'description': fields.String(description='Description'),
                'status': fields.String(description='Status', required=True,
                                        enum=["Open", "Closed"])
})

card = api.model('Card', {
    'card': fields.Nested(card_change_fields, required=True)
})

并且您的文档渲染也将分别更改为:

@api.doc(body=card)
    def put(self, card_id, *args, **kwargs):