-
Notifications
You must be signed in to change notification settings - Fork 7
Fix TypeError: Object of type __proxy__ is not JSON serializable em task_migrate_and_publish_articles #930
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 3 commits
19ded80
14921d7
10fd2dc
fc84f73
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -97,6 +97,50 @@ def test_high_surrogate_handled(self): | |||||
| json.dumps(result) # Must not raise | ||||||
| self.assertNotIn("\ud800", result) | ||||||
|
|
||||||
| def test_django_lazy_proxy_converted_to_string(self): | ||||||
| """Django lazy translation objects (__proxy__) must be converted to str.""" | ||||||
| from django.utils.translation import gettext_lazy as _ | ||||||
|
|
||||||
| lazy_text = _("Select journals by collection") | ||||||
| result = sanitize_for_json(lazy_text) | ||||||
| self.assertIsInstance(result, str) | ||||||
| self.assertEqual(result, "Select journals by collection") | ||||||
|
||||||
| self.assertEqual(result, "Select journals by collection") | |
| self.assertEqual(result, str(lazy_text)) |
Copilot
AI
Apr 26, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These new tests import Django (gettext_lazy) but the file still exposes a __main__ runner (python proc/tests.py). Running it directly will now fail unless Django settings are configured. Either remove the __main__ block or configure DJANGO_SETTINGS_MODULE/django.setup() there to keep the standalone runner functional.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sanitize_for_jsonnow converts unknown objects withstr(obj), but the resulting string is returned without re-running the surrogate sanitization. If__str__()(or a lazy translation) yields a string containing surrogate code points, this reintroduces the original PostgreSQL/JSON rejection. Consider sanitizing the string result as well (e.g., passstr(obj)back throughsanitize_for_json).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot teste se obj é serializavel usando try/except e json.dumps, aí caso contário, sanitiza
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Implementado em fc84f73. Agora
sanitize_for_jsonusatry/except json.dumps(obj)para testar serializabilidade: se o objeto já for serializável, é retornado inalterado; caso contrário, é convertido parastr()e re-passado recursivamente pela própria função (para também tratar surrogates que__str__()possa produzir).