Get Device in REST API doesn't include DeviceType.device_count as described in Swagger schema #9799

Closed
opened 2025-12-29 21:22:52 +01:00 by adam · 6 comments
Owner

Originally created by @janhlavin on GitHub (Jun 5, 2024).

Deployment Type

Self-hosted

NetBox Version

v4.0.3

Python Version

3.11

Steps to Reproduce

  1. create Manufacturer, Device type, Device role, Site and Device
  2. call GET to /api/dcim/devices/$DeviceID/

Expected Behavior

return:

{
 "id": 1,
 "device_type": {
  "id": 1,
  "device_count": 1,
  "...": "..."
 },
 "...": "..."
}

Observed Behavior

return:

{
 "id": 1,
 "device_type": {
  "id": 1,
  "...": "..."
 },
 "...": "..."
}

Narrative (from comments below, edited somewhat for clarity)

The main trouble is that there's an inconsistency between the Swagger schema and the actual API responses. The actual response fails to validate against the example schema provided by Swagger. This causes problems when using strongly-typed API client frameworks to generate code that talks to NetBox.

I don't need the property device_count as a child of device_type. A change in the Swagger definition, declaring device_type to be of type NestedDeviceType is fine and will fix this bug for my purposes.

Originally created by @janhlavin on GitHub (Jun 5, 2024). ### Deployment Type Self-hosted ### NetBox Version v4.0.3 ### Python Version 3.11 ### Steps to Reproduce 1. create Manufacturer, Device type, Device role, Site and Device 2. call GET to /api/dcim/devices/$DeviceID/ ### Expected Behavior return: ```json { "id": 1, "device_type": { "id": 1, "device_count": 1, "...": "..." }, "...": "..." } ``` ### Observed Behavior return: ```json { "id": 1, "device_type": { "id": 1, "...": "..." }, "...": "..." } ``` ### Narrative (from comments below, edited somewhat for clarity) The main trouble is that there's an inconsistency between the Swagger schema and the actual API responses. The actual response fails to validate against the example schema provided by Swagger. This causes problems when using strongly-typed API client frameworks to generate code that talks to NetBox. I don't need the property `device_count` as a child of `device_type`. A change in the Swagger definition, declaring `device_type` to be of type `NestedDeviceType` is fine and will fix this bug for my purposes.
adam added the type: bugstatus: needs ownerseverity: medium labels 2025-12-29 21:22:52 +01:00
adam closed this issue 2025-12-29 21:22:52 +01:00
Author
Owner

@tgoodsell-tempus commented on GitHub (Jun 5, 2024):

I'm seeing similar behavior for the prefix_count attribute embedded in the vrf object of a Prefix:

  1. Call GET https://hostname/api/ipam/prefixes/

Return:

{
    "count": 4,
    "next": null,
    "previous": null,
    "results": [
        {
            "id": 3,
            "url": "https://hostname/api/ipam/prefixes/3/",
            "display": "10.64.0.0/11",
            "family": {
                "value": 4,
                "label": "IPv4"
            },
            "prefix": "10.64.0.0/11",
            "site": null,
            "vrf": {
                "id": 1,
                "url": "https://hostname/api/ipam/vrfs/1/",
                "display": "ZRO",
                "name": "ZRO",
                "rd": null,
                "description": ""
            },
            "tenant": null,
            "vlan": null,
            "status": {
                "value": "container",
                "label": "Container"
            },
            "role": null,
            "is_pool": true,
            "mark_utilized": false,
            "description": "ZRO",
            "comments": "",
            "tags": [],
            "custom_fields": {},
            "created": "2024-05-14T00:26:48.400795Z",
            "last_updated": "2024-05-14T00:26:48.400812Z",
            "children": 1,
            "_depth": 0
        },
        ...

Expected schema (from https://hostname/api/schema/swagger-ui/#/ipam/ipam_prefixes_list):

{
  "count": 123,
  "next": "http://api.example.org/accounts/?offset=400&limit=100",
  "previous": "http://api.example.org/accounts/?offset=200&limit=100",
  "results": [
    {
      "id": 0,
      "url": "string",
      "display": "string",
      "family": {
        "value": 4,
        "label": "IPv4"
      },
      "prefix": "string",
      "site": {
        "id": 0,
        "url": "string",
        "display": "string",
        "name": "string",
        "slug": "lt35brDofIze9mWTFO78eHPXsP-lqRLkbhUxRluQT66YdsdBXIAAdM1ZAIKNO_f-mR7PM5GDcKzhfsnZE-6kuX0W6MzqH",
        "description": "string"
      },
      "vrf": {
        "id": 0,
        "url": "string",
        "display": "string",
        "name": "string",
        "rd": "string",
        "description": "string",
        "prefix_count": 0
      },

Causes issues with strongly typed SDKs like the go-netbox sdk (ref: https://github.com/netbox-community/go-netbox/issues/177)

When calling GET https://hostname/api/ipam/vrfs/1/:

{
    "id": 1,
    "url": "https://hostname/api/ipam/vrfs/1/",
    "display": "ZRO",
    "name": "ZRO",
    "rd": null,
    "tenant": null,
    "enforce_unique": true,
    "description": "",
    "comments": "",
    "import_targets": [],
    "export_targets": [],
    "tags": [],
    "custom_fields": {},
    "created": "2024-05-14T00:25:30.457087Z",
    "last_updated": "2024-05-14T00:25:30.457103Z",
    "ipaddress_count": 0,
    "prefix_count": 2
}
@tgoodsell-tempus commented on GitHub (Jun 5, 2024): I'm seeing similar behavior for the `prefix_count` attribute embedded in the `vrf` object of a Prefix: 1. Call `GET https://hostname/api/ipam/prefixes/` Return: ``` { "count": 4, "next": null, "previous": null, "results": [ { "id": 3, "url": "https://hostname/api/ipam/prefixes/3/", "display": "10.64.0.0/11", "family": { "value": 4, "label": "IPv4" }, "prefix": "10.64.0.0/11", "site": null, "vrf": { "id": 1, "url": "https://hostname/api/ipam/vrfs/1/", "display": "ZRO", "name": "ZRO", "rd": null, "description": "" }, "tenant": null, "vlan": null, "status": { "value": "container", "label": "Container" }, "role": null, "is_pool": true, "mark_utilized": false, "description": "ZRO", "comments": "", "tags": [], "custom_fields": {}, "created": "2024-05-14T00:26:48.400795Z", "last_updated": "2024-05-14T00:26:48.400812Z", "children": 1, "_depth": 0 }, ... ``` Expected schema (from https://hostname/api/schema/swagger-ui/#/ipam/ipam_prefixes_list): ``` { "count": 123, "next": "http://api.example.org/accounts/?offset=400&limit=100", "previous": "http://api.example.org/accounts/?offset=200&limit=100", "results": [ { "id": 0, "url": "string", "display": "string", "family": { "value": 4, "label": "IPv4" }, "prefix": "string", "site": { "id": 0, "url": "string", "display": "string", "name": "string", "slug": "lt35brDofIze9mWTFO78eHPXsP-lqRLkbhUxRluQT66YdsdBXIAAdM1ZAIKNO_f-mR7PM5GDcKzhfsnZE-6kuX0W6MzqH", "description": "string" }, "vrf": { "id": 0, "url": "string", "display": "string", "name": "string", "rd": "string", "description": "string", "prefix_count": 0 }, ``` Causes issues with strongly typed SDKs like the `go-netbox` sdk (ref: https://github.com/netbox-community/go-netbox/issues/177) When calling `GET https://hostname/api/ipam/vrfs/1/`: ``` { "id": 1, "url": "https://hostname/api/ipam/vrfs/1/", "display": "ZRO", "name": "ZRO", "rd": null, "tenant": null, "enforce_unique": true, "description": "", "comments": "", "import_targets": [], "export_targets": [], "tags": [], "custom_fields": {}, "created": "2024-05-14T00:25:30.457087Z", "last_updated": "2024-05-14T00:25:30.457103Z", "ipaddress_count": 0, "prefix_count": 2 } ```
Author
Owner

@jeffgdotorg commented on GitHub (Jun 6, 2024):

Thanks for reporting a problem you've encountered in NetBox.

Do I understand correctly that you expect to see device_type.device_count in the response to /api/dcim/devices/{id}/ because the example schema produced by the Swagger UI specifies its presence?

If so, please update the issue body to clarify the reason for your expectation. As a person who's only somewhat familiar with OpenAPI, understanding why this is a problem took me quite some time.

@jeffgdotorg commented on GitHub (Jun 6, 2024): Thanks for reporting a problem you've encountered in NetBox. Do I understand correctly that you expect to see `device_type.device_count` in the response to `/api/dcim/devices/{id}/` because the example schema produced by the Swagger UI specifies its presence? If so, please update the issue body to clarify the reason for your expectation. As a person who's only somewhat familiar with OpenAPI, understanding why this is a problem took me quite some time.
Author
Owner

@janhlavin commented on GitHub (Jun 7, 2024):

main trouble is in inconsistency Swagger schema and real API responses - I don't need property "device_count" in "device_type" => change in Swagger definition, where "device_type" is type of "NestedDeviceType" is fine and fix this bug

@janhlavin commented on GitHub (Jun 7, 2024): main trouble is in inconsistency Swagger schema and real API responses - I don't need property "device_count" in "device_type" => change in Swagger definition, where "device_type" is type of "NestedDeviceType" is fine and fix this bug
Author
Owner

@jeffgdotorg commented on GitHub (Jun 7, 2024):

Thanks for clarifying the practical impact. I've adapted your previous comment and included it at the bottom of the issue body.

@jeffgdotorg commented on GitHub (Jun 7, 2024): Thanks for clarifying the practical impact. I've adapted your previous comment and included it at the bottom of the issue body.
Author
Owner

@jeffgdotorg commented on GitHub (Jun 7, 2024):

I just spotted #16085 which captures the same underlying problem as this issue. Resolving this one as a duplicate.

@jeffgdotorg commented on GitHub (Jun 7, 2024): I just spotted #16085 which captures the same underlying problem as this issue. Resolving this one as a duplicate.
Author
Owner

@jeffgdotorg commented on GitHub (Jun 7, 2024):

Duplicate of #16085

@jeffgdotorg commented on GitHub (Jun 7, 2024): Duplicate of #16085
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#9799