Skip to content

Commit

Permalink
model inheritance + doc
Browse files Browse the repository at this point in the history
  • Loading branch information
denisovkv committed Mar 9, 2020
1 parent fe29d07 commit be991ee
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
33 changes: 33 additions & 0 deletions docs/sanic_openapi/fields.md
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,39 @@ async def test(request):
And the result:
![](../_static/images/fields/object.png)

Inheritance is also supported.

```python
from sanic import Sanic
from sanic.response import json

from sanic_openapi import doc, swagger_blueprint

app = Sanic()
app.blueprint(swagger_blueprint)


class User:
username = doc.String("The name of your user account.")
password = doc.String("The password of your user account.")


class UserInfo(User):
first_name = doc.String("The first name of user.")
last_name = doc.String("The last name of user.")


@app.get("/test")
@doc.produces(UserInfo)
async def test(request):
return json({})

app.run(host="0.0.0.0", debug=True)

```

And the result:
![](../_static/images/fields/object_inheritance.png)

## [PEP484](https://www.python.org/dev/peps/pep-0484/)'s type hinting

Expand Down
3 changes: 2 additions & 1 deletion sanic_openapi/doc.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,8 @@ def definition(self):
"properties": {
key: serialize_schema(schema)
for key, schema in chain(
self.cls.__dict__.items(), typing.get_type_hints(self.cls).items()
{key: getattr(self.cls, key) for key in dir(self.cls)}.items(),
typing.get_type_hints(self.cls).items(),
)
if not key.startswith("_")
},
Expand Down

0 comments on commit be991ee

Please sign in to comment.