Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove deprecated CompositionView (as of Sanic 21.12.0) #253

Merged
merged 1 commit into from Dec 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
41 changes: 0 additions & 41 deletions docs/sanic_openapi2/document_routes.md
Expand Up @@ -6,11 +6,9 @@ Sanic-OpenAPI support different ways to document APIs includes:
* routes of `Blueprint` instance
* routes of `HTTPMethodView` under `Sanic` instance
* routes of `HTTPMethodView` under `Bluebprint` instance
* routes of `CompositionView` under `Sanic` instance

But with some exceptions:

* Sanic-OpenAPI does not support routes of `CompositionView` under `Bluebprint` instance now.
* Sanic-OpenAPI does not document routes with `OPTIONS` method.
* Sanic-OpenAPI does not document routes which registered by `static()`.

Expand Down Expand Up @@ -172,42 +170,3 @@ if __name__ == "__main__":

The result:
![](../_static/images/blueprint_class_based_view_example.png)


## CompositionView Routes

There is another class-based view named `CompositionView`. Sanic-OpenAPI also support to document routes under class-based view.

```python
from sanic import Sanic
from sanic.response import text
from sanic.views import CompositionView

from sanic_openapi import openapi2_blueprint

app = Sanic()
app.blueprint(openapi2_blueprint)


def get_handler(request):
return text("I am a get method")


view = CompositionView()
view.add(["GET"], get_handler)
view.add(["POST", "PUT"], lambda request: text("I am a post/put method"))

# Use the new view to handle requests to the base URL
app.add_route(view, "/")

if __name__ == "__main__":
app.run(host="0.0.0.0", port=8000)

```

The Swagger will looks like:
![](../_static/images/composition_view_example.png)

```eval_rst
.. note:: Sanic-OpenAPI does not support routes of `CompositionView` under `Bluebprint` instance now.
```
7 changes: 1 addition & 6 deletions sanic_openapi/utils.py
@@ -1,7 +1,5 @@
import re

from sanic.views import CompositionView


def get_uri_filter(app):
"""
Expand Down Expand Up @@ -128,10 +126,7 @@ def get_all_routes(app, skip_prefix):
# create dict httpMethod -> handler
# e.g. {"GET" -> lambda request: response}

if type(route.handler) is CompositionView:
method_handlers = route.handler.handlers

elif hasattr(route.handler, "view_class"):
if hasattr(route.handler, "view_class"):
method_handlers = {
method: getattr(route.handler.view_class, method.lower())
for method in route.methods
Expand Down
37 changes: 1 addition & 36 deletions tests/test_swagger.py
Expand Up @@ -2,7 +2,7 @@
from sanic import Blueprint
from sanic.constants import HTTP_METHODS
from sanic.response import text
from sanic.views import CompositionView, HTTPMethodView
from sanic.views import HTTPMethodView

METHODS = [method.lower() for method in HTTP_METHODS]

Expand Down Expand Up @@ -34,11 +34,6 @@ def get_handler(request):
return text("I am a get method")


view = CompositionView()
view.add(["GET"], get_handler)
view.add(["POST", "PUT"], lambda request: text("I am a post/put method"))


def test_swagger_endpoint(app):
_, response = app.test_client.get("/swagger/")
assert response.status == 200
Expand Down Expand Up @@ -182,36 +177,6 @@ def test_blueprint_class_based_view(app):
assert {"name": "test"} in swagger_json["tags"]


def test_document_compositionview(app):
app.add_route(view, "/")

_, response = app.test_client.get("/swagger/swagger.json")
assert response.status == 200
assert response.content_type == "application/json"

swagger_json = response.json
assert set(swagger_json["paths"]["/"].keys()) == set(
["get", "post", "put"]
)
assert {"name": "test"} in swagger_json["tags"]


@pytest.mark.skip(reason="Not support now.")
def test_document_blueprint_compositionview(app):

bp = Blueprint("test")
bp.add_route(view, "/")

_, response = app.test_client.get("/swagger/swagger.json")
assert response.status == 200
assert response.content_type == "application/json"

swagger_json = response.json
assert set(swagger_json["paths"]["/"].keys()) == set(
["get", "post", "put"]
)


def test_swagger_ui_config(app):

_, response = app.test_client.get("/swagger/swagger-config")
Expand Down