netbox-community / go-netbox

The official Go API client for Netbox IPAM and DCIM service.
Other
194 stars 144 forks source link

IpamPrefixesAvailableIpsCreate fails with unmarshal error #122

Closed ston1th closed 1 year ago

ston1th commented 2 years ago

Hey guys,

I have found a little bug while implementing a netbox based IP allocator.

Example:

package main

import (
        "fmt"

        transport "github.com/go-openapi/runtime/client"
        "github.com/netbox-community/go-netbox/netbox/client"
        "github.com/netbox-community/go-netbox/netbox/client/ipam"
        "github.com/netbox-community/go-netbox/netbox/models"
)

func main() {
        token := "<your demo token>"
        server := "demo.netbox.dev"
        tp := transport.New(server, client.DefaultBasePath, []string{"https"})
        tp.DefaultAuthentication = transport.APIKeyAuth("Authorization", "header", "Token "+token)
        c := client.New(tp, nil)
        ips, err := c.Ipam.IpamPrefixesAvailableIpsCreate(
                ipam.NewIpamPrefixesAvailableIpsCreateParams().WithID(1).WithData(
                        []*models.AvailableIP{
                                &models.AvailableIP{Family: 4},
                        }),
                nil,
        )
        fmt.Println(ips, err)
}

Output:

$ go run main.go
<nil> json: cannot unmarshal object into Go struct field AvailableIP.family of type int64

The API Response looks like this - here family is an object and not a int64:

[
  {
    "id": 16,
    "url": "https://demo.netbox.dev/api/ipam/ip-addresses/16/",
    "display": "10.112.0.1/15",
    "family": {
      "value": 4,
      "label": "IPv4"
    },
    "address": "10.112.0.1/15",
...

The type to which the json is unmarshaled:

type AvailableIP struct {
        Address string `json:"address,omitempty"`
        Family int64   `json:"family,omitempty"`
        Vrf *NestedVRF `json:"vrf,omitempty"`
}

Is a fix just a matter of regenerating the code/models based on the swagger spec or does this need to be changed manually? I think the unmarshal type needs to be changed from models.AvailableIP to models.IPAddress.

ston1th commented 2 years ago

I found another bug somewhat related to this.

When I changed the above example to this

        ips, err := c.Ipam.IpamPrefixesAvailableIpsCreate(
                ipam.NewIpamPrefixesAvailableIpsCreateParams().WithID(1),
                nil,
        )

I get a different unmarshal error:

$ go run main.go
<nil> json: cannot unmarshal object into Go value of type []*models.AvailableIP

The API Response is now a single object and no array:

{
  "id": 17,
  "url": "https://demo.netbox.dev/api/ipam/ip-addresses/17/",
  "display": "10.112.0.2/15",
  "family": {
    "value": 4,
    "label": "IPv4"
  },
  "address": "10.112.0.2/15",
...

So I think this case would also need to be handled.

chdxD1 commented 2 years ago

Hey @ston1th, I was the last one to update the project to v3. The sad thing is that the swagger we get from the netbox side is wrong at multiple points and this is one of them. The swagger output of netbox is also very unsupported, just keep this in mind when using go-netbox.

So manually changing the swagger.json is needed and then regenerate it :/

ston1th commented 2 years ago

Hi @chdxD1 thanks for your response. After thinking a bit about it I don't feel like edditing a big json file.. I'll implement the calls I need myself which also reduces the overall code size.

Should we keep this issue open for whom who like to pick it up or just close it?

chernogorsky commented 2 years ago

@chdxD1, sorry for writing in a wrong thread. I have some issue with vlangroup. (v3) do you want me open a separate issue with my workaround (at least how I understand the source of the issue) ? or the only way - try changing that issue in the main netbox-community ?

v0ctor commented 1 year ago

Since the library is automatically generated from the Netbox OpenAPI specification, specification-related issues should be created in the Netbox project.

When it's fixed in the spec, the error will be fixed in the library.