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

cannot correctly serialize Json typed field #1038

Open
braindevices opened this issue Oct 9, 2024 · 1 comment
Open

cannot correctly serialize Json typed field #1038

braindevices opened this issue Oct 9, 2024 · 1 comment

Comments

@braindevices
Copy link

Bug description

if we have model like following:

model Test {
  id                String    @default(uuid()) @unique
  manifest          Json
}

It will generate prisma model:

class Test(bases.BaseTest):
    id: _str
    manifest: 'fields.Json'

The problem is, the Json type result automatically de-serialize to a python obect.
For example: '{"a":1}' become a dict {'a': 1}

We would expect Test.prisma(db).create(data=test.model_dump(mode='json')) work without problem.

However, it will not work, because during serialization the manifest does not serialize back to string. It keeps the dict type instead.

I think the correct behaviour should serialize the manifest back to Json string.

How to reproduce

Steps to reproduce the behavior:

  1. generate the model
  2. Test.prisma().create(data=Test(id="1", manifest='{"a": 1}').model_dump(mode='json'))

Expected behavior

It should just work

Prisma information

generator client {
  provider = "prisma-client-py"
  recursive_type_depth = -1
}

datasource db {
  provider = "mysql"
  url      = env("DATABASE_URL")
}
model Test {
  id                String    @default(uuid()) @unique
  manifest          Json
}

Environment & setup

  • OS: ubuntu
  • Database: mariadb
  • Python version: 3.10
  • Prisma version: 5.17.0
  • prisma client python : 0.15.0
@AutoScrape123TX
Copy link

AutoScrape123TX commented Nov 16, 2024

Yes absolutely. The Json deserializer is not giving back a str but a dict on a model_dump()...

I may send a PR soon to fix this.

But when reading source there is this statement so I think this was a choice but I don't understand why...

class Json(BaseJson):
    data: Serializable

    def __init__(self, data: Serializable) -> None:
        self.data = data
        super().__init__()

    @classmethod
    def keys(cls, **data: Serializable) -> Json:
        return cls(data)

    if TYPE_CHECKING:
        # Fields that are of the `Json` type are automatically
        # de-serialized from json to the corresponding python type
        # when the model is created, e.g.
        #
        # User(json_obj='{"foo": null}') -> User(json_obj={'foo': None})
        #
        # As we don't know what the type will actually be at runtime
        # we add methods here for convenience so that naive access
        # to the field is still allowed, e.g.
        #
        # user.json_obj['foo']
        # user.json_obj[1]
        # user.json_obj[1:5]
        #
        # It should be noted that users will still have
        # to validate / cast fields to the type they are expecting
        # for any strict type binding or nested index calls to work, e.g.
        #
        # isinstance(user.json_obj, dict)
        # cast(Dict[str, Any], user.json_obj)
        # prisma.validate(ExpectedType, user.json_obj)  # NOTE: not implemented yet

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants