fix: use JsonValue for models.JSONField to accept dict input#58
Open
yulinfeng000 wants to merge 1 commit into
Open
fix: use JsonValue for models.JSONField to accept dict input#58yulinfeng000 wants to merge 1 commit into
yulinfeng000 wants to merge 1 commit into
Conversation
Pydantic's Json type expects serialized strings, so response schemas built from Django models with JSONField fail with json_type ValidationError when the ORM returns dicts. Switch the converter for both PostgreSQL JSON/HStore fields and Django 3.1+ models.JSONField to JsonValue, which accepts JSON-compatible Python objects directly. Updated the corresponding v2 test to assert the new (correct) behavior. Refs eadwinCode/django-ninja-extra#267
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes the
json_typeValidationError raised when aModelSchemabuilt from a Django model containingJSONFieldis populated from a database row.convert_postgres_field_to_stringandconvert_field_to_json_stringnow producepydantic.JsonValueinstead ofpydantic.Json.JsonValueaccepts the Python dict/list values the Django ORM returns;Jsonrequired a serialized string/bytes payload, which is the source of the bug.tests/test_v2_pydantic/test_converters.py::test_django_31_fieldsto assert the new (correct) behavior: dict input is now valid, and the JSON schema fragment isJsonValue's$refform.Related issue
Refs eadwinCode/django-ninja-extra#267
Note on Pydantic v1 compatibility
pydantic.JsonValuewas introduced in Pydantic 2.5. This PR's top-level import will fail under Pydantic v1, which is still permitted bypyproject.toml(pydantic>=1.7.4,...,<3.0.0).I'm happy to follow up with either:
Json(or plaindict) for Pydantic v1 behind anIS_PYDANTIC_V1branch, ORpydantic>=2.5inpyproject.tomland remove the v1 branches.Let me know which direction you prefer and I'll send a follow-up commit.
Test plan
pytest tests/test_v2_pydantic/test_converters.py::test_django_31_fieldspassesruff check ninja_schema testsclean on the modified filesModelSchemawith aJSONFieldaccepts{"any": "data"}directly without raisingValidationErrorpytest tests/test_v1_pydantic/...— see "Note on Pydantic v1" above