Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Object of type UUID is not JSON serializable #126

Open
devova opened this issue Jul 1, 2024 · 1 comment
Open

Object of type UUID is not JSON serializable #126

devova opened this issue Jul 1, 2024 · 1 comment
Assignees
Labels
bug Something isn't working

Comments

@devova
Copy link

devova commented Jul 1, 2024

Looks like TypeAdapter.dump_python() miss one critical argument - mode="json", as result not all objects are properly serialised

STR:

  1. Create a Payload model
from pydantic import BaseModel
from uuid import UUID, uuid4

class A(BaseModel):
    id: UUID
  1. Then use it in Protocol
class TheClient(Protocol):
    @http_method("POST")
    @path("/fix-me")
    def fix_json_dump(self, payload: Payload[A]) -> SomeSuccesfullResponce:
        ...
  1. Eventually try to call it
client = HttpxBackend(Client(base_url="http://bin.com"))[TheClient]
client.fix_json_dump(A(id=uuid4()))

Actual result
It crashes

_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

cls = <class 'combadge.core.errors.BackendError'>
exc_type = <class 'TypeError'>
exc_value = TypeError('Object of type UUID is not JSON serializable')
traceback = <traceback object at 0x112cad1c0>

    def __exit__(  # type: ignore[misc]
        cls: Type[BaseException],  # noqa: N805
        exc_type: Type[BaseException] | None,
        exc_value: BaseException | None,
        traceback: TracebackType | None,
        /,
    ) -> None:
        if exc_value is not None:
>           raise cls(exc_value) from exc_value
E           combadge.core.errors.BackendError: Object of type UUID is not JSON serializable

../../../Library/Caches/pypoetry/virtualenvs/the-service-HGRE9N-l-py3.11/lib/python3.11/site-packages/combadge/core/errors.py:23: BackendError

So the problem is here, it should be .dump_python(..., mode="json")

@eigenein eigenein self-assigned this Jul 18, 2024
@eigenein eigenein added the bug Something isn't working label Jul 18, 2024
@eigenein
Copy link
Member

eigenein commented Jul 18, 2024

Thanks for reporting it, and sorry for the slow response

I'll look into it! Unfortunately, fixing it isn't that simple: it works alright, but it defeats the purpose of having different backends and serialization formats (some have a special representation for e.g. UUID)

Related: #94

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants