Maillol / aiohttp-pydantic

Aiohttp View that validates request body and query sting regarding the annotations declared in the View method

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Pure List Body

carmonium opened this issue · comments

Hi there,

I'm testing your library, but can't figure out how declare that the body of the put or post request will be a List. I mean no json wrapper around, just a list. [{"a":"b", ...}].
I have tried in the function signature to declare like post(self, query_para: str, List[ListBody]), being ListBody the Class of objects the List will contain. The oas builder think it is a query parameter as well so throws an error that such query param is not existent.

If i do it with pydantic, I'm forced to put a tag on a new class like:
class RecordsList(BaseModel)
records: List[ListBody]
which is exactly what I need to avoid.

Am I doing something wrong, or is it a requirement for the library to do it via Pydantic ?

Thanks in advance.

LC

Hi,

According to the pydantic documentation you can define the __root__ attribute in your pydantic model :

https://pydantic-docs.helpmanual.io/usage/models/#custom-root-types

class Pet(BaseModel):
    id: int
    name: str
    age: int
    friends: Friend


class PetList(BaseModel):

    __root__: List[Pet]

    def __iter__(self):
        return iter(self.__root__)

    def __getitem__(self, item):
        return self.__root__[item]

And declare your post method:

    async def post(self, pets: PetList) -> r201[Pet]: