netbox-community / go-netbox

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

Generate from swagger.json #26

Closed SchwarzM closed 6 years ago

SchwarzM commented 6 years ago

Hi,

this might be a stupid question but why is this an extra implementation and not generated from swagger.json via something like https://github.com/go-swagger/go-swagger?

Am I missing something obvious?

Kind regards SchwarzM

mdlayher commented 6 years ago

When I originally started building this, I didn't realize it was possible. If you could come up with something that generates reasonably nice code (or can be easily wrapped in a good API), I'd be in favor of doing so.

SchwarzM commented 6 years ago

I played with this some more: https://github.com/SchwarzM/go-netbox

The current swagger.json has all api calls but lacks proper definitions for responses maybe we could present this to @jeremystretch if this was left out on purpose or if he would be open for some swagger annotation tweaks to make go-netbox's life easier.

The generated client is not usable due to the models missing but is clearly shows the outcome of a swagger generation.

Kind regards SchwarzM

davcamer commented 6 years ago

I need another chunk of NetBox API for my current project. Seeing this issue, I thought I'd dig deeper before adding more endpoints using the current code-generation approach.

NetBox is using django-rest-framework (drf) for the JSON api and django-rest-swagger (drs) for the API exploration API. drf bases it's schema support on coreapi, and drs glosses that in to the openapi/swagger standard format. But, coreapi doesn't have a capability of defining response bodies, so the resulting swagger lacks responses.

The drf documentation offers a couple of alternatives. I'm going to try these out locally and see if they result in a usable go-netbox client. If they do, I'll approach Jeremy about changing the API doc support in NetBox.

chrigl commented 6 years ago

Would be great!

Tested go-swagger some time ago as well. I was able to generate a working tenant-list-client by adding definition and responses by hand in the generated swagger.json. In general this should work.

If someone is interrested:

Definition: https://gist.github.com/chrigl/3e35f98272637baf930d8d3c9b74d206#file-json-L23585 Response: https://gist.github.com/chrigl/3e35f98272637baf930d8d3c9b74d206#file-json-L20918

davcamer commented 6 years ago

The first alternative I tried currently has some trouble with Netbox's pattern of labeled values backed with integer or boolean values. I'm going to link the issue here to keep track of what I tried.

https://github.com/axnsan12/drf-yasg/issues/69

davcamer commented 6 years ago

Looked at https://github.com/limdauto/drf_openapi but having trouble getting it to work. It expects versioning, and URLPath versioning specifically. Netbox uses Accept header versioning instead. Also the idea that all the serializers should subclass VersionedSerializer would lead to many code changes in the netbox codebase.

davcamer commented 6 years ago

With some custom FieldInspectors to deal with the custom ChoiceFieldSerializaer I was able to generate a full client. Will test-drive it next week. How do we distribute a fully generated client? Check it in to this repo? Should I open a PR?

SchwarzM commented 6 years ago

since #28 we should close this. Amazing work @davcamer.