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

support one-ofs in body #570

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions examples/clients/echo/echo_service_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,69 @@ func (a EchoServiceApi) EchoBody(body ExamplepbSimpleMessage) (*ExamplepbSimpleM
return successPayload, localVarAPIResponse, err
}

/**
* EchoBody method receives a simple message and returns it.
*
* @param id
* @param body
* @return *ExamplepbSimpleMessage
*/
func (a EchoServiceApi) EchoBody2(id string, body ExamplepbEmbedded) (*ExamplepbSimpleMessage, *APIResponse, error) {

var localVarHttpMethod = strings.ToUpper("Put")
// create path and map variables
localVarPath := a.Configuration.BasePath + "/v1/example/echo_body/{id}"
localVarPath = strings.Replace(localVarPath, "{"+"id"+"}", fmt.Sprintf("%v", id), -1)

localVarHeaderParams := make(map[string]string)
localVarQueryParams := url.Values{}
localVarFormParams := make(map[string]string)
var localVarPostBody interface{}
var localVarFileName string
var localVarFileBytes []byte
// add default headers if any
for key := range a.Configuration.DefaultHeader {
localVarHeaderParams[key] = a.Configuration.DefaultHeader[key]
}

// to determine the Content-Type header
localVarHttpContentTypes := []string{ "application/json", }

// set Content-Type header
localVarHttpContentType := a.Configuration.APIClient.SelectHeaderContentType(localVarHttpContentTypes)
if localVarHttpContentType != "" {
localVarHeaderParams["Content-Type"] = localVarHttpContentType
}
// to determine the Accept header
localVarHttpHeaderAccepts := []string{
"application/json",
}

// set Accept header
localVarHttpHeaderAccept := a.Configuration.APIClient.SelectHeaderAccept(localVarHttpHeaderAccepts)
if localVarHttpHeaderAccept != "" {
localVarHeaderParams["Accept"] = localVarHttpHeaderAccept
}
// body params
localVarPostBody = &body
var successPayload = new(ExamplepbSimpleMessage)
localVarHttpResponse, err := a.Configuration.APIClient.CallAPI(localVarPath, localVarHttpMethod, localVarPostBody, localVarHeaderParams, localVarQueryParams, localVarFormParams, localVarFileName, localVarFileBytes)

var localVarURL, _ = url.Parse(localVarPath)
localVarURL.RawQuery = localVarQueryParams.Encode()
var localVarAPIResponse = &APIResponse{Operation: "EchoBody2", Method: localVarHttpMethod, RequestURL: localVarURL.String()}
if localVarHttpResponse != nil {
localVarAPIResponse.Response = localVarHttpResponse.RawResponse
localVarAPIResponse.Payload = localVarHttpResponse.Body()
}

if err != nil {
return successPayload, localVarAPIResponse, err
}
err = json.Unmarshal(localVarHttpResponse.Body(), &successPayload)
return successPayload, localVarAPIResponse, err
}

/**
* EchoDelete method receives a simple message and returns it.
*
Expand Down
75 changes: 38 additions & 37 deletions examples/proto/examplepb/echo_service.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

77 changes: 77 additions & 0 deletions examples/proto/examplepb/echo_service.pb.gw.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions examples/proto/examplepb/echo_service.proto
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ service EchoService {
option (google.api.http) = {
post: "/v1/example/echo_body"
body: "*"
additional_bindings {
put: "/v1/example/echo_body/{id}"
body: "no"
}
};
}
// EchoDelete method receives a simple message and returns it.
Expand Down
33 changes: 33 additions & 0 deletions examples/proto/examplepb/echo_service.swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,39 @@
]
}
},
"/v1/example/echo_body/{id}": {
"put": {
"summary": "EchoBody method receives a simple message and returns it.",
"operationId": "EchoBody2",
"responses": {
"200": {
"description": "",
"schema": {
"$ref": "#/definitions/examplepbSimpleMessage"
}
}
},
"parameters": [
{
"name": "id",
"in": "path",
"required": true,
"type": "string"
},
{
"name": "body",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/examplepbEmbedded"
}
}
],
"tags": [
"EchoService"
]
}
},
"/v1/example/echo_delete": {
"delete": {
"summary": "EchoDelete method receives a simple message and returns it.",
Expand Down
42 changes: 40 additions & 2 deletions protoc-gen-grpc-gateway/descriptor/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,12 @@ func (b Body) AssignableExpr(msgExpr string) string {
return b.FieldPath.AssignableExpr(msgExpr)
}

// AssignableExprPrep returns prepatory statements for an assignable expression to initialize
// method request object.
func (b Body) AssignableExprPrep(msgExpr string) string {
return b.FieldPath.AssignableExprPrep(msgExpr)
}

// FieldPath is a path to a field from a request message.
type FieldPath []FieldPathComponent

Expand All @@ -261,13 +267,46 @@ func (p FieldPath) IsNestedProto3() bool {
}

// AssignableExpr is an assignable expression in Go to be used to assign a value to the target field.
// It starts with "msgExpr", which is the go expression of the method request object.
// It starts with "msgExpr", which is the go expression of the method request object. Before using
// such an expression the prep statements must be emitted first, in case the field path includes
// a oneof. See FieldPath.AssignableExprPrep.
func (p FieldPath) AssignableExpr(msgExpr string) string {
l := len(p)
if l == 0 {
return msgExpr
}

components := msgExpr
for i, c := range p {
// Check if it is a oneOf field.
if c.Target.OneofIndex != nil {
index := c.Target.OneofIndex
msg := c.Target.Message
oneOfName := gogen.CamelCase(msg.GetOneofDecl()[*index].GetName())
oneofFieldName := msg.GetName() + "_" + c.AssignableExpr()

components = components + "." + oneOfName + ".(*" + oneofFieldName + ")"
}

if i == l-1 {
components = components + "." + c.AssignableExpr()
continue
}
components = components + "." + c.ValueExpr()
}

return components
}

// AssignableExprPrep returns preparation statements for an assignable expression to assign a value
// to the target field. The go expression of the method request object is "msgExpr". This is only
// needed for field paths that contain oneofs. Otherwise, an empty string is returned.
func (p FieldPath) AssignableExprPrep(msgExpr string) string {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I essentially forked AssignableExpr, with this method preserving just the preparations and the other only emitting the last line, the actual field ref.

l := len(p)
if l == 0 {
return ""
}

var preparations []string
components := msgExpr
for i, c := range p {
Expand Down Expand Up @@ -296,7 +335,6 @@ func (p FieldPath) AssignableExpr(msgExpr string) string {
components = components + "." + c.ValueExpr()
}

preparations = append(preparations, components)
return strings.Join(preparations, "\n")
}

Expand Down
Loading