简体   繁体   English

在命名空间实例化 flask_restx 上添加 doc 装饰器

[英]Add doc decorators on Namespace instantiation flask_restx

I'm trying to add OpenAPI/Swagger docs to a flask_restx Namespace class.我正在尝试将 OpenAPI/Swagger 文档添加到 flask_restx 命名空间 class。

The documented way of adding docs it's done by adding the doc decorator over a Resource class: @ns.doc(description="my documentation") .添加文档的记录方法是通过在资源 class: @ns.doc(description="my documentation")上添加文档装饰器来完成的。

In Namespace class there is a decorators parameter which takes a list of decorators and applies them on each resource (they are then called in Api class).在命名空间 class 中有一个装饰器参数,它采用装饰器列表并将它们应用于每个资源(然后在 Api 类中调用它们)。

I can't figure out how to add a list of @ns.doc decorators to Namespace我不知道如何将@ns.doc装饰器列表添加到命名空间

from flask_restx import Namespace, Resource

ns = Namespace(name = "namespace name")

ns.decorators = [
  simple_decorator_works,
  ns_doc_decorator(description="My default documentation on each resource")
]

@ns.route("/")
class MyResource(Resource):
    def get(self):
      return "awesome"

ns_doc_decorator should be able to receive some default parameters ns_doc_decorator应该可以接收一些默认参数

A base resource class was needed and the decorator put on that base resource class.需要一个基础资源 class 并且装饰器放置在该基础资源 class 上。


@ns.doc(description="My default documentation on each resource" etc)
class BaseResource(Resource): 
    ...
       
resource = type("resourceName", (BaseResource,), {'get': get_method, 'post': post_method etc})

ns.add_resource(resource)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM