From ffbc8b4b41afc63283128e98c679b54beee374ee Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Tue, 27 Aug 2024 13:56:38 -0400 Subject: [PATCH] Fix system config export --- netbox/core/views.py | 3 ++- netbox/utilities/json.py | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/netbox/core/views.py b/netbox/core/views.py index bc45089f1..230134cc3 100644 --- a/netbox/core/views.py +++ b/netbox/core/views.py @@ -574,13 +574,14 @@ class SystemView(UserPassesTestMixin, View): k: getattr(config, k) for k in sorted(params) }, } - response = HttpResponse(json.dumps(data, indent=4), content_type='text/json') + response = HttpResponse(json.dumps(data, cls=ConfigJSONEncoder, indent=4), content_type='text/json') response['Content-Disposition'] = 'attachment; filename="netbox.json"' return response plugins_table = tables.PluginTable(plugins, orderable=False) plugins_table.configure(request) + # Serialize any CustomValidator classes if hasattr(config, 'CUSTOM_VALIDATORS') and config.CUSTOM_VALIDATORS: config.CUSTOM_VALIDATORS = json.dumps(config.CUSTOM_VALIDATORS, cls=ConfigJSONEncoder, indent=4) diff --git a/netbox/utilities/json.py b/netbox/utilities/json.py index 926df898e..3114be1bf 100644 --- a/netbox/utilities/json.py +++ b/netbox/utilities/json.py @@ -20,11 +20,12 @@ class CustomFieldJSONEncoder(DjangoJSONEncoder): class ConfigJSONEncoder(DjangoJSONEncoder): """ - Override Django's built-in JSON encoder to save python functions as function names. + Override Django's built-in JSON encoder to serialize CustomValidator classes as strings. """ def default(self, o): from extras.validators import CustomValidator if issubclass(type(o), CustomValidator): return type(o).__name__ + return super().default(o)