Skip to content
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

Trying to generate properties using the camelCase from underscores #1426

Closed
zoubarevm opened this issue May 30, 2020 · 3 comments
Closed

Trying to generate properties using the camelCase from underscores #1426

zoubarevm opened this issue May 30, 2020 · 3 comments

Comments

@zoubarevm
Copy link

📚 Documentation

I'm not entirely sure if this is a documentation issue, but the current proposed suggested method is to generate message using the undescore convention:
e.g.

message CreateUserRequest {
  string first_name = 1;
  string last_name = 2;
  string email = 3;
}

When I generate this using:

protoc --go_out=plugins=grpc,paths=source_relative:. \
--grpc-gateway_out=logtostderr=true,paths=source_relative:. \
--swagger_out=logtostderr=true,json_names_for_fields=true,fqn_for_swagger_name=true:.

I get this in the *.pb.go file:

type CreateUserRequest struct {
	state         protoimpl.MessageState
	sizeCache     protoimpl.SizeCache
	unknownFields protoimpl.UnknownFields

	FirstName       string `protobuf:"bytes,1,opt,name=first_name,json=firstName,proto3" json:"first_name,omitempty"`
	LastName        string `protobuf:"bytes,2,opt,name=last_name,json=lastName,proto3" json:"last_name,omitempty"`
	Email           string `protobuf:"bytes,3,opt,name=email,proto3" json:"email,omitempty"`
}

For some reason, I can't get the flags to line up between the protofbuf json and the json marshal, specifically the json=firstName and json:"first_name,omitempty"

Am I missing something from a documentation perspective? Is there a flag I need to set?

@johanbrandhorst
Copy link
Collaborator

Hi Michael,

I'm a little confused, the struct definition you've shared isn't something I'd have expected from protoc-gen-go using the message you shared. Take a look at helloworld.proto:

https://github.com/grpc-ecosystem/grpc-gateway/blob/v2/examples/internal/helloworld/helloworld.proto#L58-L60

and it's generated counterpart:

https://github.com/grpc-ecosystem/grpc-gateway/blob/v2/examples/internal/helloworld/helloworld.pb.go#L149-L154

The json field is not set in the protobuf struct tag unless you manually override it in your protofile.

Furthermore, why are you invoking protoc-gen-swagger the way you are? I don't think we recommend those settings anywhere in our docs, please correct me if I'm wrong. If you follow the instructions in the README, you should end up with a working gateway, and what you're showing me now has deviated a bit from that.

Having said all of that, this is not an issue with this project, as we do not control the protoc-gen-go generator. I'd be happy to help understand your issue in #protobuf or #grpc-gateway on the Gophers slack (https://invite.slack.golangbridge.org/), but this is not the right place for a support/configuration issue such as this.

@zoubarevm
Copy link
Author

Sounds good. I’ll post my question on slack and aim to figure out how to solve my problem there. Im still new to the grpc world, but I have noticed that none of your examples have underscores for the field names in message. Any chance you have an example using that? See https://developers.google.com/protocol-buffers/docs/style#message-and-field-names

Thanks for quick feedback.

@johanbrandhorst
Copy link
Collaborator

Haha I had the same thought when I went through the examples. It's not important, but I thought it was funny that we didn't, you're right. Again, it's not really our responsibility to test that part, it's part of the official Google go protobuf project.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants