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

Tommmlij/fix custom types

Open tommmlij opened this issue 1 year ago • 0 comments

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.

tommmlij avatar Mar 25 '24 15:03 tommmlij