diff --git a/apisix/plugins/grpc-transcode/proto.lua b/apisix/plugins/grpc-transcode/proto.lua index a4f0f2744888..b9e9235fe2c7 100644 --- a/apisix/plugins/grpc-transcode/proto.lua +++ b/apisix/plugins/grpc-transcode/proto.lua @@ -132,8 +132,11 @@ local function create_proto_obj(proto_id) end local content + -- `id_schema` accepts a string or an integer, and the two are never equal + -- in Lua, so compare them in the same form. + proto_id = tostring(proto_id) for _, proto in config_util.iterate_values(protos.values) do - if proto_id == proto.value.id then + if proto_id == tostring(proto.value.id) then content = proto.value.content break end diff --git a/t/plugin/grpc-transcode.t b/t/plugin/grpc-transcode.t index 67d92ac2ab8d..16bf390262c1 100644 --- a/t/plugin/grpc-transcode.t +++ b/t/plugin/grpc-transcode.t @@ -837,3 +837,55 @@ POST /grpctest Content-Type: application/json --- response_body eval qr/"items":\[\]/ + + + +=== TEST 32: set route with proto_id as an integer +--- 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": ["POST"], + "uri": "/grpctest", + "plugins": { + "grpc-transcode": { + "proto_id": 1, + "service": "helloworld.Greeter", + "method": "SayHello" + } + }, + "upstream": { + "scheme": "grpc", + "type": "roundrobin", + "nodes": { + "127.0.0.1:10051": 1 + } + } + }]] + ) + + if code >= 300 then + ngx.status = code + end + ngx.say(body) + } + } +--- request +GET /t +--- response_body +passed + + + +=== TEST 33: hit route, an integer proto_id resolves the same proto +--- request +POST /grpctest +{"name":"world"} +--- more_headers +Content-Type: application/json +--- response_body eval +qr/"message":"Hello world"/