JSON serializer issue for Protection Rules defined in configuration.py #10641

Closed
opened 2025-12-29 21:34:02 +01:00 by adam · 2 comments
Owner

Originally created by @atownson on GitHub (Jan 9, 2025).

Originally assigned to: @atownson on GitHub.

Deployment Type

Self-hosted

Triage priority

N/A

NetBox Version

v4.1.11

Python Version

3.12

Steps to Reproduce

  1. Create a custom Protection_Rule in the configuration.py. Example:
from extras.validators import CustomValidator

# Prevent IP addresses from being deleted by a cascading delete
class IpAddressProtectionRule(CustomValidator):
    def validate(self, instance, request):
        if hasattr(instance, 'primary_ip') and instance.primary_ip is not None:
            self.fail('This instance has a related IP address(es).')


PROTECTION_RULES = {
    'dcim.device': (
        IpAddressProtectionRule(),
    ),
}
  1. Restart NetBox services
  2. Navigate to Admin -> System

Expected Behavior

The system detail page is returned.

Observed Behavior

An exception is thrown:
image

Originally created by @atownson on GitHub (Jan 9, 2025). Originally assigned to: @atownson on GitHub. ### Deployment Type Self-hosted ### Triage priority N/A ### NetBox Version v4.1.11 ### Python Version 3.12 ### Steps to Reproduce 1. Create a custom `Protection_Rule` in the configuration.py. Example: ``` from extras.validators import CustomValidator # Prevent IP addresses from being deleted by a cascading delete class IpAddressProtectionRule(CustomValidator): def validate(self, instance, request): if hasattr(instance, 'primary_ip') and instance.primary_ip is not None: self.fail('This instance has a related IP address(es).') PROTECTION_RULES = { 'dcim.device': ( IpAddressProtectionRule(), ), } ``` 2. Restart NetBox services 3. Navigate to Admin -> System ### Expected Behavior The system detail page is returned. ### Observed Behavior An exception is thrown: ![image](https://github.com/user-attachments/assets/041d5221-2562-4b68-8b9a-19b495993b44)
adam added the type: bugstatus: acceptedseverity: medium labels 2025-12-29 21:34:02 +01:00
adam closed this issue 2025-12-29 21:34:02 +01:00
Author
Owner

@atownson commented on GitHub (Jan 9, 2025):

I believe the fix for this is to update netbox/netbox/tempates/core/inc/config_data.html as follows:
Current:

...
  <tr>
    <th scope="row" class="border-0 ps-3">{% trans "Protection rules" %}</th>
    {% if config.PROTECTION_RULES %}
      <td class="border-0"><pre>{{ config.PROTECTION_RULES|json }}</pre></td>
    {% else %}
      <td class="border-0">{{ ''|placeholder }}</td>
    {% endif %}
  </tr>
...

Proposed change:

...
  <tr>
    <th scope="row" class="border-0 ps-3">{% trans "Protection rules" %}</th>
    {% if config.PROTECTION_RULES %}
      <td class="border-0"><pre>{{ config.PROTECTION_RULES }}</pre></td>
    {% else %}
      <td class="border-0">{{ ''|placeholder }}</td>
    {% endif %}
  </tr>
...

This will match the CUSTOM_VALIDATORS block above it. I can submit a PR for this if approved.

@atownson commented on GitHub (Jan 9, 2025): I believe the fix for this is to update **netbox/netbox/tempates/core/inc/config_data.html** as follows: Current: ``` ... <tr> <th scope="row" class="border-0 ps-3">{% trans "Protection rules" %}</th> {% if config.PROTECTION_RULES %} <td class="border-0"><pre>{{ config.PROTECTION_RULES|json }}</pre></td> {% else %} <td class="border-0">{{ ''|placeholder }}</td> {% endif %} </tr> ... ``` Proposed change: ``` ... <tr> <th scope="row" class="border-0 ps-3">{% trans "Protection rules" %}</th> {% if config.PROTECTION_RULES %} <td class="border-0"><pre>{{ config.PROTECTION_RULES }}</pre></td> {% else %} <td class="border-0">{{ ''|placeholder }}</td> {% endif %} </tr> ... ``` This will match the CUSTOM_VALIDATORS block above it. I can submit a PR for this if approved.
Author
Owner

@jeremystretch commented on GitHub (Jan 13, 2025):

Thanks @atownson, I've assigned this to you.

@jeremystretch commented on GitHub (Jan 13, 2025): Thanks @atownson, I've assigned this to you.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#10641