aiohttp-pydantic icon indicating copy to clipboard operation
aiohttp-pydantic copied to clipboard

Pure List Body

Open carmonium opened this issue 3 years ago • 1 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

carmonium avatar Nov 08 '22 00:11 carmonium

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]:

Maillol avatar Nov 19 '22 20:11 Maillol