tortoise / tortoise/tortoise-orm

JSONField not accepting string values

Open
#1,080 0 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
5.6k
Forks
516
Avg merge
2d 21h
Merged PRs (30d)
9

Description

Describe the bug

JSONField should be a generic field that accepts anything that can be "stringfied" to json, and that string be loaded back to their original values.

Currently, trying to pass a string to JSONField makes it try to decode it first, which raises an error saying that the value is not a valid json value.

To Reproduce

Try to set any JSONField to any string, like "foobar"

Expected behavior

The expected behaviour would be that any serializable value would be JSON stringified and then be converted back. E.g, using the json module:

In [1]: import json

In [2]: json.dumps("foobar")
Out[2]: '"foobar"'

In [3]: json.loads('"foobar"')
Out[3]: 'foobar'

In [4]: json.dumps(123)
Out[4]: '123'

In [5]: json.loads('123')
Out[5]: 123

More specifically, if I set a string on it, I expect a string back. If I pass an int to it, I expect an int back. If I pass some dict to it, I expect that dict back, as long as the encoder/decoder can encode/decode those attributes.

This is also the way most JSONField works, like django's.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating the JSONField implementation and reproduce the issue by assigning a string such as "foobar". Check the field's serialization and deserialization path; done means strings, integers, and dictionaries round-trip to their original values when supported by the encoder and decoder.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
databases
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.