fix bug we cannot use optional params

This commit is contained in:
Vincent Maillol
2020-11-03 12:51:27 +01:00
parent 13c19105d8
commit d866ce5358
7 changed files with 44 additions and 27 deletions

View File

@@ -4,6 +4,7 @@ from pydantic import BaseModel
class Pet(BaseModel):
id: int
name: str
age: int
class Error(BaseModel):

View File

@@ -1,4 +1,4 @@
from typing import List, Union
from typing import List, Optional, Union
from aiohttp import web
@@ -9,9 +9,11 @@ from .model import Error, Pet
class PetCollectionView(PydanticView):
async def get(self) -> r200[List[Pet]]:
async def get(self, age: Optional[int] = None) -> r200[List[Pet]]:
pets = self.request.app["model"].list_pets()
return web.json_response([pet.dict() for pet in pets])
return web.json_response(
[pet.dict() for pet in pets if age is None or age == pet.age]
)
async def post(self, pet: Pet) -> r201[Pet]:
self.request.app["model"].add_pet(pet)