This is an automated email from the ASF dual-hosted git repository.
ashishtiwari 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 be7461951 refactor: use setmetatable to set hidden variables without
effecting serialisation (#11770)
be7461951 is described below
commit be7461951213e6939dbc1757b7cae31ea88b68b3
Author: Ashish Tiwari <[email protected]>
AuthorDate: Fri Nov 22 14:10:33 2024 +0530
refactor: use setmetatable to set hidden variables without effecting
serialisation (#11770)
---
apisix/plugins/body-transformer.lua | 12 ++---
t/plugin/body-transformer2.t | 89 +++++++++++++++++++++++++++++++++++++
2 files changed, 96 insertions(+), 5 deletions(-)
diff --git a/apisix/plugins/body-transformer.lua
b/apisix/plugins/body-transformer.lua
index 977ebce67..f63381e01 100644
--- a/apisix/plugins/body-transformer.lua
+++ b/apisix/plugins/body-transformer.lua
@@ -29,7 +29,7 @@ local type = type
local pcall = pcall
local pairs = pairs
local next = next
-
+local setmetatable = setmetatable
local transform_schema = {
type = "object",
@@ -155,10 +155,12 @@ local function transform(conf, body, typ, ctx,
request_method)
return nil, 503, err
end
- out._ctx = ctx
- out._body = body
- out._escape_xml = escape_xml
- out._escape_json = escape_json
+ setmetatable(out, {__index = {
+ _ctx = ctx,
+ _body = body,
+ _escape_xml = escape_xml,
+ _escape_json = escape_json,
+ }})
local ok, render_out = pcall(render, out)
if not ok then
local err = str_format("%s template rendering: %s", typ, render_out)
diff --git a/t/plugin/body-transformer2.t b/t/plugin/body-transformer2.t
new file mode 100644
index 000000000..748b1fd1f
--- /dev/null
+++ b/t/plugin/body-transformer2.t
@@ -0,0 +1,89 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+use t::APISIX 'no_plan';
+
+no_long_string();
+no_shuffle();
+no_root_location();
+
+add_block_preprocessor(sub {
+ my ($block) = @_;
+
+ if (!$block->request) {
+ $block->set_value("request", "GET /t");
+ }
+});
+
+run_tests;
+
+__DATA__
+
+=== TEST 1: body transformer with decoded body (keyword: context)
+--- config
+ location /t {
+ content_by_lua_block {
+ local t = require("lib.test_admin")
+ local core = require("apisix.core")
+
+ local req_template = ngx.encode_base64[[
+ {%
+ local core = require 'apisix.core'
+ local cjson = require 'cjson'
+ context.name = "bar"
+ context.address = nil
+ context.age = context.age + 1
+ local body = core.json.encode(context)
+ %}{* body *}
+ ]]
+
+ local code, body = t.test('/apisix/admin/routes/1',
+ ngx.HTTP_PUT,
+ string.format([[{
+ "uri": "/echo",
+ "plugins": {
+ "body-transformer": {
+ "request": {
+ "template": "%s"
+ }
+ }
+ },
+ "upstream": {
+ "type": "roundrobin",
+ "nodes": {
+ "127.0.0.1:1980": 1
+ }
+ }
+ }]], req_template)
+ )
+
+ if code >= 300 then
+ ngx.status = code
+ end
+ ngx.say(body)
+ }
+ }
+--- response_body
+passed
+
+
+
+=== TEST 2: verify the transformed body
+--- request
+POST /echo
+{"name": "foo", "address":"LA", "age": 18}
+-- response_body
+{"name": "bar", "age": 19}