Skip to content

protoc-gen-go: JSON struct tags are generated with the wrong naming convention #698

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
bmhatfield opened this issue Sep 11, 2018 · 2 comments

Comments

@bmhatfield
Copy link

What version of protobuf and what language are you using?
% go version
go version go1.11 darwin/amd64

What did you do?
Observed that generated code looked funny:

InviterUserId string `protobuf:"bytes,2,opt,name=inviter_user_id,json=inviterUserId,proto3" json:"inviter_user_id,omitempty"`

What did you expect to see?
I expected the json= output in the protobuf: tag to match the json: tag field name.

What did you see instead?
JSON tag field naming that doesn't match JSON field norms.

The problem is that g.goTag uses GetJsonName but the actual JSON tag generation uses *field.Name.

Reccomendation:
Switch https://github.com/golang/protobuf/blob/master/protoc-gen-go/generator/generator.go#L2488 to use GetJsonName just like https://github.com/golang/protobuf/blob/master/protoc-gen-go/generator/generator.go#L1548

@dsnet
Copy link
Member

dsnet commented Sep 11, 2018

The situation is unfortunate and can't be changed without breaking existing code. The generation of the json tag pre-dated the formalization of the JSON<->PB specification. In it's current state, using encoding/json does not produce JSON that conforms to any particular pre-defined schema.

The jsonpb package exists to implement the aforementioned JSON specification since it is not possible to do so using the json tags alone. Rather than fixing the json tag, it should probably be removed entirely and have #256 addressed instead.

@dsnet dsnet changed the title JSON Struct Tags are generated with the wrong naming convention protoc-gen-go: JSON struct tags are generated with the wrong naming convention Sep 11, 2018
@dsnet
Copy link
Member

dsnet commented Sep 11, 2018

Closing as there's not really any actionable to do here without breaking compatibility. The best we can do is #256.

@dsnet dsnet closed this as completed Sep 11, 2018
@golang golang locked and limited conversation to collaborators Jun 26, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants