Skip to content

Commit 24e2b75

Browse files
committed
Updated README.md.
Signed-off-by: Pavel Kirilin <win10@list.ru>
1 parent 057abb8 commit 24e2b75

1 file changed

Lines changed: 44 additions & 2 deletions

File tree

README.md

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,10 @@ main_router.add_routes(memes_router, prefix="/memes")
6969

7070
If you use dependencies in you handlers, we can easily generate swagger for you.
7171
We have some limitations:
72-
1. We don't support string type annotation for detecting required parameters in openapi. Like `a: "Optional[int]"`.
73-
2. We don't have support for 3.10 style Option annotations. E.G. `int | None`
72+
1. We don't support resolving type aliases if hint is a string.
73+
If you define variable like this: `myvar = int | None` and then in handler
74+
you'd create annotation like this: `param: "str | myvar"` it will fail.
75+
You need to unquote type hint in order to get it work.
7476

7577
We will try to fix these limitations later.
7678

@@ -84,6 +86,46 @@ app = web.Application()
8486
app.on_startup.extend([init, setup_swagger()])
8587
```
8688

89+
### Responses
90+
91+
You can define schema for responses using dataclasses or
92+
pydantic models. This would not affect handlers in any way,
93+
it's only for documentation purposes, if you want to actually
94+
validate values your handler returns, please write your own wrapper.
95+
96+
```python
97+
from dataclasses import dataclass
98+
99+
from aiohttp import web
100+
from pydantic import BaseModel
101+
102+
from aiohttp_deps import Router, openapi_response
103+
104+
router = Router()
105+
106+
107+
@dataclass
108+
class Success:
109+
data: str
110+
111+
112+
class Unauthorized(BaseModel):
113+
why: str
114+
115+
116+
@router.get("/")
117+
@openapi_response(200, Success, content_type="application/xml")
118+
@openapi_response(200, Success)
119+
@openapi_response(401, Unauthorized, description="When token is not correct")
120+
async def handler() -> web.Response:
121+
...
122+
```
123+
124+
This example illustrates how much you can do with this decorator. You
125+
can have multiple content-types for a single status, or you can have different
126+
possble statuses. This function is pretty simple and if you want to make
127+
your own decorator for your responses, it won't be hard.
128+
87129

88130
## Default dependencies
89131

0 commit comments

Comments
 (0)