Webhook: Object of type Decimal is not JSON serializable #7553

Closed
opened 2025-12-29 20:25:10 +01:00 by adam · 5 comments
Owner

Originally created by @janhlavin on GitHub (Jan 24, 2023).

NetBox version

v3.4.3

Python version

3.10

Steps to Reproduce

  1. create "webhook" (parameters below)
  2. create "site"
  3. create "device role"
  4. create "manufacturer"
  5. create "device type" with "height (U)" greater then 0
  6. create "rack" with "height (U)" greater then 0
  7. create "device" in rack position

Webhook parameters:

URL: irrelevant for reproduction
Events: create + update
Assigned Models: DCIM > device
HTTP method: POST
HTTP content type: application/json
Body template: {"data": {{ data | tojson }}, "snapshots": {{ snapshots | tojson }}}

Expected Behavior

webhook worker send request to URL

Observed Behavior

webhook worker (job) crash with exception:

Traceback (most recent call last):
  File "/opt/netbox/venv/lib/python3.10/site-packages/rq/worker.py", line 1111, in perform_job
    rv = job.perform()
  File "/opt/netbox/venv/lib/python3.10/site-packages/rq/job.py", line 923, in perform
    self._result = self._execute()
  File "/opt/netbox/venv/lib/python3.10/site-packages/rq/job.py", line 946, in _execute
    result = self.func(*self.args, **self.kwargs)
  File "/opt/netbox/netbox/extras/webhooks_worker.py", line 62, in process_webhook
    body = webhook.render_body(context)
  File "/opt/netbox/netbox/extras/models/models.py", line 193, in render_body
    return render_jinja2(self.body_template, context)
  File "/opt/netbox/netbox/utilities/utils.py", line 336, in render_jinja2
    return environment.from_string(source=template_code).render(**context)
  File "/opt/netbox/venv/lib/python3.10/site-packages/jinja2/environment.py", line 1301, in render
    self.environment.handle_exception()
  File "/opt/netbox/venv/lib/python3.10/site-packages/jinja2/environment.py", line 936, in handle_exception
    raise rewrite_traceback_stack(source=source)
  File "<template>", line 1, in top-level template code
  File "/opt/netbox/venv/lib/python3.10/site-packages/jinja2/filters.py", line 1688, in do_tojson
    return htmlsafe_json_dumps(value, dumps=dumps, **kwargs)
  File "/opt/netbox/venv/lib/python3.10/site-packages/jinja2/utils.py", line 658, in htmlsafe_json_dumps
    dumps(obj, **kwargs)
  File "/usr/lib/python3.10/json/__init__.py", line 238, in dumps
    **kw).encode(obj)
  File "/usr/lib/python3.10/json/encoder.py", line 199, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/usr/lib/python3.10/json/encoder.py", line 257, in iterencode
    return _iterencode(o, 0)
  File "/usr/lib/python3.10/json/encoder.py", line 179, in default
    raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type Decimal is not JSON serializable
Originally created by @janhlavin on GitHub (Jan 24, 2023). ### NetBox version v3.4.3 ### Python version 3.10 ### Steps to Reproduce 1. create "webhook" (parameters below) 1. create "site" 1. create "device role" 1. create "manufacturer" 1. create "device type" with "height (U)" greater then 0 1. create "rack" with "height (U)" greater then 0 1. create "device" in rack position Webhook parameters: URL: irrelevant for reproduction Events: `create` + `update` Assigned Models: `DCIM > device` HTTP method: `POST` HTTP content type: `application/json` Body template: `{"data": {{ data | tojson }}, "snapshots": {{ snapshots | tojson }}}` ### Expected Behavior webhook worker send request to URL ### Observed Behavior webhook worker (job) crash with exception: ``` Traceback (most recent call last): File "/opt/netbox/venv/lib/python3.10/site-packages/rq/worker.py", line 1111, in perform_job rv = job.perform() File "/opt/netbox/venv/lib/python3.10/site-packages/rq/job.py", line 923, in perform self._result = self._execute() File "/opt/netbox/venv/lib/python3.10/site-packages/rq/job.py", line 946, in _execute result = self.func(*self.args, **self.kwargs) File "/opt/netbox/netbox/extras/webhooks_worker.py", line 62, in process_webhook body = webhook.render_body(context) File "/opt/netbox/netbox/extras/models/models.py", line 193, in render_body return render_jinja2(self.body_template, context) File "/opt/netbox/netbox/utilities/utils.py", line 336, in render_jinja2 return environment.from_string(source=template_code).render(**context) File "/opt/netbox/venv/lib/python3.10/site-packages/jinja2/environment.py", line 1301, in render self.environment.handle_exception() File "/opt/netbox/venv/lib/python3.10/site-packages/jinja2/environment.py", line 936, in handle_exception raise rewrite_traceback_stack(source=source) File "<template>", line 1, in top-level template code File "/opt/netbox/venv/lib/python3.10/site-packages/jinja2/filters.py", line 1688, in do_tojson return htmlsafe_json_dumps(value, dumps=dumps, **kwargs) File "/opt/netbox/venv/lib/python3.10/site-packages/jinja2/utils.py", line 658, in htmlsafe_json_dumps dumps(obj, **kwargs) File "/usr/lib/python3.10/json/__init__.py", line 238, in dumps **kw).encode(obj) File "/usr/lib/python3.10/json/encoder.py", line 199, in encode chunks = self.iterencode(o, _one_shot=True) File "/usr/lib/python3.10/json/encoder.py", line 257, in iterencode return _iterencode(o, 0) File "/usr/lib/python3.10/json/encoder.py", line 179, in default raise TypeError(f'Object of type {o.__class__.__name__} ' TypeError: Object of type Decimal is not JSON serializable ```
adam added the type: bug label 2025-12-29 20:25:10 +01:00
adam closed this issue 2025-12-29 20:25:10 +01:00
Author
Owner

@jeremystretch commented on GitHub (Jan 24, 2023):

Thank you for opening a bug report. I was unable to reproduce the reported behavior on NetBox v3.4.3. Please re-confirm the reported behavior on the current stable release and adjust your post above as necessary. Remember to provide detailed steps that someone else can follow using a clean installation of NetBox to reproduce the issue. Remember to include the steps taken to create any initial objects or other data.

Do you have anything defined for the webhook body?

@jeremystretch commented on GitHub (Jan 24, 2023): Thank you for opening a bug report. I was unable to reproduce the reported behavior on NetBox v3.4.3. Please re-confirm the reported behavior on the current stable release and adjust your post above as necessary. Remember to provide detailed steps that someone else can follow using a clean installation of NetBox to reproduce the issue. Remember to include the steps taken to create any initial objects or other data. Do you have anything defined for the webhook body?
Author
Owner

@janhlavin commented on GitHub (Jan 25, 2023):

Do you have anything defined for the webhook body?

yes, I updated information for replication

@janhlavin commented on GitHub (Jan 25, 2023): > Do you have anything defined for the webhook body? yes, I updated information for replication
Author
Owner

@jeremystretch commented on GitHub (Jan 25, 2023):

It seems that Jinja's built-in filter doesn't know how to handle Decimal types. But the data you're trying to embed in the payload is already there by default: Just remove your custom body template and NetBox will default to providing that same information.

@jeremystretch commented on GitHub (Jan 25, 2023): It seems that Jinja's built-in filter doesn't know how to handle Decimal types. But the data you're trying to embed in the payload is already there by default: Just remove your custom body template and NetBox will default to providing that same information.
Author
Owner

@janhlavin commented on GitHub (Feb 1, 2023):

It seems that Jinja's built-in filter doesn't know how to handle Decimal types. But the data you're trying to embed in the payload is already there by default: Just remove your custom body template and NetBox will default to providing that same information.

yes, with default body is working.

@janhlavin commented on GitHub (Feb 1, 2023): > It seems that Jinja's built-in filter doesn't know how to handle Decimal types. But the data you're trying to embed in the payload is already there by default: Just remove your custom body template and NetBox will default to providing that same information. yes, with default body is working.
Author
Owner

@jeremystretch commented on GitHub (Feb 1, 2023):

Alright, I'm going to close this out as it seems to be both unnecessary and also something we can't easily add support for. (Jinja2 uses Python's native JSON library, which doesn't yet support the Decimal type.) We can take another look at it if a pressing use case arises, but I imagine it's not a common need.

@jeremystretch commented on GitHub (Feb 1, 2023): Alright, I'm going to close this out as it seems to be both unnecessary and also something we can't easily add support for. (Jinja2 uses Python's native JSON library, which doesn't yet support the Decimal type.) We can take another look at it if a pressing use case arises, but I imagine it's not a common need.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#7553