From f9d479c37cb48334575d18e9e0039a677d63d542 Mon Sep 17 00:00:00 2001 From: Egor fureunoir Gorbunov Date: Mon, 9 Mar 2026 18:41:30 +0300 Subject: [PATCH] feat(fields): enhance encryption in JSONField with direct processing Replace parent method call in `get_prep_value` with direct encryption logic to prevent errors caused by incorrect JSON parsing. This ensures the proper handling of JSON strings and avoids reliance on parent class behavior. --- schon/fields.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/schon/fields.py b/schon/fields.py index c98e9d2d..2dd7a482 100644 --- a/schon/fields.py +++ b/schon/fields.py @@ -21,9 +21,17 @@ class EncryptedJSONTextField(EncryptedTextField): return super().formfield(**{"form_class": forms.JSONField, **kwargs}) def get_prep_value(self, value): - if value is not None and not isinstance(value, str): + # Encrypt directly instead of delegating to super().get_prep_value(). + # The parent chain (EncryptedFieldMixin → CharField) calls + # self.to_python() which parses the JSON string back to a dict, + # then str(dict) produces Python repr with single quotes — not JSON. + if value is None: + return None + if not isinstance(value, str): value = orjson.dumps(value, default=str).decode("utf-8") - return super().get_prep_value(value) + if not value: + return None + return self.f.encrypt(value.encode("utf-8")).decode("utf-8") def from_db_value(self, value, expression, connection): if value is None: