Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 3 additions & 7 deletions ninja_schema/orm/utils/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from django.db import models
from django.db.models.fields import Field
from django.utils.encoding import force_str
from pydantic import AnyUrl, EmailStr, IPvAnyAddress, Json
from pydantic import AnyUrl, EmailStr, IPvAnyAddress, JsonValue
from pydantic.fields import Field as PydanticField
from typing_extensions import Annotated # F401

Expand Down Expand Up @@ -452,9 +452,7 @@ def convert_postgres_array_to_list(
def convert_postgres_field_to_string(
field: Field, **kwargs: DictStrAny
) -> t.Tuple[t.Type, PydanticField]:
python_type = Json
if field.null:
python_type = t.Optional[Json]
python_type = JsonValue
return construct_field_info(python_type, field)


Expand All @@ -476,7 +474,5 @@ def convert_postgres_range_to_string(
def convert_field_to_json_string(
field: Field, **kwargs: DictStrAny
) -> t.Tuple[t.Type, PydanticField]:
python_type = Json
if field.null:
python_type = t.Optional[Json]
python_type = JsonValue
return construct_field_info(python_type, field)
11 changes: 3 additions & 8 deletions tests/test_v2_pydantic/test_converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import pytest
from django.db import models
from django.db.models import Manager
from pydantic import ValidationError

from ninja_schema import ModelSchema
from ninja_schema.pydanticutils import IS_PYDANTIC_V1
Expand Down Expand Up @@ -303,6 +302,7 @@ class Config:

print(ModelNewFieldsSchema.schema())
assert ModelNewFieldsSchema.schema() == {
"$defs": {"JsonValue": {}},
"properties": {
"id": {
"anyOf": [{"type": "integer"}, {"type": "null"}],
Expand All @@ -311,11 +311,9 @@ class Config:
"title": "Id",
},
"jsonfield": {
"contentMediaType": "application/json",
"contentSchema": {},
"$ref": "#/$defs/JsonValue",
"description": "",
"title": "Jsonfield",
"type": "string",
},
"positivebigintegerfield": {
"description": "",
Expand All @@ -328,11 +326,8 @@ class Config:
"type": "object",
}

with pytest.raises(ValidationError):
ModelNewFieldsSchema(id=1, jsonfield={"any": "data"}, positivebigintegerfield=1)

obj = ModelNewFieldsSchema(
id=1, jsonfield=json.dumps({"any": "data"}), positivebigintegerfield=1
id=1, jsonfield={"any": "data"}, positivebigintegerfield=1
)
assert obj.dict() == {
"id": 1,
Expand Down