Skip to content

Commit 736c8f2

Browse files
authoredJan 30, 2023
fix: grpc-transcode plugin: fix map data population (#8731)
Fixes #8655
1 parent c8d5afb commit 736c8f2

File tree

9 files changed

+505
-5
lines changed

9 files changed

+505
-5
lines changed
 

‎.editorconfig

+1
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,4 @@ indent_style = unset
5050
indent_style = unset
5151
insert_final_newline = unset
5252
trim_trailing_whitespace = unset
53+
end_of_line = unset

‎apisix/plugins/grpc-transcode/util.lua

+25-5
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ local ngx = ngx
2222
local string = string
2323
local table = table
2424
local ipairs = ipairs
25+
local pairs = pairs
2526
local tonumber = tonumber
2627
local type = type
2728

@@ -131,7 +132,7 @@ local function get_from_request(request_table, name, kind)
131132
end
132133

133134

134-
function _M.map_message(field, default_values, request_table)
135+
function _M.map_message(field, default_values, request_table, real_key)
135136
if not pb.type(field) then
136137
return nil, "Field " .. field .. " is not defined"
137138
end
@@ -164,15 +165,34 @@ function _M.map_message(field, default_values, request_table)
164165
end
165166
sub = sub_array
166167
else
167-
sub, err = _M.map_message(field_type, default_values and default_values[name],
168-
request_table[name])
169-
if err then
170-
return nil, err
168+
if ty == "map" then
169+
for k, v in pairs(request_table[name]) do
170+
local tbl, err = _M.map_message(field_type,
171+
default_values and default_values[name],
172+
request_table[name], k)
173+
if err then
174+
return nil, err
175+
end
176+
if not sub then
177+
sub = {}
178+
end
179+
sub[k] = tbl[k]
180+
end
181+
else
182+
sub, err = _M.map_message(field_type,
183+
default_values and default_values[name],
184+
request_table[name])
185+
if err then
186+
return nil, err
187+
end
171188
end
172189
end
173190

174191
request[name] = sub
175192
else
193+
if real_key then
194+
name = real_key
195+
end
176196
request[name] = get_from_request(request_table, name, field_type)
177197
or (default_values and default_values[name])
178198
end

‎t/grpc_server_example/echo.pb

997 Bytes
Binary file not shown.

‎t/grpc_server_example/go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module github.com/api7/grpc_server_example
33
go 1.11
44

55
require (
6+
github.com/golang/protobuf v1.5.0
67
golang.org/x/net v0.2.0
78
google.golang.org/grpc v1.32.0
89
google.golang.org/protobuf v1.27.1

‎t/grpc_server_example/main.go

+12
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
//go:generate protoc --go_out=. --go_opt=paths=source_relative --go-grpc_out=. --go-grpc_opt=paths=source_relative proto/helloworld.proto
1919
//go:generate protoc --go_out=. --go_opt=paths=source_relative --go-grpc_out=. --go-grpc_opt=paths=source_relative proto/import.proto
2020
//go:generate protoc --include_imports --descriptor_set_out=proto.pb --go_out=. --go_opt=paths=source_relative --go-grpc_out=. --go-grpc_opt=paths=source_relative proto/src.proto
21+
//go:generate protoc --descriptor_set_out=echo.pb --include_imports --proto_path=$PWD/proto echo.proto
22+
//go:generate protoc --go_out=. --go_opt=paths=source_relative --go-grpc_out=. --go-grpc_opt=paths=source_relative proto/echo.proto
2123

2224
// Package main implements a server for Greeter service.
2325
package main
@@ -76,6 +78,7 @@ type server struct {
7678
// Embed the unimplemented server
7779
pb.UnimplementedGreeterServer
7880
pb.UnimplementedTestImportServer
81+
pb.UnimplementedEchoServer
7982
}
8083

8184
// SayHello implements helloworld.GreeterServer
@@ -141,6 +144,14 @@ func (s *server) Plus(ctx context.Context, in *pb.PlusRequest) (*pb.PlusReply, e
141144
return &pb.PlusReply{Result: in.A + in.B}, nil
142145
}
143146

147+
func (s *server) EchoStruct(ctx context.Context, in *pb.StructRequest) (*pb.StructReply, error) {
148+
log.Printf("Received: %+v", in)
149+
150+
return &pb.StructReply{
151+
Data: in.Data,
152+
}, nil
153+
}
154+
144155
// SayHelloServerStream streams HelloReply back to the client.
145156
func (s *server) SayHelloServerStream(req *pb.HelloRequest, stream pb.Greeter_SayHelloServerStreamServer) error {
146157
log.Printf("Received server side stream req: %v\n", req)
@@ -251,6 +262,7 @@ func main() {
251262
reflection.Register(s)
252263
pb.RegisterGreeterServer(s, &server{})
253264
pb.RegisterTestImportServer(s, &server{})
265+
pb.RegisterEchoServer(s, &server{})
254266

255267
if err := s.Serve(lis); err != nil {
256268
log.Fatalf("failed to serve: %v", err)

‎t/grpc_server_example/proto/echo.pb.go

+236
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//
2+
// Licensed to the Apache Software Foundation (ASF) under one or more
3+
// contributor license agreements. See the NOTICE file distributed with
4+
// this work for additional information regarding copyright ownership.
5+
// The ASF licenses this file to You under the Apache License, Version 2.0
6+
// (the "License"); you may not use this file except in compliance with
7+
// the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
//
17+
18+
syntax = "proto3";
19+
20+
package echo;
21+
option go_package = "./proto";
22+
23+
import "google/protobuf/struct.proto";
24+
25+
service Echo {
26+
rpc EchoStruct (StructRequest) returns (StructReply) {}
27+
}
28+
29+
message StructRequest {
30+
google.protobuf.Struct data = 1;
31+
}
32+
33+
message StructReply {
34+
google.protobuf.Struct data = 1;
35+
}

0 commit comments

Comments
 (0)