Changing event rule action type messes up Conditions field #9104

Closed
opened 2025-12-29 20:45:29 +01:00 by adam · 2 comments
Owner

Originally created by @markkuleinio on GitHub (Jan 18, 2024).

Originally assigned to: @jeremystretch on GitHub.

Deployment Type

Self-hosted

NetBox Version

v3.7.1

Python Version

3.11

Steps to Reproduce

  1. Go to Operations - Event Rules - Add
  2. Set Conditions = { "and": [{"attr": "status.value", "value": "deprecated"}]}
  3. Select Action type = Script

Expected Behavior

Conditions field stays as-is

Observed Behavior

Conditions field is rewritten as invalid JSON:

"{ \"and\": [{\"attr\": \"status.value\", \"value\": \"deprecated\"}]}"

The quoting is added with every change of the Action type.

This also happens if editing an existing event rule and changing the Action type.

Workaround

  • Copy the Conditions field somewhere
  • Change the Action type
  • Paste the Conditions field back
Originally created by @markkuleinio on GitHub (Jan 18, 2024). Originally assigned to: @jeremystretch on GitHub. ### Deployment Type Self-hosted ### NetBox Version v3.7.1 ### Python Version 3.11 ### Steps to Reproduce 1. Go to **Operations - Event Rules - Add** 2. Set Conditions = `{ "and": [{"attr": "status.value", "value": "deprecated"}]}` 3. Select Action type = Script ### Expected Behavior Conditions field stays as-is ### Observed Behavior Conditions field is rewritten as invalid JSON: `"{ \"and\": [{\"attr\": \"status.value\", \"value\": \"deprecated\"}]}"` The quoting is added with every change of the Action type. This also happens if editing an existing event rule and changing the Action type. ### Workaround - Copy the Conditions field somewhere - Change the Action type - Paste the Conditions field back
adam added the type: bugstatus: acceptedseverity: mediumtopic: event rules labels 2025-12-29 20:45:29 +01:00
adam closed this issue 2025-12-29 20:45:29 +01:00
Author
Owner

@markkuleinio commented on GitHub (Jan 18, 2024):

Btw, the error message (that happens if the event rule with the modified Conditions is saved) is slightly incorrect:

Ruleset must be a dictionary, not <class 'django.forms.fields.JSONString'>.
Enter conditions in JSON format.

A Python dictionary is not necessarily valid JSON (take for example None vs. null, or single-quotes vs. double-quotes)

@markkuleinio commented on GitHub (Jan 18, 2024): Btw, the error message (that happens if the event rule with the modified Conditions is saved) is slightly incorrect: **Ruleset must be a dictionary, not <class 'django.forms.fields.JSONString'>. Enter conditions in [JSON](https://json.org/) format.** A Python dictionary is not necessarily valid JSON (take for example `None` vs. `null`, or single-quotes vs. double-quotes)
Author
Owner

@jeremystretch commented on GitHub (Jan 23, 2024):

This is occurring because the initial data passed as a query parameter when re-rendering the form is interpreted as a string and automatically escaped. The same behavior can be observed by manually passing initial data to the form. For instance, GET /extras/event-rules/add/?conditions={"foo": 123} populates the value "{\"foo\": 123}" (a string) into the conditions field.

We need some logic in place to detect and load initial data as JSON, rather than treating it as a string. @DanSheps I'm going to take this one if you don't mind as I think we can fix this pretty easily under the field class' prepare_value() method.

@jeremystretch commented on GitHub (Jan 23, 2024): This is occurring because the initial data passed as a query parameter when re-rendering the form is interpreted as a string and automatically escaped. The same behavior can be observed by manually passing initial data to the form. For instance, `GET /extras/event-rules/add/?conditions={"foo": 123}` populates the value `"{\"foo\": 123}"` (a string) into the conditions field. We need some logic in place to detect and load initial data as JSON, rather than treating it as a string. @DanSheps I'm going to take this one if you don't mind as I think we can fix this pretty easily under the field class' `prepare_value()` method.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#9104