fix(cmd/protoc-gen-go-http): support opaque protobuf API for named body and response_body fields#3813
Open
lhsanghcmus wants to merge 1 commit into
Open
Conversation
…dy and response_body fields When body or response_body is a named field (not "*"), the generated code used direct struct field access (e.g. in.Payload, &in.Payload) which does not compile with opaque protobuf types where fields are hidden behind getter/setter methods. Use proto reflection (ProtoReflect().Mutable()) for server-side body binding and getter methods (Get<Field>()) for client-side body access and server-side response_body access. This is compatible with both open and opaque protobuf Go API styles.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
When the HTTP annotation uses a named
bodyorresponse_bodyfield (not"*"),protoc-gen-go-httpgenerates direct struct field access like&in.Payloadandreply.Books. This does not compile when using opaque protobuf types (newerprotoc-gen-goeditions), because fields are hidden (xxx_hidden_*) and only accessible via getter/setter methods.This PR fixes the generated code to be compatible with both open and opaque protobuf Go API styles:
in.ProtoReflect().Mutable(fd).Message().Interface()via proto reflection instead of&in.Fieldin.GetField()instead ofin.Fieldreply.GetField()instead ofreply.Field&out.Fieldbody: "*"behavior is unchanged — still usesctx.Bind(&in)andinas beforeExample:
bodywith a named fieldGiven this proto definition:
With opaque protobuf generation, the message has no exported
Payloadfield:Before (broken with opaque types):
After (works with both open and opaque types):
Example:
response_bodywith a named fieldGiven this proto definition:
Before (broken with opaque types):
After (works with both open and opaque types):
Changes
http.go: AddedtoGetterAccessorhelper to convert CamelCase field accessors to chained getter calls; set BodyProtoName/ResponseBodyProtoNameinbuildHTTPRule`template.go: AddedBodyProtoNameandResponseBodyProtoNamefields tomethodDesc; registered getterAccessor` template functionhttpTemplate.tpl: Updated server and client templates to use proto reflection / getters for named body fieldshttp_test.go: Added tests fortoGetterAccessorand template output for both named body andbody: "*"casesversion.go: Bumped to v2.9.3Test plan
toGetterAccessorfunctionProtoReflect().Mutable()for named body andctx.Bind(&in)forbody: "*"