netbox icon indicating copy to clipboard operation
netbox copied to clipboard

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

Open phpfs opened this issue 2 years ago • 2 comments

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

phpfs avatar Aug 01 '22 17:08 phpfs

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 avatar Aug 02 '22 18:08 DanSheps

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 avatar Aug 02 '22 20:08 phpfs

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 avatar Aug 28 '22 20:08 amhn

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

phpfs avatar Aug 29 '22 08:08 phpfs