Potential Bug: JSONField Serializer Failed to Validate null
Json Value in HTML Form
#9497
Unanswered
yitistica
asked this question in
Potential Issue
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Summary
It seems that the
JSONField
serializer in Django Rest Framework (DRF) does not correctly validatenull
values when the input comes from an HTML form, which also causes inconsistent behaviour between HTML form submissions and API client requests.to reproduce:
Create a serializer with json_data field and does not allow null value:
test:
Expected Behavior
The serializer should raise a validation error when allow_null=False and the input is 'null', regardless of the input source (HTML form or API client).
Actual Behavior
Root Cause
data process:
within Serializer.to_internal_value(self, data):
During validation (2.1.1), where
JsonString('null')
is passed to JsonField's parent validate_empty_values(), the method implemented in the parent class does not treat it as a null/empty value.Possible Fix
Alternatively, 2. convert 'null' in JsonField.get_value(dictionary) method. (I do see some conversion logic implemented in other subclasses of Field regarding empty value).
Beta Was this translation helpful? Give feedback.
All reactions