emmett-framework / emmett

The web framework for inventors
Other
1.03k stars 70 forks source link

Bug in post to AuthUser model in version 2.4.x #431

Closed josejachuf closed 2 years ago

josejachuf commented 2 years ago

Other bugs are happening with version 2.4.x. It is when I create a new user. I overwritten the email field so that it is not unique to avoid the problem [1]

email = Field(length=255)

class User(AuthUser):
    tablename = 'auth_users'
    belongs_to({'institucion': 'Institucion'})

    avatar = Field.upload(autodelete=True)
    email = Field(length=255)

When I make the post:

curl -X POST http://localhost:8080/main/api/v1/admin/usuarios -H 'Content-Type: application/json' -d '{"email": "juan@mail.com", "first_name": "Juan", "institucion": 2, "last_name": "Lopez", "password": "123456"}'

this error occurs

ERROR in handlers [/venv/lib/python3.8/site-packages/emmett/asgi/handlers.py:340]: Application exception: Traceback (most recent call last): File "/venv/lib/python3.8/site-packages/emmett/asgi/handlers.py", line 325, in dynamic_handler http = await self.router.dispatch(ctx.request, ctx.response) File "/venv/lib/python3.8/site-packages/emmett/routing/router.py", line 249, in dispatch return await match.dispatch(reqargs, response) File "/venv/lib/python3.8/site-packages/emmett/routing/dispatchers.py", line 72, in dispatch rv = self.response_builder(await self.f(reqargs), response) File "/venv/lib/python3.8/site-packages/emmett/pipeline.py", line 328, in flow output = await pipe_method(f, kwargs) File "/venv/lib/python3.8/site-packages/emmett/pipeline.py", line 234, in pipe return await next_pipe(kwargs) File "/venv/lib/python3.8/site-packages/emmett/pipeline.py", line 328, in flow output = await pipe_method(f, kwargs) File "/venv/lib/python3.8/site-packages/emmett/tools/auth/apis.py", line 277, in pipe return await next_pipe(kwargs) File "/venv/lib/python3.8/site-packages/emmett/pipeline.py", line 369, in flow return await pipe_method(f, kwargs) File "/venv/lib/python3.8/site-packages/emmett/tools/service.py", line 28, in pipe_request return self.encoder(await next_pipe(**kwargs)) File "/venv/lib/python3.8/site-packages/emmett/serializers.py", line 55, in _json_default raise TypeError TypeError INFO: 127.0.0.1:59876 - "POST /main/api/v1/admin/usuarios HTTP/1.1" 500 Internal Server Error

José

[1] https://github.com/emmett-framework/emmett/issues/428

gi0baro commented 2 years ago

@josejachuf is this rapid-json or orjson? do you have any additional info on which field causes the TypeError?

josejachuf commented 2 years ago

Hi @gi0baro

it is python-rapidjson 1.6

I will try to pass the project that fails before Saturday

josejachuf commented 2 years ago

With orjson installed:

ERROR in handlers [/venv/lib/python3.8/site-packages/emmett/asgi/handlers.py:340]: Application exception: Traceback (most recent call last): File "/venv/lib/python3.8/site-packages/emmett/asgi/handlers.py", line 325, in dynamic_handler http = await self.router.dispatch(ctx.request, ctx.response) File "/venv/lib/python3.8/site-packages/emmett/routing/router.py", line 249, in dispatch return await match.dispatch(reqargs, response) File "/venv/lib/python3.8/site-packages/emmett/routing/dispatchers.py", line 72, in dispatch rv = self.response_builder(await self.f(reqargs), response) File "/venv/lib/python3.8/site-packages/emmett/pipeline.py", line 328, in flow output = await pipe_method(f, kwargs) File "/venv/lib/python3.8/site-packages/emmett/pipeline.py", line 234, in pipe return await next_pipe(kwargs) File "/venv/lib/python3.8/site-packages/emmett/pipeline.py", line 328, in flow output = await pipe_method(f, kwargs) File "/venv/lib/python3.8/site-packages/emmett/tools/auth/apis.py", line 277, in pipe return await next_pipe(kwargs) File "/venv/lib/python3.8/site-packages/emmett/pipeline.py", line 369, in flow return await pipe_method(f, kwargs) File "/venv/lib/python3.8/site-packages/emmett/tools/service.py", line 28, in pipe_request return self.encoder(await next_pipe(**kwargs)) TypeError: Type is not JSON serializable: LazyCrypt

gi0baro commented 2 years ago

@josejachuf that's not a bug, is intended. Password fields are not JSON serializable. I suggest you to filter what you serialize, with REST extensions you might exclude those fields with the rest_rw dictionary in your model or a custom Serializer.

josejachuf commented 2 years ago

Thanks @gi0baro

This works fine:

rest_rw = {
        'institucion': True,
        'password': (False, True)
    }

I thought it was a bug, since in 2.3.1 it functioned well. The 2.4.x seems to be stricter in many things