swagger/ipam/ip-addresses/: Invalid type for "assigned_object" #6754

Closed
opened 2025-12-29 19:45:00 +01:00 by adam · 4 comments
Owner

Originally created by @phpfs on GitHub (Aug 1, 2022).

Originally assigned to: @amhn on GitHub.

NetBox version

v3.2.6

Python version

3.8

Steps to Reproduce

Hi there,

the netbox swagger.yml states that the IP address list endpoint /ipam/ip-addresses/ returns a value of type map[string]string for key assigned_object instead of the correct type object. This causes issues for me since I use an auto-generated api client which will error out in case it receives something not string-like - in this case e.g. a number as id.

Another similar issue has been opened here already: https://github.com/netbox-community/go-netbox/issues/133

What can we do about that? :)

Thanks a lot!

Expected Behavior

Correct type stated in swagger.yml

Observed Behavior

Incorrect type stated in swagger.yml

Originally created by @phpfs on GitHub (Aug 1, 2022). Originally assigned to: @amhn on GitHub. ### NetBox version v3.2.6 ### Python version 3.8 ### Steps to Reproduce Hi there, the netbox swagger.yml states that the IP address list endpoint `/ipam/ip-addresses/` returns a value of type map[string]string for key `assigned_object ` instead of the correct type object. This causes issues for me since I use an auto-generated api client which will error out in case it receives something _not string-like_ - in this case e.g. a number as id. Another similar issue has been opened here already: https://github.com/netbox-community/go-netbox/issues/133 What can we do about that? :) Thanks a lot! ### Expected Behavior Correct type stated in swagger.yml ### Observed Behavior Incorrect type stated in swagger.yml
adam added the type: bugstatus: accepted labels 2025-12-29 19:45:00 +01:00
adam closed this issue 2025-12-29 19:45:00 +01:00
Author
Owner

@DanSheps commented on GitHub (Aug 2, 2022):

I don't know if there is a way around this, as assigned_object is a GenericForeignKey object and could may to any number of serializers so there is not one specific serializer used to be sent to drf_yasg for generation.

If someone is willing to come up with a way to fix this, they are welcome to submit a PR.

@DanSheps commented on GitHub (Aug 2, 2022): I don't know if there is a way around this, as assigned_object is a GenericForeignKey object and could may to any number of serializers so there is not one specific serializer used to be sent to drf_yasg for generation. If someone is willing to come up with a way to fix this, they are welcome to submit a PR.
Author
Owner

@phpfs commented on GitHub (Aug 2, 2022):

As it turns out, within swagger.json the type is correctly set to object already, but an additional property leads to the interpretation as string map:

"type": "object",
"additionalProperties": {
    "type": "string",
    "x-nullable": true
}

Simply removing "type": "string", yields a perfectly running API client for us :)

Additionally, we observed that the swagger file sometimes includes random _occupied keys that are set to string instead of boolean:

"_occupied": {
    "title": "occupied",
    "type": "string",
    "type": "boolean",
    "readOnly": true
}

This also caused some issues for us - though again quite easy to fix!

@phpfs commented on GitHub (Aug 2, 2022): As it turns out, within `swagger.json` the type is correctly set to `object` already, but an additional property leads to the interpretation as string map: ```json "type": "object", "additionalProperties": { "type": "string", "x-nullable": true } ``` Simply removing ` "type": "string",` yields a perfectly running API client for us :) Additionally, we observed that the swagger file sometimes includes random `_occupied` keys that are set to `string` instead of `boolean`: ```json "_occupied": { "title": "occupied", "type": "string", "type": "boolean", "readOnly": true } ``` This also caused some issues for us - though again quite easy to fix!
Author
Owner

@amhn commented on GitHub (Aug 28, 2022):

I looked into that.

It looks like the condition in the custom JSONFieldInpector in netbox/utilities/custom_inspectors.py is never true because the JSONField imported is from django.contrib.postgres.fields while it should be from rest_framework.fields. After fixing that there is an error because "dict" is not a valid swagger type. Since drf_yasg has since gained the abilitiy to serialize JSON fields, this inspector can be removed.

Afterwards changing all mentions of "serializers.DictField" to "serializers.JSONField" fixes the issue for this field and others.

To fix the _occupied rendered as string a BooleanField has to be added to all NestedSerializers with a _occupied field.

Happy to provide a PR if you assign this to me.

@amhn commented on GitHub (Aug 28, 2022): I looked into that. It looks like the condition in the custom JSONFieldInpector in netbox/utilities/custom_inspectors.py is never true because the JSONField imported is from django.contrib.postgres.fields while it should be from rest_framework.fields. After fixing that there is an error because "dict" is not a valid swagger type. Since drf_yasg has since gained the abilitiy to serialize JSON fields, this inspector can be removed. Afterwards changing all mentions of "serializers.DictField" to "serializers.JSONField" fixes the issue for this field and others. To fix the _occupied rendered as string a BooleanField has to be added to all NestedSerializers with a _occupied field. Happy to provide a PR if you assign this to me.
Author
Owner

@phpfs commented on GitHub (Aug 29, 2022):

Thanks @amhn! Please let me know in case I can help or test something :)

@phpfs commented on GitHub (Aug 29, 2022): Thanks @amhn! Please let me know in case I can help or test something :)
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#6754