Skip to content

Commit

Permalink
squashed: oas2/oas3 fixes, cleanup, docs,
Browse files Browse the repository at this point in the history
* fix(oas2): encourage better swagger2 spec without user configuration by setting required-by-spec defaults

* clean: start to make oas2 blueprint.py and oas3 blueprint.py share behaviour

* fix(oas3): adjust typo during original implementation that might mess up class based view

* clean: remove deprecated/invalid dockerfile

* feat: add types sto openapi

* feat: add OS related section to .gitignore

* fix: use Any for Array items

* build: fix broken tox conf

* loosen pyyaml requirement

* feat: add oas3 example

* docs: huge-success -> sanic-org

* fix: oas3 spec fixes

* style

* update setup.py classifiers

* dont set optional field operationId with bad value

* fix: use SecurityScheme

* wip: refactor routing parsing logic out of blueprint generation func

* Revert "fix: use SecurityScheme"

This reverts commit c815b71.

* mark secured notimplemented

* style

Co-authored-by: artcg <arthurgoldbergfwd@gmail.com>
  • Loading branch information
astagi and artcg committed Mar 22, 2021
1 parent 90b3fe5 commit 8fe2131
Show file tree
Hide file tree
Showing 36 changed files with 622 additions and 504 deletions.
38 changes: 19 additions & 19 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

### Features

* Model inheritance ([0aa81c](https://github.com/huge-success/sanic-openapi/commit/0aa81c83754767c78444c3d01d548305858d8a22))
* Model inheritance ([0aa81c](https://github.com/sanic-org/sanic-openapi/commit/0aa81c83754767c78444c3d01d548305858d8a22))

### Bug Fixes

* TypeError when Spec obj is not JSON serializable ([fe29d0](https://github.com/huge-success/sanic-openapi/commit/fe29d07ccb0e02ec0be6496e971946269b2d7907))
* Attribute name "name" conflict in consumes body ([67aaf3](https://github.com/huge-success/sanic-openapi/commit/67aaf34eca5e339c349ef65bd0392cb8a97f184e))
* TypeError when Spec obj is not JSON serializable ([fe29d0](https://github.com/sanic-org/sanic-openapi/commit/fe29d07ccb0e02ec0be6496e971946269b2d7907))
* Attribute name "name" conflict in consumes body ([67aaf3](https://github.com/sanic-org/sanic-openapi/commit/67aaf34eca5e339c349ef65bd0392cb8a97f184e))

### 0.6.1 (2020-01-03)

Expand All @@ -19,39 +19,39 @@
* **examples:** add class based routing demonstration in cars example ([df7724f](https://github.com/chenjr0719/sanic-openapi/commit/df7724fa344a8feefede5168541433e19e969b5c))
* **examples:** class based routing example ([9137de1](https://github.com/chenjr0719/sanic-openapi/commit/9137de14037778e0588853cb83a1ded2e645845f))
* fix for class based routing ([de21965](https://github.com/chenjr0719/sanic-openapi/commit/de21965a8e94e1f73efa8bd420a8d39367fa7f26))
* Provide ASGI compatibility through server event ([a6df84](https://github.com/huge-success/sanic-openapi/pull/130))
* Add basic support for type hinting-based docs ([ee640f](https://github.com/huge-success/sanic-openapi/pull/129))
* Basic support for TypedDicts (PEP 589) ([360a03](https://github.com/huge-success/sanic-openapi/pull/134))
* Support typing.Sequence and typing.List ([f80d19](https://github.com/huge-success/sanic-openapi/pull/136))
* Remove global variables ([3cdf88](https://github.com/huge-success/sanic-openapi/pull/140))
* removed field 'type': 'object' from swagger json when using doc.Object() ([60605c](https://github.com/huge-success/sanic-openapi/pull/149))
* Provide ASGI compatibility through server event ([a6df84](https://github.com/sanic-org/sanic-openapi/pull/130))
* Add basic support for type hinting-based docs ([ee640f](https://github.com/sanic-org/sanic-openapi/pull/129))
* Basic support for TypedDicts (PEP 589) ([360a03](https://github.com/sanic-org/sanic-openapi/pull/134))
* Support typing.Sequence and typing.List ([f80d19](https://github.com/sanic-org/sanic-openapi/pull/136))
* Remove global variables ([3cdf88](https://github.com/sanic-org/sanic-openapi/pull/140))
* removed field 'type': 'object' from swagger json when using doc.Object() ([60605c](https://github.com/sanic-org/sanic-openapi/pull/149))

### Bug Fixes

* Allow empty list with List field ([74fd710](https://github.com/chenjr0719/sanic-openapi/commit/74fd71081a725d58545322dc2105c540de004529))
* fix example of class based view ([9c0bbd3](https://github.com/chenjr0719/sanic-openapi/commit/9c0bbd3a7e0ec2dfd999d6fbe14c2c1dedf36a29))
* Ignore routes under swagger blueprint ([ed932cc](https://github.com/chenjr0719/sanic-openapi/commit/ed932cca7286e59d8ac854a5dd0cf314c98ac688))
* fix typo in setup where sphinx-rtd-theme needed ([2e729e](https://github.com/huge-success/sanic-openapi/pull/139))
* fix typo in setup where sphinx-rtd-theme needed ([2e729e](https://github.com/sanic-org/sanic-openapi/pull/139))

## 0.6.0 (2019-08-02)

### Features

* Add API module ([4a43817](https://github.com/huge-success/sanic-openapi/pull/111))
* Make it possible to register the same model definition multiple times in `doc.definitions` ([3c3329c](https://github.com/huge-success/sanic-openapi/pull/110))
* Support Swagger configuration ([a093d55](https://github.com/huge-success/sanic-openapi/pull/100))
* Super call added to List field serialize method ([93ab6fa](https://github.com/huge-success/sanic-openapi/pull/93))
* Support and examples for Class-Based Views (HTTPMethodView) ([de21965](https://github.com/huge-success/sanic-openapi/pull/64))
* Add operation decorator ([039a0db](https://github.com/huge-success/sanic-openapi/pull/95))
* Add a if-statement at build_spec checking add default 200 response or not ([1d8d7d0](https://github.com/huge-success/sanic-openapi/pull/116))
* Add File field & Fix some line with black style ([06c662b](https://github.com/huge-success/sanic-openapi/pull/120))
* Add API module ([4a43817](https://github.com/sanic-org/sanic-openapi/pull/111))
* Make it possible to register the same model definition multiple times in `doc.definitions` ([3c3329c](https://github.com/sanic-org/sanic-openapi/pull/110))
* Support Swagger configuration ([a093d55](https://github.com/sanic-org/sanic-openapi/pull/100))
* Super call added to List field serialize method ([93ab6fa](https://github.com/sanic-org/sanic-openapi/pull/93))
* Support and examples for Class-Based Views (HTTPMethodView) ([de21965](https://github.com/sanic-org/sanic-openapi/pull/64))
* Add operation decorator ([039a0db](https://github.com/sanic-org/sanic-openapi/pull/95))
* Add a if-statement at build_spec checking add default 200 response or not ([1d8d7d0](https://github.com/sanic-org/sanic-openapi/pull/116))
* Add File field & Fix some line with black style ([06c662b](https://github.com/sanic-org/sanic-openapi/pull/120))

### Bug Fixes

* Allow empty list with List field ([74fd710](https://github.com/chenjr0719/sanic-openapi/commit/74fd710))
* fix example of class based view ([9c0bbd3](https://github.com/chenjr0719/sanic-openapi/commit/9c0bbd3))
* Ignore routes under swagger blueprint ([ed932cc](https://github.com/chenjr0719/sanic-openapi/commit/ed932cc))
* fix static and exclude ([5a8aab8](https://github.com/huge-success/sanic-openapi/pull/80))
* fix static and exclude ([5a8aab8](https://github.com/sanic-org/sanic-openapi/pull/80))


### Build System
Expand Down
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ Please make sure your contribution supports `Sanic 18.12` and `Python 3.5` **UNL

Some discussion of compatibility:

* [What position of this project](https://github.com/huge-success/sanic-openapi/issues/103#issuecomment-499463005)
* [What position of this project](https://github.com/sanic-org/sanic-openapi/issues/103#issuecomment-499463005)
* [Background story](https://community.sanicframework.org/t/should-we-bump-the-minimum-python-required-version-to-3-6/238/6?u=ahopkins)

## Swagger UI
Expand Down
12 changes: 0 additions & 12 deletions Dockerfile

This file was deleted.

8 changes: 0 additions & 8 deletions docker-compose.yml

This file was deleted.

2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pip install sanic-openapi
Or, use master banch from GitHub with latest features:

```shell
pip install git+https://github.com/huge-success/sanic-openapi.git
pip install git+https://github.com/sanic-org/sanic-openapi.git
```

## Getting Started
Expand Down
4 changes: 2 additions & 2 deletions docs/sanic_openapi/configurations.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ For more detail of those additional information, please check the [document](htt

app = Sanic()
app.blueprint(swagger_blueprint)
app.config["API_TERMS_OF_SERVICE"] = "https://github.com/huge-success/sanic-openapi/blob/master/README.md"
app.config["API_TERMS_OF_SERVICE"] = "https://github.com/sanic-org/sanic-openapi/blob/master/README.md"

```

Expand Down Expand Up @@ -210,7 +210,7 @@ For more detail of those additional information, please check the [document](htt

app = Sanic()
app.blueprint(swagger_blueprint)
app.config["API_LICENSE_URL"] = "https://github.com/huge-success/sanic-openapi/blob/master/LICENSE"
app.config["API_LICENSE_URL"] = "https://github.com/sanic-org/sanic-openapi/blob/master/LICENSE"

```

Expand Down
2 changes: 1 addition & 1 deletion examples/cars/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## Cars

[Source](https://github.com/huge-success/sanic-openapi/tree/master/examples/cars)
[Source](https://github.com/sanic-org/sanic-openapi/tree/master/examples/cars)

This example shows how to use Sanic-OpenAPI with Sanic's Blueprint and model all the data into Sanic-OpenAPI's fields.

Expand Down
4 changes: 2 additions & 2 deletions examples/cars/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
test_manufacturer = {
'id': 1,
'name': "Nissan",
'start_date': datetime.date(year=1933, month=12, day=26)
'start_date': str(datetime.date(year=1933, month=12, day=26))
}

test_driver = {
'id': 1,
'name': "Sanic",
'birthday': datetime.date(year=2010, month=3, day=31)
'birthday': str(datetime.date(year=2010, month=3, day=31))
}

test_car = {
Expand Down
3 changes: 2 additions & 1 deletion examples/cars/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from sanic_openapi import swagger_blueprint

app = Sanic()
app = Sanic("Cars API example")

app.blueprint(swagger_blueprint)
app.blueprint(car_blueprint)
Expand All @@ -20,5 +20,6 @@
app.config.API_TITLE = 'Car API'
app.config.API_TERMS_OF_SERVICE = 'Use with caution!'
app.config.API_CONTACT_EMAIL = 'channelcat@gmail.com'
app.config.API_DESCRIPTION = 'Cars API example'

app.run(host="0.0.0.0", debug=True)
21 changes: 21 additions & 0 deletions examples/cars_oas3/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
## Cars

[Source](https://github.com/sanic-org/sanic-openapi/tree/master/examples/cars)

This example shows how to use Sanic-OpenAPI with Sanic's Blueprint and model all the data into Sanic-OpenAPI's fields.

To run this example, please make sure you have already install `sanic` and `sanic-openapi`.
Or, run the install command:

```shell
pip install sanic sanic-openapi
```

And, you can run this example by:

```shell
python main.py
```

Now, check <http://localhost:8000/swagger> and you should see the swagger like:
![](./swagger.png)
43 changes: 43 additions & 0 deletions examples/cars_oas3/blueprints/car.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from data import test_car, test_success
from models import Car, Status
from sanic.blueprints import Blueprint
from sanic.response import json

from sanic_openapi import openapi

blueprint = Blueprint('Car', '/car')


@blueprint.get("/", strict_slashes=True)
@openapi.summary("Fetches all cars")
@openapi.description("Really gets the job done fetching these cars. I mean, really, wow.")
@openapi.response(200, {"application/json" : [Car]})
def car_list(request):
return json([test_car])


@blueprint.get("/<car_id:int>", strict_slashes=True)
@openapi.summary("Fetches a car")
@openapi.response(200, {"application/json" : Car})
def car_get(request, car_id):
return json(test_car)


@blueprint.put("/<car_id:int>", strict_slashes=True)
@openapi.summary("Updates a car")
@openapi.body(
{ "application/json" : Car },
description="Body description",
required=True,
)
@openapi.parameter("AUTHORIZATION", str, location="header")
@openapi.response(200, {"application/json" : Car})
def car_put(request, car_id):
return json(test_car)


@blueprint.delete("/<car_id:int>", strict_slashes=True)
@openapi.summary("Deletes a car")
@openapi.response(204, {"application/json" : Status})
def car_delete(request, car_id):
return json(test_success)
43 changes: 43 additions & 0 deletions examples/cars_oas3/blueprints/driver.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import json

from data import test_driver, test_success
from models import Driver, Status
from sanic.blueprints import Blueprint
from sanic.response import json

from sanic_openapi import openapi

blueprint = Blueprint('Driver', '/driver')


@blueprint.get("/", strict_slashes=True)
@openapi.summary("Fetches all drivers")
@openapi.response(200, {"application/json" : [Driver]})
def driver_list(request):
return json([test_driver])


@blueprint.get("/<driver_id:int>", strict_slashes=True)
@openapi.summary("Fetches a driver")
@openapi.response(200, {"application/json" : Driver})
def driver_get(request, driver_id):
return json(test_driver)


@blueprint.put("/<driver_id:int>", strict_slashes=True)
@openapi.summary("Updates a driver")
@openapi.body(
{ "application/json" : Driver },
description="Body description",
required=True,
)
@openapi.response(200, {"application/json" : Driver})
def driver_put(request, driver_id):
return json(test_driver)


@blueprint.delete("/<driver_id:int>", strict_slashes=True)
@openapi.summary("Deletes a driver")
@openapi.response(204, {"application/json" : Status})
def driver_delete(request, driver_id):
return json(test_success)
41 changes: 41 additions & 0 deletions examples/cars_oas3/blueprints/garage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import json

from data import test_garage, test_success
from models import Car, Garage, Status
from sanic.blueprints import Blueprint
from sanic.response import json

from sanic_openapi import openapi

blueprint = Blueprint('Garage', '/garage')


@blueprint.get("/", strict_slashes=True)
@openapi.summary("Fetches the garage")
@openapi.response(200, {"application/json" : Garage})
async def get_garage(request):
return json(test_garage)


@blueprint.get("/cars", strict_slashes=True)
@openapi.summary("Fetches the cars in the garage")
@openapi.body(
{ "application/json" : {"doors": int} },
description="Body description",
required=True,
)
@openapi.response(200, {"application/json" : {"cars": [Car]}})
async def get_cars(request):
return json(test_garage.get('cars'))


@blueprint.put("/car", strict_slashes=True)
@openapi.summary("Adds a car to the garage")
@openapi.body(
{ "application/json" : Car },
description="Body description",
required=True,
)
@openapi.response(204, {"application/json" : Status})
async def add_car(request):
return json(test_success)
41 changes: 41 additions & 0 deletions examples/cars_oas3/blueprints/manufacturer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from data import test_manufacturer, test_success
from models import Driver, Status
from sanic.blueprints import Blueprint
from sanic.response import json

from sanic_openapi import openapi

blueprint = Blueprint('Manufacturer', '/manufacturer')


@blueprint.get("/", strict_slashes=True)
@openapi.summary("Fetches all manufacturers")
@openapi.response(200, {"application/json" : [Driver]})
def manufacturer_list(request):
return json([test_manufacturer])


@blueprint.get("/<manufacturer_id:int>", strict_slashes=True)
@openapi.summary("Fetches a manufacturer")
@openapi.response(200, {"application/json" : Driver})
def manufacturer_get(request, manufacturer_id):
return json(test_manufacturer)


@blueprint.put("/<manufacturer_id:int>", strict_slashes=True)
@openapi.summary("Updates a manufacturer")
@openapi.body(
{ "application/json" : Driver },
description="Body description",
required=True,
)
@openapi.response(200, {"application/json" : Driver})
def manufacturer_put(request, manufacturer_id):
return json(test_manufacturer)


@blueprint.delete("/<manufacturer_id:int>", strict_slashes=True)
@openapi.summary("Deletes a manufacturer")
@openapi.response(200, {"application/json" : Status})
def manufacturer_delete(request, manufacturer_id):
return json(test_success)
24 changes: 24 additions & 0 deletions examples/cars_oas3/blueprints/repair.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from sanic.blueprints import Blueprint
from sanic.response import json
from sanic.views import HTTPMethodView

from sanic_openapi import openapi

blueprint = Blueprint('Repair', '/repair')
from data import test_station
from models import Station


class RepairStation(HTTPMethodView):
@openapi.summary("Fetches all repair stations")
@openapi.response(200, {"application/json" : [Station]})
def get(self, request):
return json([test_station])

@openapi.summary("make an appointment")
@openapi.description("submit necessary information for appointment")
def post(self, request):
return json(request.json)


blueprint.add_route(RepairStation.as_view(), "/station", strict_slashes=True)

0 comments on commit 8fe2131

Please sign in to comment.