Maillol / aiohttp-pydantic

Aiohttp View that validates request body and query sting regarding the annotations declared in the View method
MIT License
67 stars 21 forks source link

Tommmlij/fix custom types #52

Closed tommmlij closed 3 months ago

tommmlij commented 8 months ago

After some tests, I found out that adding the include_extras parameter when inferring the types, would lead to annotations being carried over to request validation. This leads to this working:

from aiohttp.web import json_response, Application, run_app
from pydantic import Base64UrlStr, SerializeAsAny
from aiohttp_pydantic import PydanticView

class MyView(PydanticView):

    async def get(self, search: SerializeAsAny[Base64UrlStr], /):
        return json_response({"search": search})

if __name__ == '__main__':
    app = Application()
    app.router.add_view('/{search}', MyView)
    run_app(app)
curl localhost:8080/Zm9v
# {"search": "foo"}

curl localhost:8080/foo
# [{"type": "base64_decode", "loc": ["search"], "msg": "Base64 decoding error: 'Incorrect padding'", "input": "foo", "ctx": {"error": "Incorrect padding"}, "in": "path"}]

So base64 decoding implicitly via the type. That is awesome. But I am not 100% sure about problems with that. Tests are running through.

P.S.: Thx for the good work, the project is really enhancing the work with my aiohttp-servers.

Maillol commented 3 months ago

Thanks a lot !

Released in aiohttp-pydantic==2.2.2