From 913f50298c0376f0ba24df050c3308f8cdd1699a Mon Sep 17 00:00:00 2001 From: Vincent Maillol Date: Sat, 14 Nov 2020 20:33:55 +0100 Subject: [PATCH] Use docstring of handler in the OAS description --- aiohttp_pydantic/oas/view.py | 4 ++++ aiohttp_pydantic/view.py | 10 ++++++++-- tests/test_oas/test_view.py | 8 +++++++- 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/aiohttp_pydantic/oas/view.py b/aiohttp_pydantic/oas/view.py index b53c167..4c03ff0 100644 --- a/aiohttp_pydantic/oas/view.py +++ b/aiohttp_pydantic/oas/view.py @@ -1,3 +1,4 @@ +from inspect import getdoc import typing from typing import List, Type @@ -67,6 +68,9 @@ def _add_http_method_to_oas( oas_operation: OperationObject = getattr(oas_path, http_method) handler = getattr(view, http_method) path_args, body_args, qs_args, header_args = _parse_func_signature(handler) + description = getdoc(handler) + if description: + oas_operation.description = description if body_args: oas_operation.request_body.content = { diff --git a/aiohttp_pydantic/view.py b/aiohttp_pydantic/view.py index a899542..e04f71e 100644 --- a/aiohttp_pydantic/view.py +++ b/aiohttp_pydantic/view.py @@ -9,8 +9,14 @@ from aiohttp.web_exceptions import HTTPMethodNotAllowed from aiohttp.web_response import StreamResponse from pydantic import ValidationError -from .injectors import (AbstractInjector, BodyGetter, HeadersGetter, - MatchInfoGetter, QueryGetter, _parse_func_signature) +from .injectors import ( + AbstractInjector, + BodyGetter, + HeadersGetter, + MatchInfoGetter, + QueryGetter, + _parse_func_signature, +) class PydanticView(AbstractView): diff --git a/tests/test_oas/test_view.py b/tests/test_oas/test_view.py index 17332d0..f3017a8 100644 --- a/tests/test_oas/test_view.py +++ b/tests/test_oas/test_view.py @@ -15,9 +15,13 @@ class Pet(BaseModel): class PetCollectionView(PydanticView): async def get(self) -> r200[List[Pet]]: + """ + Get a list of pets + """ return web.json_response() async def post(self, pet: Pet) -> r201[Pet]: + """Create a Pet""" return web.json_response() @@ -52,6 +56,7 @@ async def test_generated_oas_should_have_pets_paths(generated_oas): async def test_pets_route_should_have_get_method(generated_oas): assert generated_oas["paths"]["/pets"]["get"] == { + "description": "Get a list of pets", "responses": { "200": { "content": { @@ -71,12 +76,13 @@ async def test_pets_route_should_have_get_method(generated_oas): } } } - } + }, } async def test_pets_route_should_have_post_method(generated_oas): assert generated_oas["paths"]["/pets"]["post"] == { + "description": "Create a Pet", "requestBody": { "content": { "application/json": {