Skip to content

Commit 4979c5f

Browse files
author
vvcheremushkin
committed
Nested Response class can be defined in method
1 parent 74d0a34 commit 4979c5f

File tree

7 files changed

+37
-13
lines changed

7 files changed

+37
-13
lines changed

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def get_packages(package):
2929
"starlette<=1",
3030
"apispec<3",
3131
],
32-
version="0.0.9",
32+
version="0.0.10",
3333
url="https://github.com/slv0/start_resty",
3434
license="BSD",
3535
description="The web framework",

star_resty/method/meta.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ def __new__(mcs, name, bases, attrs, **kwargs):
2727
def create_render(mcs, method):
2828
renders = []
2929
response_schema = getattr(method, 'response_schema', None)
30+
if response_schema is None:
31+
response_schema = getattr(method, 'Response', None)
32+
3033
if response_schema is not None:
3134
if inspect.isclass(response_schema):
3235
response_schema = response_schema()

star_resty/method/method.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import abc
22
from functools import wraps
3-
from typing import ClassVar, Type, Union
3+
from typing import ClassVar, Type, Union, Optional
44

55
from marshmallow import Schema
66
from starlette.requests import Request
@@ -20,6 +20,7 @@ class Method(abc.ABC, metaclass=MethodMeta):
2020
meta: ClassVar[Operation] = Operation(tag='default')
2121
serializer: ClassVar[Serializer] = JsonSerializer
2222
response_schema: ClassVar[Union[Schema, Type[Schema], None]] = None
23+
Response: ClassVar[Optional[Type[Schema]]] = None
2324
status_code: int = 200
2425

2526
def __init__(self, request: Request):

star_resty/operation/schema.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,4 @@ class Operation(NamedTuple):
55
tag: str = 'default'
66
description: Optional[str] = None
77
summary: Optional[str] = None
8-
status: int = 200
98
errors: Sequence[Any] = ()

tests/test_apidocs.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -95,15 +95,12 @@ def test_generate_api_docs_for_path():
9595
assert body is not None
9696
assert body.get('paths') == {
9797
'/users/{user_id}': {
98-
'post': {
99-
'tags': ['users'], 'description': 'get user', 'produces': ['application/json'],
100-
'parameters': [
101-
{'in': 'path', 'name': 'id', 'required': True, 'type': 'integer', 'format': 'int32'},
102-
{'in': 'body', 'required': False, 'name': 'body',
103-
'schema': {'$ref': '#/definitions/tests.utils.method.BodySchema'}}],
104-
'responses': {
105-
'200': {'schema': {'$ref': '#/definitions/tests.utils.method.CreateUserResponse'}},
106-
'400': {'description': 'Bad request'}}}}}
98+
'post': {'tags': ['users'], 'description': 'get user', 'produces': ['application/json'],
99+
'parameters': [
100+
{'in': 'path', 'name': 'id', 'required': True, 'type': 'integer', 'format': 'int32'},
101+
{'in': 'body', 'required': False, 'name': 'body',
102+
'schema': {'$ref': '#/definitions/tests.utils.method.BodySchema'}}],
103+
'responses': {'400': {'description': 'Bad request'}}}}}
107104

108105

109106
def test_generate_api_docs_for_nested():

tests/test_method.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,25 @@ async def execute(self):
4444
endpoint = TestMethod.as_endpoint()
4545
with pytest.raises(DumpError):
4646
await endpoint(request)
47+
48+
49+
@pytest.mark.asyncio
50+
async def test_response_schema_cls():
51+
class TestMethod(Method):
52+
class Response(Schema):
53+
id = fields.Integer()
54+
55+
async def execute(self):
56+
return {'id': 1}
57+
58+
request = mock.MagicMock(spec_set=Request)
59+
endpoint = TestMethod.as_endpoint()
60+
resp = await endpoint(request)
61+
assert resp is not None
62+
assert isinstance(resp, Response)
63+
assert resp.status_code == 200
64+
assert resp.media_type == 'application/json'
65+
body = resp.body
66+
assert body is not None
67+
user = json.loads(body)
68+
assert user == {'id': 1}

tests/utils/method.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ async def execute(self, user: path(PathParams),
5858

5959
class GetUser(Method):
6060
meta = Operation(tag='users', description='get user')
61-
response_schema = CreateUserResponse
61+
62+
class Response(CreateUserResponse):
63+
pass
6264

6365
async def execute(self, user: path(PathParams),
6466
payload: json_payload(BodySchema)):

0 commit comments

Comments
 (0)