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
16.21k stars 2.59k forks source link

DeviceType_Count on Manufacturer #17976

Open ZionDials opened 3 days ago

ZionDials commented 3 days ago

Deployment Type

Self-hosted

Triage priority

I volunteer to perform this work (if approved)

NetBox Version

v4.1.6

Python Version

3.10

Steps to Reproduce

GET /api/dcim/device-types/

{
    "count": 2424,
    "next": "https://netbox.com/api/dcim/device-types/?limit=50&offset=50",
    "previous": null,
    "results": [
        {
            "id": 1112,
            "url": "https://netbox.com/api/dcim/device-types/1112/",
            "display_url": "https://netbox.com/dcim/device-types/1112/",
            "display": "Thunder 4440 ADC",
            "manufacturer": {
                "id": 47,
                "url": "https://netbox.com/api/dcim/manufacturers/47/",
                "display": "A10",
                "name": "A10",
                "slug": "a10",
                "description": ""
            },
            "default_platform": null,
            "model": "Thunder 4440 ADC",
            "slug": "a10-thunder-4440-adc",
            "part_number": "TH4440",
            "u_height": 1.0,
            "exclude_from_utilization": false,
            "is_full_depth": true,
            "subdevice_role": null,
            "airflow": null,
            "weight": null,
            "weight_unit": null,
            "front_image": null,
            "rear_image": null,
            "description": "",
            "comments": "[A10 Thunder 4440 Applcation Delivery Controller Datasheet](https://www.a10networks.com/wp-content/uploads/A10-DS-Thunder-ADC.pdf)",
            "tags": [],
            "custom_fields": {},
            "created": "2024-10-28T06:49:53.123905-04:00",
            "last_updated": "2024-10-28T06:49:53.123918-04:00",
            "device_count": 0,
            "console_port_template_count": 0,
            "console_server_port_template_count": 0,
            "power_port_template_count": 0,
            "power_outlet_template_count": 0,
            "interface_template_count": 8,
            "front_port_template_count": 0,
            "rear_port_template_count": 0,
            "device_bay_template_count": 0,
            "module_bay_template_count": 0,
            "inventory_item_template_count": 0
        }
    ]
}

Note "devicetype_count" is missing.

Expected Behavior

The expected response, according to the OpenAPI specification for Netbox:

GET /api/dcim/device-types/

{
    "count": 2424,
    "next": "https://netbox.com/api/dcim/device-types/?limit=50&offset=50",
    "previous": null,
    "results": [
        {
            "id": 1112,
            "url": "https://netbox.com/api/dcim/device-types/1112/",
            "display_url": "https://netbox.com/dcim/device-types/1112/",
            "display": "Thunder 4440 ADC",
            "manufacturer": {
                "id": 47,
                "url": "https://netbox.com/api/dcim/manufacturers/47/",
                "display": "A10",
                "name": "A10",
                "slug": "a10",
                "description": "",
                "devicetype_count": 1
            },
            "default_platform": null,
            "model": "Thunder 4440 ADC",
            "slug": "a10-thunder-4440-adc",
            "part_number": "TH4440",
            "u_height": 1.0,
            "exclude_from_utilization": false,
            "is_full_depth": true,
            "subdevice_role": null,
            "airflow": null,
            "weight": null,
            "weight_unit": null,
            "front_image": null,
            "rear_image": null,
            "description": "",
            "comments": "[A10 Thunder 4440 Applcation Delivery Controller Datasheet](https://www.a10networks.com/wp-content/uploads/A10-DS-Thunder-ADC.pdf)",
            "tags": [],
            "custom_fields": {},
            "created": "2024-10-28T06:49:53.123905-04:00",
            "last_updated": "2024-10-28T06:49:53.123918-04:00",
            "device_count": 0,
            "console_port_template_count": 0,
            "console_server_port_template_count": 0,
            "power_port_template_count": 0,
            "power_outlet_template_count": 0,
            "interface_template_count": 8,
            "front_port_template_count": 0,
            "rear_port_template_count": 0,
            "device_bay_template_count": 0,
            "module_bay_template_count": 0,
            "inventory_item_template_count": 0
        }
    ]
}

Observed Behavior

According to the OpenAPI specification BriefManufacturer has a required field of "devicetype_count". However, when querying DeviceTypes and ModuleTypes, the field is not populated and causes SDKs using the OpenAPI generator, to fail.

Go-Netbox Creating Manufacture errors out because of missing required property devicetype_count #165

jeremystretch commented 1 day ago

The devicetype_count field is being omitted from the nested representation of manufacturers because the dynamic annotation logic does not recurse for nested serializers. (The field is populated as expected when querying the manufacturers endpoint with ?brief=True.)

While it's feasible to fix this by explicitly attaching the annotation to DeviceTypeViewSet, we should instead extend the dynamic logic to account for nested fields, as it's very likely there are similar instances in other nested serializers that also need to be addressed.

jeremystretch commented 1 day ago

@ZionDials just realized that you volunteered for this. Assigning to you, thanks!