grafeas / client-go

Apache License 2.0
11 stars 23 forks source link

JSON difference in the key format between the client and the server #17

Open lynnsh opened 5 years ago

lynnsh commented 5 years ago

Problem

Server and client json renderers do not match. Client code (defined in client-go) expects json keys in snake_case, and the server code (defined in grafeas/grafeas) sends json keys in camelCase. Thus, server response values are ignored by the client marshaller.

Example

Field definitions: client-go: ShortDescription string 'json:"short_description,omitempty"' grafeas: ShortDescription string 'protobuf:"bytes,2,opt,name=short_description,json=shortDescription,proto3" json:"short_description,omitempty"'

What the client receives before the values are unmarshalled: "name":"projects/grafeasclienttest/notes/grafeasattestation","shortDescription":"short","longDescription":"long","kind":"ATTESTATION","relatedUrl":[],"expirationTime":"0001-01-01T00:00:00Z","createTime":"2019-11-12T14:12:44.705616Z","updateTime":"2019-11-12T14:12:44.705616Z","relatedNoteNames":[],"attestationAuthority":{"hint":{"humanReadableName":"projects/grafeasclienttest/notes/grafeasattestation"}}

The final unmarshalled object: Name:projects/grafeasclienttest/notes/grafeasattestation ShortDescription: LongDescription: Kind:0xc00041b0e0 RelatedUrl:[] ExpirationTime:0001-01-01 00:00:00 +0000 UTC CreateTime:0001-01-01 00:00:00 +0000 UTC UpdateTime:0001-01-01 00:00:00 +0000 UTC RelatedNoteNames:[] Vulnerability:<nil><HIGHLIGHT:END>

Potential Solution

Rename json keys in client-go models to use the camelCase, e.g. (from model_v1beta1_note.go):

// A one sentence description of this note.
ShortDescription string `json:"shortDescription,omitempty"`
aysylu commented 4 years ago

Hi @lynnsh, this is a great catch and I think the long-term fix to this would be incredibly valuable. As discussed in #16, the best way forward would probably be to fix this at the Swagger codegen level. What do you think?