Inconsistent AvailablePrefixes API #6532

Closed
opened 2025-12-29 19:42:02 +01:00 by adam · 6 comments
Owner

Originally created by @darkweaver87 on GitHub (Jun 3, 2022).

NetBox version

v3.2.4

Python version

3.8

Steps to Reproduce

  1. Ask for a prefix creation:
curl -X POST -d '{"prefix_length":16}' -H 'Content-Type: application/json' -H 'Authorization: Token xxxx'
  1. this returns a single prefix instead of a list per specified here
{"id":119,"url":"http://localhost:8080/api/ipam/prefixes/119/","display":"10.4.0.0/16","family":{"value":4,"label":"IPv4"},"prefix":"10.4.0.0/16","site":null,"vrf":{"id":1,"url":"http://localhost:8080/api/ipam/vrfs/1/","display":"default","name":"default","rd":null},"tenant":null,"vlan":null,"status":{"value":"active","label":"Active"},"role":null,"is_pool":false,"mark_utilized":false,"description":"","tags":[],"custom_fields":{},"created":"2022-06-03T08:35:06.481023Z","last_updated":"2022-06-03T08:35:06.481047Z","children":0,"_depth":0}
  1. consequently the golang client uses the swagger schema and crashes with:
json: cannot unmarshal object into Go value of type []*models.Prefix

Expected Behavior

This API should return a list of one single element or may be split.

Observed Behavior

It returns either a list or a single element.

Originally created by @darkweaver87 on GitHub (Jun 3, 2022). ### NetBox version v3.2.4 ### Python version 3.8 ### Steps to Reproduce 1. Ask for a prefix creation: ```bash curl -X POST -d '{"prefix_length":16}' -H 'Content-Type: application/json' -H 'Authorization: Token xxxx' ``` 2. this returns a single prefix instead of a list per specified [here](https://github.com/netbox-community/netbox/blob/3fbf1f7e71801487abd3524797298d82dca4db4f/netbox/ipam/api/views.py#L181) ``` {"id":119,"url":"http://localhost:8080/api/ipam/prefixes/119/","display":"10.4.0.0/16","family":{"value":4,"label":"IPv4"},"prefix":"10.4.0.0/16","site":null,"vrf":{"id":1,"url":"http://localhost:8080/api/ipam/vrfs/1/","display":"default","name":"default","rd":null},"tenant":null,"vlan":null,"status":{"value":"active","label":"Active"},"role":null,"is_pool":false,"mark_utilized":false,"description":"","tags":[],"custom_fields":{},"created":"2022-06-03T08:35:06.481023Z","last_updated":"2022-06-03T08:35:06.481047Z","children":0,"_depth":0} ``` 3. consequently the golang client uses the swagger schema and crashes with: ``` json: cannot unmarshal object into Go value of type []*models.Prefix ``` ### Expected Behavior This API should return a list of one single element or may be split. ### Observed Behavior It returns either a list or a single element.
adam added the type: bug label 2025-12-29 19:42:02 +01:00
adam closed this issue 2025-12-29 19:42:02 +01:00
Author
Owner

@ldez commented on GitHub (Jun 3, 2022):

Seems related to 9d308e6246/netbox/ipam/api/views.py (L231)

@ldez commented on GitHub (Jun 3, 2022): Seems related to https://github.com/netbox-community/netbox/blob/9d308e62464590f4ce3e3b0e71970e099c565f4e/netbox/ipam/api/views.py#L231
Author
Owner

@jeremystretch commented on GitHub (Jun 3, 2022):

This behavior is expected. A single object is returned because a single object was requested. If you instead request a list of prefixes, a list will be returned. This is consistent with other API endpoints for object creation, which support requests for either a single object or a list of objects to be created.

@jeremystretch commented on GitHub (Jun 3, 2022): This behavior is expected. A single object is returned because a single object was requested. If you instead request a list of prefixes, a list will be returned. This is consistent with other API endpoints for object creation, which support requests for either a single object or a list of objects to be created.
Author
Owner

@darkweaver87 commented on GitHub (Jun 3, 2022):

OK, then the go client generation from swagger spec needs a fix somehow.

@darkweaver87 commented on GitHub (Jun 3, 2022): OK, then the go client generation from swagger spec needs a fix somehow.
Author
Owner

@jeremystretch commented on GitHub (Jun 3, 2022):

Yeah, I don't know if it's possible to document using Swagger that an endpoint can return a single object or a list.

@jeremystretch commented on GitHub (Jun 3, 2022): Yeah, I don't know if it's possible to document using Swagger that an endpoint can return a single object _or_ a list.
Author
Owner

@darkweaver87 commented on GitHub (Jun 3, 2022):

go is strongly typed so I'm not sure it will be easy to do or even possible :-/

@darkweaver87 commented on GitHub (Jun 3, 2022): go is strongly typed so I'm not sure it will be easy to do or even possible :-/
Author
Owner

@jeremystretch commented on GitHub (Jun 7, 2022):

I'm going to close this out as it does not appear that any action is to be taken, however if someone would like to propose a specific change I'd be happy to discuss. As for the Go client, I'd suggest always wrapping the requested objects in a list (even if only one object is needed) to workaround the issue.

@jeremystretch commented on GitHub (Jun 7, 2022): I'm going to close this out as it does not appear that any action is to be taken, however if someone would like to propose a specific change I'd be happy to discuss. As for the Go client, I'd suggest always wrapping the requested objects in a list (even if only one object is needed) to workaround the issue.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#6532