You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by ch...@apache.org on 2020/09/10 10:02:26 UTC
[apisix] branch master updated: bugfix: grpc-transcode plugin
converts http/json parameters abnormally (#2139)
This is an automated email from the ASF dual-hosted git repository.
chenjunxu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/apisix.git
The following commit(s) were added to refs/heads/master by this push:
new 087f87a bugfix: grpc-transcode plugin converts http/json parameters abnormally (#2139)
087f87a is described below
commit 087f87a7e49b228595b9d9b27c004608d04588e8
Author: nic-chen <33...@users.noreply.github.com>
AuthorDate: Thu Sep 10 18:02:16 2020 +0800
bugfix: grpc-transcode plugin converts http/json parameters abnormally (#2139)
* fix bug
* optimiz
* update grpc_server_example
* add test cases
* fix: test case
* fix: test case
* return val directly
* fix: test case respond body compare text
* fix test case error
Co-authored-by: Wen Ming <mo...@gmail.com>
---
.travis/linux_openresty_runner.sh | 2 +-
.travis/linux_tengine_runner.sh | 2 +-
.travis/osx_openresty_runner.sh | 2 +-
apisix/plugins/grpc-transcode/util.lua | 45 ++++++++++------
t/plugin/grpc-transcode.t | 94 ++++++++++++++++++++++++++++++++++
5 files changed, 126 insertions(+), 19 deletions(-)
diff --git a/.travis/linux_openresty_runner.sh b/.travis/linux_openresty_runner.sh
index 7281c0d..b37aaea 100755
--- a/.travis/linux_openresty_runner.sh
+++ b/.travis/linux_openresty_runner.sh
@@ -99,7 +99,7 @@ do_install() {
ls -l ./
if [ ! -f "build-cache/grpc_server_example" ]; then
- wget https://github.com/iresty/grpc_server_example/releases/download/20200314/grpc_server_example-amd64.tar.gz
+ wget https://github.com/iresty/grpc_server_example/releases/download/20200901/grpc_server_example-amd64.tar.gz
tar -xvf grpc_server_example-amd64.tar.gz
mv grpc_server_example build-cache/
fi
diff --git a/.travis/linux_tengine_runner.sh b/.travis/linux_tengine_runner.sh
index 2404978..12b6819 100755
--- a/.travis/linux_tengine_runner.sh
+++ b/.travis/linux_tengine_runner.sh
@@ -268,7 +268,7 @@ do_install() {
ls -l ./
if [ ! -f "build-cache/grpc_server_example" ]; then
- wget https://github.com/iresty/grpc_server_example/releases/download/20200314/grpc_server_example-amd64.tar.gz
+ wget https://github.com/iresty/grpc_server_example/releases/download/20200901/grpc_server_example-amd64.tar.gz
tar -xvf grpc_server_example-amd64.tar.gz
mv grpc_server_example build-cache/
fi
diff --git a/.travis/osx_openresty_runner.sh b/.travis/osx_openresty_runner.sh
index 6070f02..808359d 100755
--- a/.travis/osx_openresty_runner.sh
+++ b/.travis/osx_openresty_runner.sh
@@ -51,7 +51,7 @@ do_install() {
wget -P utils https://raw.githubusercontent.com/openresty/openresty-devel-utils/master/lj-releng
chmod a+x utils/lj-releng
- wget https://github.com/iresty/grpc_server_example/releases/download/20200314/grpc_server_example-darwin-amd64.tar.gz
+ wget https://github.com/iresty/grpc_server_example/releases/download/20200901/grpc_server_example-darwin-amd64.tar.gz
tar -xvf grpc_server_example-darwin-amd64.tar.gz
brew install grpcurl
diff --git a/apisix/plugins/grpc-transcode/util.lua b/apisix/plugins/grpc-transcode/util.lua
index d705a1e..da76426 100644
--- a/apisix/plugins/grpc-transcode/util.lua
+++ b/apisix/plugins/grpc-transcode/util.lua
@@ -14,7 +14,8 @@
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-local json = require("apisix.core.json")
+local core = require("apisix.core")
+local json = core.json
local pb = require("pb")
local ngx = ngx
local pairs = pairs
@@ -47,24 +48,35 @@ function _M.find_method(protos, service, method)
end
-local function get_from_request(name, kind)
- local request_table
- if ngx.req.get_method() == "POST" then
- if string.find(ngx.req.get_headers()["Content-Type"] or "",
- "application/json", 1, true) then
- request_table = json.decode(ngx.req.get_body_data())
- else
- request_table = ngx.req.get_post_args()
+local function get_request_table()
+ local method = ngx.req.get_method()
+ local content_type = ngx.req.get_headers()["Content-Type"] or ""
+ if string.find(content_type, "application/json", 1, true) and
+ (method == "POST" or method == "PUT" or method == "PATCH")
+ then
+ local req_body, _ = core.request.get_body()
+ if req_body then
+ local data, _ = json.decode(req_body)
+ if data then
+ return data
+ end
end
- else
- request_table = ngx.req.get_uri_args()
end
- local prefix = kind:sub(1, 3)
- if prefix == "str" then
- return request_table[name] or nil
+ if method == "POST" then
+ return ngx.req.get_post_args()
+ end
+
+ return ngx.req.get_uri_args()
+end
+
+
+local function get_from_request(request_table, name, kind)
+ if not request_table then
+ return nil
end
+ local prefix = kind:sub(1, 3)
if prefix == "int" then
if request_table[name] then
if kind == "int64" then
@@ -75,7 +87,7 @@ local function get_from_request(name, kind)
end
end
- return nil
+ return request_table[name]
end
@@ -86,6 +98,7 @@ function _M.map_message(field, default_values)
local request = {}
local sub, err
+ local request_table = get_request_table()
for name, _, field_type in pb.fields(field) do
if field_type:sub(1, 1) == "." then
sub, err = _M.map_message(field_type, default_values)
@@ -94,7 +107,7 @@ function _M.map_message(field, default_values)
end
request[name] = sub
else
- request[name] = get_from_request(name, field_type)
+ request[name] = get_from_request(request_table, name, field_type)
or default_values[name] or nil
end
end
diff --git a/t/plugin/grpc-transcode.t b/t/plugin/grpc-transcode.t
index 9baca82..2b3aa56 100644
--- a/t/plugin/grpc-transcode.t
+++ b/t/plugin/grpc-transcode.t
@@ -485,3 +485,97 @@ GET /t
{"error_msg":"failed to check the configuration of plugin grpc-transcode err: property \"method\" is required"}
--- no_error_log
[error]
+
+
+
+=== TEST 18: set proto(id: 1, with array parameter)
+--- config
+ location /t {
+ content_by_lua_block {
+ local t = require("lib.test_admin").test
+ local code, body = t('/apisix/admin/proto/1',
+ ngx.HTTP_PUT,
+ [[{
+ "content" : "syntax = \"proto3\";
+ package helloworld;
+ service Greeter {
+ rpc SayHello (HelloRequest) returns (HelloReply) {}
+ }
+ message HelloRequest {
+ string name = 1;
+ repeated string items = 2;
+ }
+ message HelloReply {
+ string message = 1;
+ repeated string items = 2;
+ }"
+ }]]
+ )
+
+ if code >= 300 then
+ ngx.status = code
+ end
+ ngx.say(body)
+ }
+ }
+--- request
+GET /t
+--- response_body
+passed
+--- no_error_log
+[error]
+
+
+
+=== TEST 19: set routes(id: 1, with array parameter)
+--- config
+ location /t {
+ content_by_lua_block {
+ local t = require("lib.test_admin").test
+ local code, body = t('/apisix/admin/routes/1',
+ ngx.HTTP_PUT,
+ [[{
+ "methods": ["GET", "POST"],
+ "uri": "/grpctest",
+ "service_protocol": "grpc",
+ "plugins": {
+ "grpc-transcode": {
+ "proto_id": "1",
+ "service": "helloworld.Greeter",
+ "method": "SayHello"
+ }
+ },
+ "upstream": {
+ "type": "roundrobin",
+ "nodes": {
+ "127.0.0.1:50051": 1
+ }
+ }
+ }]]
+ )
+
+ if code >= 300 then
+ ngx.status = code
+ end
+ ngx.say(body)
+ }
+ }
+--- request
+GET /t
+--- response_body
+passed
+--- no_error_log
+[error]
+
+
+
+=== TEST 20: hit route
+--- request
+POST /grpctest
+{"name":"apisix", "items": ["a","b","c"]}
+--- more_headers
+Content-Type: application/json
+--- response_body eval
+qr/\{"items":\["a","b","c"\],"message":"Hello apisix"\}/
+--- no_error_log
+[error]