Skip to content

Commit be74619

Browse files
authored
refactor: use setmetatable to set hidden variables without effecting serialisation (#11770)
1 parent 4e05192 commit be74619

File tree

2 files changed

+96
-5
lines changed

2 files changed

+96
-5
lines changed

apisix/plugins/body-transformer.lua

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ local type = type
2929
local pcall = pcall
3030
local pairs = pairs
3131
local next = next
32-
32+
local setmetatable = setmetatable
3333

3434
local transform_schema = {
3535
type = "object",
@@ -155,10 +155,12 @@ local function transform(conf, body, typ, ctx, request_method)
155155
return nil, 503, err
156156
end
157157

158-
out._ctx = ctx
159-
out._body = body
160-
out._escape_xml = escape_xml
161-
out._escape_json = escape_json
158+
setmetatable(out, {__index = {
159+
_ctx = ctx,
160+
_body = body,
161+
_escape_xml = escape_xml,
162+
_escape_json = escape_json,
163+
}})
162164
local ok, render_out = pcall(render, out)
163165
if not ok then
164166
local err = str_format("%s template rendering: %s", typ, render_out)

t/plugin/body-transformer2.t

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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+
use t::APISIX 'no_plan';
18+
19+
no_long_string();
20+
no_shuffle();
21+
no_root_location();
22+
23+
add_block_preprocessor(sub {
24+
my ($block) = @_;
25+
26+
if (!$block->request) {
27+
$block->set_value("request", "GET /t");
28+
}
29+
});
30+
31+
run_tests;
32+
33+
__DATA__
34+
35+
=== TEST 1: body transformer with decoded body (keyword: context)
36+
--- config
37+
location /t {
38+
content_by_lua_block {
39+
local t = require("lib.test_admin")
40+
local core = require("apisix.core")
41+
42+
local req_template = ngx.encode_base64[[
43+
{%
44+
local core = require 'apisix.core'
45+
local cjson = require 'cjson'
46+
context.name = "bar"
47+
context.address = nil
48+
context.age = context.age + 1
49+
local body = core.json.encode(context)
50+
%}{* body *}
51+
]]
52+
53+
local code, body = t.test('/apisix/admin/routes/1',
54+
ngx.HTTP_PUT,
55+
string.format([[{
56+
"uri": "/echo",
57+
"plugins": {
58+
"body-transformer": {
59+
"request": {
60+
"template": "%s"
61+
}
62+
}
63+
},
64+
"upstream": {
65+
"type": "roundrobin",
66+
"nodes": {
67+
"127.0.0.1:1980": 1
68+
}
69+
}
70+
}]], req_template)
71+
)
72+
73+
if code >= 300 then
74+
ngx.status = code
75+
end
76+
ngx.say(body)
77+
}
78+
}
79+
--- response_body
80+
passed
81+
82+
83+
84+
=== TEST 2: verify the transformed body
85+
--- request
86+
POST /echo
87+
{"name": "foo", "address":"LA", "age": 18}
88+
-- response_body
89+
{"name": "bar", "age": 19}

0 commit comments

Comments
 (0)