Skip to content

One decorator to wrap potentially all route decorators including route registration and doc #107

Closed
@volfpeter

Description

When documenting a route, we typically have to add an app.route decorator, then a set of sanic-openapi decorators, then authorization, cors, request or response transformer decorators, and so on. The resulting code is hard to read and not that good to look at.

I've implemented a sanic-openapi-based decorator factory class that can be used for documenting routes (and even registering them in the Sanic app or blueprint, or adding extra decorators to them) with a single line of code (a single decorator). I would be happy to commit it to this project if this is something you're interested in.

Here is an example code, API is the decorator factory class I implemented (route documentation classes must be derived from this base class):

# This is the actual class that documents the route.
class Data(API):
    class consumes:
        stg = str

# This Data extension adds the first and second decorators as well to the decorated route.
class DecoratedData(Data):
    decorators = (first, second)

# Below are example routes, all of which have the exact same behavior.
# --------------------------------------

@DecoratedData.get(app, "/data")
def data_all_in_one(request: Request):
    return "data"

@app.get("/data")
@DecoratedData
def data_doc_and_decorators_in_one(request: Request):
    return "data"

@Data.get(app, "/data")
@first
@second
def data_routing_and_doc_in_one(request: Request):
    return "data"

@app.get("/data")
@Data
@first
@second
def data(request:Request):
    return "data"

@app.get("/data")
@doc.consumes({"stg": str})
@first
@second
def data(request:Request):
    return "data"

I understand if this is not something you would like to have in the project.
On the other hand, if you are interested, then I'll submit a pull request so you can a have look at the code and get a better understanding of what it actually does.

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions