tortoise-orm icon indicating copy to clipboard operation
tortoise-orm copied to clipboard

JSON field in pydantic model creator should be set as dict type

Open lakshaythareja opened this issue 4 years ago • 3 comments

Is your feature request related to a problem? Please describe. Whenever I declare a field as fields.JSONFields and use it in a pydantic model by calling pydantic model creator it comes as string field in swagger. This leads to an error on sending the request through swagger on not changing the field type manually.

Describe the solution you'd like It should come as a dict field.

Describe alternatives you've considered

        # Json fields
        # elif field_type is fields.JSONField:
        #     pannotations[fname] = Any  # type: ignore

removing this in the file creator.py should help.

Additional context Add any other context about the feature request here.

lakshaythareja avatar Jul 31 '21 14:07 lakshaythareja

The decision to have Any was made here https://github.com/tortoise/tortoise-orm/pull/415/files#r429286765 @long2ice @grigi, would be dict[str, Any] better than Any?

For example, when converting a model to a pydantic schema, Any will cause issues. A payload with a regular string instate of a JSON value will be accepted by pydantic but will raise a FieldError, because a string is not a valid JSON. dict[str, Any] works well. Also, there is the issue pointed by @lakshaythareja, where using dict[str, Any] it would appear as {}

If both agreed with changing the JSONField type, I'm happy with doing a PR. BTW, thanks for your work done with Tortoise-ORM :raised_hands:

fullonic avatar Jan 05 '22 18:01 fullonic

Json only support list, what about Union[Dict[str, Any],List[Any]]?

long2ice avatar Jan 06 '22 00:01 long2ice

Any update on this?

lakshaythareja avatar Feb 15 '22 18:02 lakshaythareja