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
Nested json is transformed to string when used “$type” key in object #21454
Comments
Thank you, I thought that I was getting crazy but indeed it's easy to reproduce, just try saving an array of objects with $type key in a json column and you'll see that the objects are serialized as strings instead, making it impossible to query inside or even read back without having to use JSON.parse recursively. |
Thanks for confirmation. |
We are having the same issue. We migrated to Prisma and we can't fetch data from one of our tables because when you fetch data with a structure containing "$type" then it explodes with error: Do you have any workaround for this bug? @janpio This issue is pretty critical for us, as there's no easy fix on our side. |
can confirm this bug in prisma 5.1.1 |
found a workaround. change from
|
@karanssj4 another problem is that you can't fetch this kind of data from database because you get exception error :<
I don't understand how this bug is not high priority right now |
@niba i've not seen this issue while reading back from db, only while inserting it. we used raw queries for insrting and prisma crud methods for reading, it worked can you share the read opearation that messes it up for you? |
Reproduction for $type key in JSON issue. It works correctly if $type is a top level key in JSON field, but fails if it is nested.
Reproduced the issue in #22714 |
In JSON protocol, we use `$type` key as a special marker for the types that can not be represented in JSON natively. We did not want to make `$type` identifier reserved, so we took some precautions: if during encoding, at any point, `$type` key would be seen in user's input we encode whole object as `{"$type": "Json", "value": [Serialized object] }`. Engine handled those cases correctly, when `$type: Json` is encountered at the top level, but not when it is nested. In that case, `ArgumentValue`-to-`JSON` serialization just picked `value` key and saved it as a string. Fixed by moving Json value into it's own kind of `ArgumentValue`, that are serialized to json as-is. Fix prisma/prisma#21454
In JSON protocol, we use `$type` key as a special marker for the types that can not be represented in JSON natively. We did not want to make `$type` identifier reserved, so we took some precautions: if during encoding, at any point, `$type` key would be seen in user's input we encode whole object as `{"$type": "Json", "value": [Serialized object] }`. Engine handled those cases correctly, when `$type: Json` is encountered at the top level, but not when it is nested. In that case, `ArgumentValue`-to-`JSON` serialization just picked `value` key and saved it as a string. Fixed by moving Json value into it's own kind of `ArgumentValue`, that are serialized to json as-is. Fix prisma/prisma#21454
Introduce another special value to JSON protocol, `"$type": "Raw"`. When encountered, no other nested `$type` keys would be interpreted as special and will be written to DB as is. Main usecase is JSON column values with user-provided `$type` keys. This is an alternative to #4668, that might look cleaner on the engine side. Part of the fix for prisma/prisma#21454, will require client adjustments as well.
Introduce another special value to JSON protocol, `"$type": "Raw"`. When encountered, no other nested `$type` keys would be interpreted as special and will be written to DB as is. Main usecase is JSON column values with user-provided `$type` keys. This is an alternative to #4668, that might look cleaner on the engine side. Part of the fix for prisma/prisma#21454, will require client adjustments as well.
Introduce another special value to JSON protocol, `"$type": "Raw"`. When encountered, no other nested `$type` keys would be interpreted as special and will be written to DB as is. Main usecase is JSON column values with user-provided `$type` keys. This is an alternative to #4668, that might look cleaner on the engine side. Part of the fix for prisma/prisma#21454, will require client adjustments as well.
Introduce another special value to JSON protocol, `"$type": "Raw"`. When encountered, no other nested `$type` keys would be interpreted as special and will be written to DB as is. Main usecase is JSON column values with user-provided `$type` keys. This is an alternative to #4668, that might look cleaner on the engine side. Part of the fix for prisma/prisma#21454, will require client adjustments as well.
In JSON protocol, we use `$type` key as a special marker for the types that can not be represented in JSON natively. We did not want to make `$type` identifier reserved, so we took some precautions: if during encoding, at any point, `$type` key would be seen in user's input we encode whole object as `{"$type": "Json", "value": [Serialized object] }`. Engine handled those cases correctly, when `$type: Json` is encountered at the top level, but not when it is nested. In that case, `ArgumentValue`-to-`JSON` serialization just picked `value` key and saved it as a string. Fixed by moving Json value into it's own kind of `ArgumentValue`, that are serialized to json as-is. Fix prisma/prisma#21454
Introduce another special value to JSON protocol, `"$type": "Raw"`. When encountered, no other nested `$type` keys would be interpreted as special and will be written to DB as is. Main usecase is JSON column values with user-provided `$type` keys. This is an alternative to #4668, that might look cleaner on the engine side. Part of the fix for prisma/prisma#21454, will require client adjustments as well.
Works on top of prisma/prisma-engines#4670. Adds `$type: Raw` special case to JSON protocol and replaces `$type: Json` encoding with it. See engine PR for detailed description of the problem. Fix #21454
* qe: Fix nested objects with `$type` key in JSON protocol Introduce another special value to JSON protocol, `"$type": "Raw"`. When encountered, no other nested `$type` keys would be interpreted as special and will be written to DB as is. Main usecase is JSON column values with user-provided `$type` keys. This is an alternative to #4668, that might look cleaner on the engine side. Part of the fix for prisma/prisma#21454, will require client adjustments as well. * Fix & move the test
Works on top of prisma/prisma-engines#4670. Adds `$type: Raw` special case to JSON protocol and replaces `$type: Json` encoding with it. See engine PR for detailed description of the problem. Fix #21454
Works on top of prisma/prisma-engines#4670. Adds `$type: Raw` special case to JSON protocol and replaces `$type: Json` encoding with it. See engine PR for detailed description of the problem. Fix #21454
Hello @tomasz-py @karanssj4 @niba. |
Bug description
Json value which has array of objects where one of keys is "$type" is transformed to string
const data = await prisma.test.create({ data: { value: { options: [{ $type: "z" }, { $type: 1 }], }, }, });
data.value will looks like:
{ options: [ '{"$type":"z"}', '{"$type":1}' ] }
instead of
{ options: [ {'$type': 'z'}, {'$type': 1 } ] }
the same situation is for object in object:
const data = await prisma.test.create({ data: { value: { options: { $type: "test", }, }, }, });
data.value will contain options as string instead of json
{ options: '{"$type":"test"}' }
Additional info:
its stored properly if object key is $test, so its only breaks when key is "$type"
How to reproduce
Expected behavior
Prisma should store array of jsons as jsons instead of transforming them into string
Prisma information
Environment & setup
Prisma Version
The text was updated successfully, but these errors were encountered: