netbox-community / netbox

The premier source of truth powering network automation. Open source under Apache 2. Try NetBox Cloud free: https://netboxlabs.com/free-netbox-cloud/
http://netboxlabs.com/oss/netbox/
Apache License 2.0
15.81k stars 2.54k forks source link

TypeError when trying to list custom scripts via API #17468

Open v0tti opened 1 week ago

v0tti commented 1 week ago

Deployment Type

Self-hosted

NetBox Version

v4.1.0

Python Version

3.11

Steps to Reproduce

  1. Verify no custom scripts exist in NetBox.
  2. Create an upload a Python file with the following content as a custom script:
    
    from extras.scripts import *

class DemoScript(Script): description = StringVar()

def run(self, data, commit):
    return
4. Access `/api/extras/scripts/`. The following error is shown:

### Expected Behavior

I expect to get a valid JSON listing all existing custom scripts.

### Observed Behavior

A `TypeError` is raised.

<class 'TypeError'>

Object of type StringVar is not JSON serializable

Python version: 3.11.9 NetBox version: 4.1.0



This only happens if an input like `StringVar` (also verified with `TextVar` is defined). The scripts are listed and executed like expected and this issue only affects API requests.
jeremystretch commented 1 week ago

The description field defined on the Script class collides with its description property (which returns self.Meta.description).

The obvious workaround is to rename the field in the script, but within NetBox we should raise an error for such conflicts and/or namespace the properties somehow.

v0tti commented 1 week ago

Thanks for your comment. I have renamed the description StringVar field on all our custom scripts and the REST API is working fine now.

This was not an issue until we upgraded to NetBox 4.1 and even then only the API was having problems. The scripts themselves worked fine. I also could not find any mention of this in the announced breaking change.