Add type to define OAS responses

This commit is contained in:
Vincent Maillol
2020-10-30 15:24:48 +01:00
parent 77954cdd69
commit 62d871fb5c
16 changed files with 342 additions and 71 deletions

View File

@@ -1,8 +1,11 @@
from pydantic.main import BaseModel
from aiohttp_pydantic import PydanticView, oas
from aiohttp import web
from typing import List, Union
import pytest
from aiohttp import web
from pydantic.main import BaseModel
from aiohttp_pydantic import PydanticView, oas
from aiohttp_pydantic.oas.typing import r200, r201, r204, r404
class Pet(BaseModel):
@@ -11,21 +14,21 @@ class Pet(BaseModel):
class PetCollectionView(PydanticView):
async def get(self):
async def get(self) -> r200[List[Pet]]:
return web.json_response()
async def post(self, pet: Pet):
async def post(self, pet: Pet) -> r201[Pet]:
return web.json_response()
class PetItemView(PydanticView):
async def get(self, id: int, /):
async def get(self, id: int, /) -> Union[r200[Pet], r404]:
return web.json_response()
async def put(self, id: int, /, pet: Pet):
return web.json_response()
async def delete(self, id: int, /):
async def delete(self, id: int, /) -> r204:
return web.json_response()
@@ -48,7 +51,28 @@ 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"] == {}
assert generated_oas["paths"]["/pets"]["get"] == {
"responses": {
"200": {
"content": {
"application/json": {
"schema": {
"type": "array",
"items": {
"title": "Pet",
"type": "object",
"properties": {
"id": {"title": "Id", "type": "integer"},
"name": {"title": "Name", "type": "string"},
},
"required": ["id", "name"],
},
}
}
}
}
}
}
async def test_pets_route_should_have_post_method(generated_oas):
@@ -57,17 +81,34 @@ async def test_pets_route_should_have_post_method(generated_oas):
"content": {
"application/json": {
"schema": {
"title": "Pet",
"type": "object",
"properties": {
"id": {"title": "Id", "type": "integer"},
"name": {"title": "Name", "type": "string"},
},
"required": ["id", "name"],
"title": "Pet",
"type": "object",
}
}
}
}
},
"responses": {
"201": {
"content": {
"application/json": {
"schema": {
"title": "Pet",
"type": "object",
"properties": {
"id": {"title": "Id", "type": "integer"},
"name": {"title": "Name", "type": "string"},
},
"required": ["id", "name"],
}
}
}
}
},
}
@@ -79,12 +120,13 @@ async def test_pets_id_route_should_have_delete_method(generated_oas):
assert generated_oas["paths"]["/pets/{id}"]["delete"] == {
"parameters": [
{
"required": True,
"in": "path",
"name": "id",
"required": True,
"schema": {"type": "integer"},
}
]
],
"responses": {"204": {"content": {}}},
}
@@ -97,7 +139,25 @@ async def test_pets_id_route_should_have_get_method(generated_oas):
"required": True,
"schema": {"type": "integer"},
}
]
],
"responses": {
"200": {
"content": {
"application/json": {
"schema": {
"properties": {
"id": {"title": "Id", "type": "integer"},
"name": {"title": "Name", "type": "string"},
},
"required": ["id", "name"],
"title": "Pet",
"type": "object",
}
}
}
},
"404": {"content": {}},
},
}

View File

@@ -1,7 +1,9 @@
from aiohttp_pydantic.injectors import _parse_func_signature
from pydantic import BaseModel
from uuid import UUID
from pydantic import BaseModel
from aiohttp_pydantic.injectors import _parse_func_signature
class User(BaseModel):
firstname: str

View File

@@ -1,6 +1,8 @@
from pydantic import BaseModel
from typing import Optional
from aiohttp import web
from pydantic import BaseModel
from aiohttp_pydantic import PydanticView

View File

@@ -1,7 +1,9 @@
from aiohttp import web
from aiohttp_pydantic import PydanticView
from datetime import datetime
import json
from datetime import datetime
from aiohttp import web
from aiohttp_pydantic import PydanticView
class JSONEncoder(json.JSONEncoder):

View File

@@ -1,4 +1,5 @@
from aiohttp import web
from aiohttp_pydantic import PydanticView

View File

@@ -1,4 +1,5 @@
from aiohttp import web
from aiohttp_pydantic import PydanticView