diff --git a/Makefile b/Makefile index 034671e3..5b3610f6 100644 --- a/Makefile +++ b/Makefile @@ -42,6 +42,7 @@ publish: package format: @ruff format ${SOURCE_FILES} + @ruff check --fix ${PY_SOURCE} lint: @ruff check ${SOURCE_FILES} diff --git a/README.md b/README.md index 9c18e5bd..3e6434df 100644 --- a/README.md +++ b/README.md @@ -51,7 +51,7 @@ Check the [examples](examples) folder. * falcon: `req.context` * starlette: `request.context` 5. register to the web application `api.register(app)` -6. check the document at URL location `/apidoc/redoc` or `/apidoc/swagger` +6. check the document at URL location `/apidoc/redoc` or `/apidoc/swagger` or `/apidoc/scalar` If the request doesn't pass the validation, it will return a 422 with a JSON error message(ctx, loc, msg, type). diff --git a/examples/flask_view.py b/examples/flask_view.py new file mode 100644 index 00000000..369b12e4 --- /dev/null +++ b/examples/flask_view.py @@ -0,0 +1,25 @@ +from flask import Flask, jsonify +from flask.views import MethodView +from pydantic import BaseModel + +from spectree import SpecTree + +app = Flask(__name__) +spec = SpecTree("flask", annotations=True) + + +class User(BaseModel): + name: str + token: str + + +class Login(MethodView): + @spec.validate() + def post(self, json: User): + print(json) + return jsonify({"msg": "success"}) + + +if __name__ == "__main__": + app.add_url_rule("/login", view_func=Login.as_view("login")) + app.run(debug=True) diff --git a/pyproject.toml b/pyproject.toml index 6db278ea..a420fec3 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "spectree" -version = "1.2.8" +version = "1.2.9" dynamic = [] description = "generate OpenAPI document and validate request&response with Python annotations." readme = "README.md" diff --git a/spectree/page.py b/spectree/page.py index d892789a..7afa0fab 100644 --- a/spectree/page.py +++ b/spectree/page.py @@ -36,37 +36,17 @@ - Swagger UI - - + + + SwaggerUI +
- - + + +""", + "scalar": """ + + + + API Reference + + + + + + + + + """, } diff --git a/spectree/plugins/flask_plugin.py b/spectree/plugins/flask_plugin.py index 164ab62d..2014e285 100644 --- a/spectree/plugins/flask_plugin.py +++ b/spectree/plugins/flask_plugin.py @@ -50,10 +50,10 @@ def parse_func(self, route: Any): func = current_app.view_functions[route.endpoint] # view class: https://flask.palletsprojects.com/en/1.1.x/views/ - if getattr(func, "view_class", None): - cls = func.view_class + view_cls = getattr(func, "view_class", None) + if view_cls: for method in route.methods: - view = getattr(cls, method.lower(), None) + view = getattr(view_cls, method.lower(), None) if view: yield method, view else: diff --git a/spectree/plugins/quart_plugin.py b/spectree/plugins/quart_plugin.py index c57490ec..910652e7 100644 --- a/spectree/plugins/quart_plugin.py +++ b/spectree/plugins/quart_plugin.py @@ -53,10 +53,10 @@ def parse_func(self, route: Any): func = current_app.view_functions[route.endpoint] # view class: https://flask.palletsprojects.com/en/1.1.x/views/ - if getattr(func, "view_class", None): - cls = func.view_class + view_cls = getattr(func, "view_class", None) + if view_cls: for method in route.methods: - view = getattr(cls, method.lower(), None) + view = getattr(view_cls, method.lower(), None) if view: yield method, view else: