You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by GitBox <gi...@apache.org> on 2021/09/08 09:55:40 UTC

[GitHub] [apisix] mangoGoForward opened a new pull request #5016: feat: xml-json-conversion plugin convert xml data from request body to json response, and vice versa

mangoGoForward opened a new pull request #5016:
URL: https://github.com/apache/apisix/pull/5016


   ### What this PR does / why we need it:
   ## Summary
   
   - [**Name**](#name)
   - [**Attributes**](#Attributes)
   - [**How To Enable**](#how-to-enable)
   - [**Test Plugin**](#test-plugin)
   - [**Disable Plugin**](#disable-plugin)
   
   ## name
   
   xml-json-conversion plugin convert xml data from request body to json response, and vice versa
   
   ## Attributes
   
   | Name          | Type    | Requirement | Default | Valid                       | Description  |
   |:--------------|:--------|:------------|:--------|:----------------------------|:-------------|
   | from          | string  | optional    | xml     | ["xml", "json"]             | input type   |
   | to            | string  | optional    | json    | ["xml", "json"]             | output type  |
   
   ## how-to-enable
   
   Here's an example, enable this plugin on the specified route:
   
   ```shell
   curl -i http://127.0.0.1:9080/apisix/admin/routes/1  -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
   {
       "uri": "/hello",
       "plugins": {
           "xml-json-conversion": {
               "from": "xml",
               "to": "json"
           }
       },
       "upstream": {
           "type": "roundrobin",
           "nodes": {
               "39.97.63.215:80": 1
           }
       }
   }'
   ```
   
   ## test-plugin
   
   Via curl to access
   
   ```shell
   curl -X GET http://127.0.0.1:9080/hello \
   -H 'Content-Type: text/xml' \
   --data '
   <people>
     <person>
       <name>Manoel</name>
       <city>Palmas-TO</city>
     </person>
   </people>'
   
   
   {"people":{"person":{"name":"Manoel","city":"Palmas-TO"}}}
   ```
   
   ## disable-plugin
   
   When you want to disable the `xml-json-conversion` plugin, it is very simple,
   you can delete the corresponding json configuration in the plugin configuration,
   no need to restart the service, it will take effect immediately:
   
   ```shell
   curl -i http://127.0.0.1:9080/apisix/admin/routes/1  -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
   {
       "uri": "/hello",
       "upstream": {
           "type": "roundrobin",
           "nodes": {
               "39.97.63.215:80": 1
           }
       }
   }'
   ```
   
   
   ### Pre-submission checklist:
   
   * [ ] Did you explain what problem does this PR solve? Or what new features have been added?
   * [ ] Have you added corresponding test cases?
   * [ ] Have you modified the corresponding document?
   * [ ] Is this PR backward compatible? **If it is not backward compatible, please discuss on the [mailing list](https://github.com/apache/apisix/tree/master#community) first**
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [apisix] tzssangglass commented on pull request #5016: feat: xml-json-conversion plugin convert xml data from request body to json response, and vice versa

Posted by GitBox <gi...@apache.org>.
tzssangglass commented on pull request #5016:
URL: https://github.com/apache/apisix/pull/5016#issuecomment-927019174


   test cases failed: https://github.com/apache/apisix/runs/3682877016#step:16:579


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [apisix] arthur-zhang commented on a change in pull request #5016: feat: xml-json-conversion plugin convert xml data from request body to json response, and vice versa

Posted by GitBox <gi...@apache.org>.
arthur-zhang commented on a change in pull request #5016:
URL: https://github.com/apache/apisix/pull/5016#discussion_r709725297



##########
File path: apisix/plugins/xml-json-conversion.lua
##########
@@ -0,0 +1,156 @@
+--
+-- 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.
+--
+
+local core    = require("apisix.core")
+local xml2lua = require("xml2lua")
+local handler = require("xmlhandler.tree")
+local cjson   = require('cjson.safe')
+local string  = require("string")
+
+local schema = {
+    type = "object",
+    properties = {
+        from = {
+            type = "string",
+            enum = {"json", "xml"},
+            default = "xml"
+        },
+        to = {
+            type = "string",
+            enum = {"json", "xml"},
+            default = "json"
+        }
+    },
+    additionalProperties = false,
+}
+
+local plugin_name = "xml-json-conversion"
+
+local _M = {
+    version = 0.1,
+    priority = 90,
+    name = plugin_name,
+    schema = schema,
+}
+
+function _M.check_schema(conf)
+    return core.schema.check(schema, conf)
+end
+
+local function xml2json(xml_data)
+    local convertHandler = handler:new()
+    local parser = xml2lua.parser(convertHandler)
+    parser:parse(xml_data)
+    return 200, cjson.encode(convertHandler.root)
+end
+
+local function json2xml(table_data)
+    local xmlStr = xml2lua.toXml(cjson.decode(table_data))
+    xmlStr = string.gsub(xmlStr, "%s+", "")

Review comment:
       I think Camel-Case naming is not good here




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [apisix] mangoGoForward commented on a change in pull request #5016: feat: xml-json-conversion plugin convert xml data from request body to json response, and vice versa

Posted by GitBox <gi...@apache.org>.
mangoGoForward commented on a change in pull request #5016:
URL: https://github.com/apache/apisix/pull/5016#discussion_r711433667



##########
File path: t/plugin/xml-json-conversion.t
##########
@@ -0,0 +1,222 @@
+#
+# 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';
+
+repeat_each(1);
+no_long_string();
+no_root_location();
+no_shuffle();
+log_level('info');
+run_tests;
+
+__DATA__
+
+=== TEST 1: sanity
+--- config
+    location /t {
+        content_by_lua_block {
+            local plugin = require("apisix.plugins.xml-json-conversion")
+            local conf = {from = "xml", to = "json"}
+
+            local ok, err = plugin.check_schema(conf)
+            if not ok then
+                ngx.say(err)
+            end
+
+            ngx.say("done")
+        }
+    }
+--- request
+GET /t
+--- response_body
+done
+--- no_error_log
+[error]
+
+
+
+=== TEST 2: create a route with the plugin which set from=xml and to=json
+--- 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,
+                [[{
+                    "plugins": {
+                        "xml-json-conversion": {
+                            "from": "xml",
+                            "to": "json"
+                        }
+                    },
+                    "upstream": {
+                        "nodes": {
+                            "127.0.0.1:1980": 1
+                        },
+                        "type": "roundrobin"
+                    },
+                    "uri": "/hello"
+                }]]
+            )
+            if code >= 300 then
+                ngx.status = code
+            end
+            ngx.say(body)
+        }
+    }
+--- request
+GET /t
+--- response_body
+passed
+--- no_error_log
+[error]
+
+
+=== TEST 3: test for unsupported operation
+--- request
+GET /hello
+<people>
+ <person>
+   <name>Manoel</name>
+   <city>Palmas-TO</city>
+ </person>
+</people>
+--- more_headers
+Content-Type: text/html
+--- error_code: 400
+--- response_body
+{"message":"Operation not supported"}
+--- no_error_log
+[error]
+
+=== TEST 4: test for unsupported operation
+--- request
+GET /hello
+<people>
+ <person>
+   <name>Manoel</name>
+   <city>Palmas-TO</city>
+ </person>
+</people>
+--- more_headers
+Content-Type: application/json
+--- error_code: 400
+--- response_body
+{"message":"Operation not supported"}
+--- no_error_log
+[error]
+
+
+=== TEST 5: verify in argument
+--- config
+    location /t {
+        content_by_lua_block {
+            local headers = {}
+            headers["Content-Type"] = "text/xml"
+            local t = require("lib.test_admin").test
+            local code, body = t('/hello',
+                ngx.HTTP_GET,
+                [[<people>
+                     <person>
+                       <name>Manoel</name>
+                       <city>Palmas-TO</city>
+                     </person>
+                   </people>]],
+               nil,
+               headers
+            )
+
+            if code > 200 then
+                ngx.status = code
+                ngx.say(err)
+                return
+            end
+
+            ngx.say(body)

Review comment:
       done.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [apisix] mangoGoForward edited a comment on pull request #5016: feat: xml-json-conversion plugin convert xml data from request body to json response, and vice versa

Posted by GitBox <gi...@apache.org>.
mangoGoForward edited a comment on pull request #5016:
URL: https://github.com/apache/apisix/pull/5016#issuecomment-947305115


   > I am curious about the use case of this plugin. If it is just converting XML to JSON online, and echoing it back, why should we add it as a feature of API gateway? Maybe we should pass the converted body to the upstream, which could be useful.
   
   I think so right now, thanks for your suggestion. @spacewander @arthur-zhang 


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [apisix] tzssangglass commented on a change in pull request #5016: feat: xml-json-conversion plugin convert xml data from request body to json response, and vice versa

Posted by GitBox <gi...@apache.org>.
tzssangglass commented on a change in pull request #5016:
URL: https://github.com/apache/apisix/pull/5016#discussion_r707056108



##########
File path: docs/zh/latest/plugins/xml-json-conversion.md
##########
@@ -0,0 +1,102 @@
+---
+title: xml-json-conversion
+---
+
+
+<!--
+#
+# 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.
+#
+-->
+
+## 目录
+
+- [**名称**](#名称)
+- [**属性**](#属性)
+- [**如何开启**](#如何开启)
+- [**测试插件**](#测试插件)
+- [**禁用插件**](#禁用插件)
+
+## 名称
+
+xml-json-conversion 插件意在将request body中的xml数据转换成json输出,或者将json数据转换成xml数据输出

Review comment:
       ```suggestion
   xml-json-conversion 插件意在将 request body 中的 xml 格式数据转换成 json 输出,或者将 json 格式数据转换成 xml 格式数据输出。
   ```

##########
File path: docs/zh/latest/plugins/xml-json-conversion.md
##########
@@ -0,0 +1,102 @@
+---
+title: xml-json-conversion
+---
+
+
+<!--
+#
+# 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.
+#
+-->
+
+## 目录
+
+- [**名称**](#名称)
+- [**属性**](#属性)
+- [**如何开启**](#如何开启)
+- [**测试插件**](#测试插件)
+- [**禁用插件**](#禁用插件)
+
+## 名称
+
+xml-json-conversion 插件意在将request body中的xml数据转换成json输出,或者将json数据转换成xml数据输出
+
+## 属性
+
+| 名称           | 类型    | 必选项   | 默认值   | 有效值                       | Description  |
+|:--------------|:--------|:--------|:-------|:----------------------------|:-------------|
+| from          | string  | 可选    | xml     | ["xml", "json"]             | 输入类型  |
+| to            | string  | 可选    | json    | ["xml", "json"]             | 输出类型  |
+
+## 如何开启
+
+下面是一个示例,如何在指定的路由上开启插件:
+
+```shell
+curl -i http://127.0.0.1:9080/apisix/admin/routes/1  -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
+{
+    "uri": "/hello",
+    "plugins": {
+        "xml-json-conversion": {
+            "from": "xml",
+            "to": "json"
+        }
+    },
+    "upstream": {
+        "type": "roundrobin",
+        "nodes": {
+            "39.97.63.215:80": 1
+        }
+    }
+}'
+```
+
+## 测试插件
+
+通过curl访问
+
+```shell
+curl -X GET http://127.0.0.1:9080/hello \
+-H 'Content-Type: text/xml' \
+-H 'Accept: application/json' \
+--data '
+<people>
+  <person>
+    <name>Manoel</name>
+    <city>Palmas-TO</city>
+  </person>
+</people>'
+
+
+{"people":{"person":{"name":"Manoel","city":"Palmas-TO"}}}
+```
+
+## 禁用插件
+
+想要禁用“xml-json-conversion”插件,是非常简单的,将对应的插件配置从 json 配置删除,就会立即生效,不需要重新启动服务:

Review comment:
       ```suggestion
   想要禁用 xml-json-conversion 插件,是非常简单的,将对应的插件配置从 json 配置删除,就会立即生效,不需要重新启动服务:
   ```

##########
File path: t/plugin/xml-json-conversion.t
##########
@@ -0,0 +1,222 @@
+#
+# 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';
+
+repeat_each(1);
+no_long_string();
+no_root_location();
+no_shuffle();
+log_level('info');
+run_tests;
+
+__DATA__
+
+=== TEST 1: sanity
+--- config
+    location /t {
+        content_by_lua_block {
+            local plugin = require("apisix.plugins.xml-json-conversion")
+            local conf = {from = "xml", to = "json"}
+
+            local ok, err = plugin.check_schema(conf)
+            if not ok then
+                ngx.say(err)
+            end
+
+            ngx.say("done")
+        }
+    }
+--- request
+GET /t
+--- response_body
+done
+--- no_error_log
+[error]
+
+
+
+=== TEST 2: create a route with the plugin which set from=xml and to=json
+--- 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,
+                [[{
+                    "plugins": {
+                        "xml-json-conversion": {
+                            "from": "xml",
+                            "to": "json"
+                        }
+                    },
+                    "upstream": {
+                        "nodes": {
+                            "127.0.0.1:1980": 1
+                        },
+                        "type": "roundrobin"
+                    },
+                    "uri": "/hello"
+                }]]
+            )
+            if code >= 300 then
+                ngx.status = code
+            end
+            ngx.say(body)
+        }
+    }
+--- request
+GET /t
+--- response_body
+passed
+--- no_error_log
+[error]
+
+
+=== TEST 3: test for unsupported operation
+--- request
+GET /hello
+<people>
+ <person>
+   <name>Manoel</name>
+   <city>Palmas-TO</city>
+ </person>
+</people>
+--- more_headers
+Content-Type: text/html
+--- error_code: 400
+--- response_body
+{"message":"Operation not supported"}
+--- no_error_log
+[error]
+
+=== TEST 4: test for unsupported operation
+--- request
+GET /hello
+<people>
+ <person>
+   <name>Manoel</name>
+   <city>Palmas-TO</city>
+ </person>
+</people>
+--- more_headers
+Content-Type: application/json
+--- error_code: 400
+--- response_body
+{"message":"Operation not supported"}
+--- no_error_log
+[error]
+
+
+=== TEST 5: verify in argument
+--- config
+    location /t {
+        content_by_lua_block {
+            local headers = {}
+            headers["Content-Type"] = "text/xml"
+            local t = require("lib.test_admin").test
+            local code, body = t('/hello',
+                ngx.HTTP_GET,
+                [[<people>
+                     <person>
+                       <name>Manoel</name>
+                       <city>Palmas-TO</city>
+                     </person>
+                   </people>]],
+               nil,
+               headers
+            )
+
+            if code > 200 then
+                ngx.status = code
+                ngx.say(err)
+                return
+            end
+
+            ngx.say(body)

Review comment:
       should check the request body is converted to json format?

##########
File path: docs/zh/latest/plugins/xml-json-conversion.md
##########
@@ -0,0 +1,102 @@
+---
+title: xml-json-conversion
+---
+
+
+<!--
+#
+# 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.
+#
+-->
+
+## 目录
+
+- [**名称**](#名称)
+- [**属性**](#属性)
+- [**如何开启**](#如何开启)
+- [**测试插件**](#测试插件)
+- [**禁用插件**](#禁用插件)
+
+## 名称
+
+xml-json-conversion 插件意在将request body中的xml数据转换成json输出,或者将json数据转换成xml数据输出

Review comment:
       Also make sure that after converting the data format, it is not `输出`, but passed upstream?




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [apisix] mangoGoForward commented on pull request #5016: feat: xml-json-conversion plugin convert xml data from request body to json response, and vice versa

Posted by GitBox <gi...@apache.org>.
mangoGoForward commented on pull request #5016:
URL: https://github.com/apache/apisix/pull/5016#issuecomment-924701571


   > @mangoGoForward hi, pls resolve the conflicting files
   
   done. Already resolved.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [apisix] mangoGoForward commented on a change in pull request #5016: feat: xml-json-conversion plugin convert xml data from request body to json response, and vice versa

Posted by GitBox <gi...@apache.org>.
mangoGoForward commented on a change in pull request #5016:
URL: https://github.com/apache/apisix/pull/5016#discussion_r711425469



##########
File path: apisix/plugins/xml-json-conversion.lua
##########
@@ -0,0 +1,156 @@
+--
+-- 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.
+--
+
+local core    = require("apisix.core")
+local xml2lua = require("xml2lua")
+local handler = require("xmlhandler.tree")
+local cjson   = require('cjson.safe')
+local string  = require("string")
+
+local schema = {
+    type = "object",
+    properties = {
+        from = {
+            type = "string",
+            enum = {"json", "xml"},
+            default = "xml"
+        },
+        to = {
+            type = "string",
+            enum = {"json", "xml"},
+            default = "json"
+        }
+    },
+    additionalProperties = false,
+}
+
+local plugin_name = "xml-json-conversion"
+
+local _M = {
+    version = 0.1,
+    priority = 90,

Review comment:
       done.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [apisix] spacewander commented on pull request #5016: feat: xml-json-conversion plugin convert xml data from request body to json response, and vice versa

Posted by GitBox <gi...@apache.org>.
spacewander commented on pull request #5016:
URL: https://github.com/apache/apisix/pull/5016#issuecomment-948195906


   ```
   "xml-json-conversion": {
               "from": "xml",
               "to": "json",
   			"echo_back": true
           }
   ```
   will give us the current behavior.
   
   
   ```
   "xml-json-conversion": {
               "from": "xml",
               "to": "json"
           }
   ```
   will pass the rewritten body to the upstream.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [apisix] mangoGoForward closed pull request #5016: feat: xml-json-conversion plugin convert xml data from request body to json response, and vice versa

Posted by GitBox <gi...@apache.org>.
mangoGoForward closed pull request #5016:
URL: https://github.com/apache/apisix/pull/5016


   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [apisix] mangoGoForward commented on pull request #5016: feat: xml-json-conversion plugin convert xml data from request body to json response, and vice versa

Posted by GitBox <gi...@apache.org>.
mangoGoForward commented on pull request #5016:
URL: https://github.com/apache/apisix/pull/5016#issuecomment-945593774


   Could you help me to review this PR? Or give me some suggestions @tokers @arthur-zhang 


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [apisix] arthur-zhang commented on pull request #5016: feat: xml-json-conversion plugin convert xml data from request body to json response, and vice versa

Posted by GitBox <gi...@apache.org>.
arthur-zhang commented on pull request #5016:
URL: https://github.com/apache/apisix/pull/5016#issuecomment-947296950


   > I am curious about the use case of this plugin. If it is just converting XML to JSON online, and echoing it back, why should we add it as a feature of API gateway? Maybe we should pass the converted body to the upstream, which could be useful.
   
   agree


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [apisix] mangoGoForward commented on a change in pull request #5016: feat: xml-json-conversion plugin convert xml data from request body to json response, and vice versa

Posted by GitBox <gi...@apache.org>.
mangoGoForward commented on a change in pull request #5016:
URL: https://github.com/apache/apisix/pull/5016#discussion_r711427450



##########
File path: apisix/plugins/xml-json-conversion.lua
##########
@@ -0,0 +1,156 @@
+--
+-- 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.
+--
+
+local core    = require("apisix.core")
+local xml2lua = require("xml2lua")
+local handler = require("xmlhandler.tree")
+local cjson   = require('cjson.safe')
+local string  = require("string")
+
+local schema = {
+    type = "object",
+    properties = {
+        from = {
+            type = "string",
+            enum = {"json", "xml"},
+            default = "xml"
+        },
+        to = {
+            type = "string",
+            enum = {"json", "xml"},
+            default = "json"
+        }
+    },
+    additionalProperties = false,
+}
+
+local plugin_name = "xml-json-conversion"
+
+local _M = {
+    version = 0.1,
+    priority = 90,
+    name = plugin_name,
+    schema = schema,
+}
+
+function _M.check_schema(conf)
+    return core.schema.check(schema, conf)
+end
+
+local function xml2json(xml_data)
+    local convertHandler = handler:new()
+    local parser = xml2lua.parser(convertHandler)
+    parser:parse(xml_data)
+    return 200, cjson.encode(convertHandler.root)
+end
+
+local function json2xml(table_data)
+    local xmlStr = xml2lua.toXml(cjson.decode(table_data))
+    xmlStr = string.gsub(xmlStr, "%s+", "")
+    return 200, xmlStr
+end
+
+local _switch_anonymous = {
+    ["json"] = function(content_type, req_body, to)
+        if "application/json" ~= content_type then

Review comment:
       done.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [apisix] tzssangglass commented on a change in pull request #5016: feat: xml-json-conversion plugin convert xml data from request body to json response, and vice versa

Posted by GitBox <gi...@apache.org>.
tzssangglass commented on a change in pull request #5016:
URL: https://github.com/apache/apisix/pull/5016#discussion_r707048234



##########
File path: rockspec/apisix-master-0.rockspec
##########
@@ -70,6 +70,8 @@ dependencies = {
     "ext-plugin-proto = 0.3.0",
     "casbin = 1.26.0",
     "api7-snowflake = 2.0-1",
+    "xml2lua = 1.5-2",
+    "lua-cjson = 2.1.0.6"

Review comment:
       `lua-cjson` has installed? refer to:https://github.com/apache/apisix/blob/baf843403461883c1334e63d15a6bb3622c31940/apisix/core/json.lua#L17
   cc @spacewander 

##########
File path: apisix/plugins/xml-json-conversion.lua
##########
@@ -0,0 +1,156 @@
+--
+-- 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.
+--
+
+local core    = require("apisix.core")
+local xml2lua = require("xml2lua")
+local handler = require("xmlhandler.tree")
+local cjson   = require('cjson.safe')
+local string  = require("string")
+
+local schema = {
+    type = "object",
+    properties = {
+        from = {
+            type = "string",
+            enum = {"json", "xml"},
+            default = "xml"
+        },
+        to = {
+            type = "string",
+            enum = {"json", "xml"},
+            default = "json"
+        }
+    },
+    additionalProperties = false,
+}
+
+local plugin_name = "xml-json-conversion"
+
+local _M = {
+    version = 0.1,
+    priority = 90,
+    name = plugin_name,
+    schema = schema,
+}
+
+function _M.check_schema(conf)
+    return core.schema.check(schema, conf)
+end
+
+local function xml2json(xml_data)
+    local convertHandler = handler:new()
+    local parser = xml2lua.parser(convertHandler)
+    parser:parse(xml_data)
+    return 200, cjson.encode(convertHandler.root)
+end
+
+local function json2xml(table_data)
+    local xmlStr = xml2lua.toXml(cjson.decode(table_data))
+    xmlStr = string.gsub(xmlStr, "%s+", "")
+    return 200, xmlStr
+end
+
+local _switch_anonymous = {
+    ["json"] = function(content_type, req_body, to)
+        if "application/json" ~= content_type then
+            return 400, {message = "Operation not supported"}
+        end
+
+        local data, err = core.json.decode(req_body)
+        if not data then
+            core.log.error("invalid request body: ", req_body, " err: ", err)
+            return 400, {error_msg = "invalid request body: " .. err,
+                         req_body = req_body}
+        end
+        if to == 'xml' then
+            return json2xml(req_body)
+        else
+            return 400, {message = "Operation not supported"}
+        end
+    end,
+    ["xml"] = function(content_type, req_body, to)
+        if "text/xml" ~= content_type then
+            return 400, {message = "Operation not supported"}
+        end
+        if to == 'json' then
+            return xml2json(req_body)
+        else
+            return 400, {message = "Operation not supported"}
+        end
+    end
+}
+
+function _M.access(conf, ctx)
+    local req_body, err = core.request.get_body()
+    if err or req_body == nil or req_body == '' then
+        core.log.error("failed to read request body: ", err)
+        core.response.exit(400, {error_msg = "invalid request body: " .. err})
+    end
+
+    local from = conf.from
+    local to = conf.to
+    if from == to then
+        return req_body
+    end
+
+    local content_type = core.request.headers()["Content-Type"]
+    local _f_anon = _switch_anonymous[from]
+    if _f_anon then
+        return _f_anon(content_type, req_body, to)
+    else
+        return 400, {message = "Operation not supported"}
+    end

Review comment:
       This code is almost the same as the following `get_json()`, can we extract it into a function?

##########
File path: apisix/plugins/xml-json-conversion.lua
##########
@@ -0,0 +1,156 @@
+--
+-- 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.
+--
+
+local core    = require("apisix.core")
+local xml2lua = require("xml2lua")
+local handler = require("xmlhandler.tree")
+local cjson   = require('cjson.safe')
+local string  = require("string")
+
+local schema = {
+    type = "object",
+    properties = {
+        from = {
+            type = "string",
+            enum = {"json", "xml"},
+            default = "xml"
+        },
+        to = {
+            type = "string",
+            enum = {"json", "xml"},
+            default = "json"
+        }
+    },
+    additionalProperties = false,
+}
+
+local plugin_name = "xml-json-conversion"
+
+local _M = {
+    version = 0.1,
+    priority = 90,

Review comment:
       in `config-defalt.yaml`, `priority` is 9, pls keep same.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [apisix] mangoGoForward commented on pull request #5016: feat: xml-json-conversion plugin convert xml data from request body to json response, and vice versa

Posted by GitBox <gi...@apache.org>.
mangoGoForward commented on pull request #5016:
URL: https://github.com/apache/apisix/pull/5016#issuecomment-948182299


   > We can introduce an "echo_back" option (default to false), so the current behavior can be persisted when echo_back is true.
   
   I‘m a little bit incomprehensible, could you please provide more detailed description. And It seems that the plugin is unsuitable     as a apisix plugin.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [apisix] spacewander commented on pull request #5016: feat: xml-json-conversion plugin convert xml data from request body to json response, and vice versa

Posted by GitBox <gi...@apache.org>.
spacewander commented on pull request #5016:
URL: https://github.com/apache/apisix/pull/5016#issuecomment-948154109


   We can introduce an "echo_back" option (default to false), so the current behavior can be persisted when echo_back is true.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [apisix] mangoGoForward commented on pull request #5016: feat: xml-json-conversion plugin convert xml data from request body to json response, and vice versa

Posted by GitBox <gi...@apache.org>.
mangoGoForward commented on pull request #5016:
URL: https://github.com/apache/apisix/pull/5016#issuecomment-947305115


   > I am curious about the use case of this plugin. If it is just converting XML to JSON online, and echoing it back, why should we add it as a feature of API gateway? Maybe we should pass the converted body to the upstream, which could be useful.
   
   I think so at now, thanks for your suggestion. @spacewander @arthur-zhang 


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [apisix] mangoGoForward commented on a change in pull request #5016: feat: xml-json-conversion plugin convert xml data from request body to json response, and vice versa

Posted by GitBox <gi...@apache.org>.
mangoGoForward commented on a change in pull request #5016:
URL: https://github.com/apache/apisix/pull/5016#discussion_r711425624



##########
File path: docs/zh/latest/plugins/xml-json-conversion.md
##########
@@ -0,0 +1,102 @@
+---
+title: xml-json-conversion
+---
+
+
+<!--
+#
+# 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.
+#
+-->
+
+## 目录
+
+- [**名称**](#名称)
+- [**属性**](#属性)
+- [**如何开启**](#如何开启)
+- [**测试插件**](#测试插件)
+- [**禁用插件**](#禁用插件)
+
+## 名称
+
+xml-json-conversion 插件意在将request body中的xml数据转换成json输出,或者将json数据转换成xml数据输出
+
+## 属性
+
+| 名称           | 类型    | 必选项   | 默认值   | 有效值                       | Description  |
+|:--------------|:--------|:--------|:-------|:----------------------------|:-------------|
+| from          | string  | 可选    | xml     | ["xml", "json"]             | 输入类型  |
+| to            | string  | 可选    | json    | ["xml", "json"]             | 输出类型  |
+
+## 如何开启
+
+下面是一个示例,如何在指定的路由上开启插件:
+
+```shell
+curl -i http://127.0.0.1:9080/apisix/admin/routes/1  -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
+{
+    "uri": "/hello",
+    "plugins": {
+        "xml-json-conversion": {
+            "from": "xml",
+            "to": "json"
+        }
+    },
+    "upstream": {
+        "type": "roundrobin",
+        "nodes": {
+            "39.97.63.215:80": 1
+        }
+    }
+}'
+```
+
+## 测试插件
+
+通过curl访问
+
+```shell
+curl -X GET http://127.0.0.1:9080/hello \
+-H 'Content-Type: text/xml' \
+-H 'Accept: application/json' \
+--data '
+<people>
+  <person>
+    <name>Manoel</name>
+    <city>Palmas-TO</city>
+  </person>
+</people>'
+
+
+{"people":{"person":{"name":"Manoel","city":"Palmas-TO"}}}
+```
+
+## 禁用插件
+
+想要禁用“xml-json-conversion”插件,是非常简单的,将对应的插件配置从 json 配置删除,就会立即生效,不需要重新启动服务:

Review comment:
       done

##########
File path: docs/zh/latest/plugins/xml-json-conversion.md
##########
@@ -0,0 +1,102 @@
+---
+title: xml-json-conversion
+---
+
+
+<!--
+#
+# 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.
+#
+-->
+
+## 目录
+
+- [**名称**](#名称)
+- [**属性**](#属性)
+- [**如何开启**](#如何开启)
+- [**测试插件**](#测试插件)
+- [**禁用插件**](#禁用插件)
+
+## 名称
+
+xml-json-conversion 插件意在将request body中的xml数据转换成json输出,或者将json数据转换成xml数据输出

Review comment:
       done




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [apisix] arthur-zhang commented on a change in pull request #5016: feat: xml-json-conversion plugin convert xml data from request body to json response, and vice versa

Posted by GitBox <gi...@apache.org>.
arthur-zhang commented on a change in pull request #5016:
URL: https://github.com/apache/apisix/pull/5016#discussion_r709726095



##########
File path: apisix/plugins/xml-json-conversion.lua
##########
@@ -0,0 +1,156 @@
+--
+-- 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.
+--
+
+local core    = require("apisix.core")
+local xml2lua = require("xml2lua")
+local handler = require("xmlhandler.tree")
+local cjson   = require('cjson.safe')
+local string  = require("string")
+
+local schema = {
+    type = "object",
+    properties = {
+        from = {
+            type = "string",
+            enum = {"json", "xml"},
+            default = "xml"
+        },
+        to = {
+            type = "string",
+            enum = {"json", "xml"},
+            default = "json"
+        }
+    },
+    additionalProperties = false,
+}
+
+local plugin_name = "xml-json-conversion"
+
+local _M = {
+    version = 0.1,
+    priority = 90,
+    name = plugin_name,
+    schema = schema,
+}
+
+function _M.check_schema(conf)
+    return core.schema.check(schema, conf)
+end
+
+local function xml2json(xml_data)
+    local convertHandler = handler:new()
+    local parser = xml2lua.parser(convertHandler)
+    parser:parse(xml_data)
+    return 200, cjson.encode(convertHandler.root)
+end
+
+local function json2xml(table_data)
+    local xmlStr = xml2lua.toXml(cjson.decode(table_data))
+    xmlStr = string.gsub(xmlStr, "%s+", "")
+    return 200, xmlStr
+end
+
+local _switch_anonymous = {
+    ["json"] = function(content_type, req_body, to)
+        if "application/json" ~= content_type then

Review comment:
       Maybe you should use the flowing code to check header is json ,not the equal compare
   ```
   if string.find(content_type, "application/json", 1, true)
   ```




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [apisix] mangoGoForward commented on a change in pull request #5016: feat: xml-json-conversion plugin convert xml data from request body to json response, and vice versa

Posted by GitBox <gi...@apache.org>.
mangoGoForward commented on a change in pull request #5016:
URL: https://github.com/apache/apisix/pull/5016#discussion_r711433538



##########
File path: apisix/plugins/xml-json-conversion.lua
##########
@@ -0,0 +1,156 @@
+--
+-- 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.
+--
+
+local core    = require("apisix.core")
+local xml2lua = require("xml2lua")
+local handler = require("xmlhandler.tree")
+local cjson   = require('cjson.safe')
+local string  = require("string")
+
+local schema = {
+    type = "object",
+    properties = {
+        from = {
+            type = "string",
+            enum = {"json", "xml"},
+            default = "xml"
+        },
+        to = {
+            type = "string",
+            enum = {"json", "xml"},
+            default = "json"
+        }
+    },
+    additionalProperties = false,
+}
+
+local plugin_name = "xml-json-conversion"
+
+local _M = {
+    version = 0.1,
+    priority = 90,
+    name = plugin_name,
+    schema = schema,
+}
+
+function _M.check_schema(conf)
+    return core.schema.check(schema, conf)
+end
+
+local function xml2json(xml_data)
+    local convertHandler = handler:new()
+    local parser = xml2lua.parser(convertHandler)
+    parser:parse(xml_data)
+    return 200, cjson.encode(convertHandler.root)
+end
+
+local function json2xml(table_data)
+    local xmlStr = xml2lua.toXml(cjson.decode(table_data))
+    xmlStr = string.gsub(xmlStr, "%s+", "")
+    return 200, xmlStr
+end
+
+local _switch_anonymous = {
+    ["json"] = function(content_type, req_body, to)
+        if "application/json" ~= content_type then
+            return 400, {message = "Operation not supported"}
+        end
+
+        local data, err = core.json.decode(req_body)
+        if not data then
+            core.log.error("invalid request body: ", req_body, " err: ", err)
+            return 400, {error_msg = "invalid request body: " .. err,
+                         req_body = req_body}
+        end
+        if to == 'xml' then
+            return json2xml(req_body)
+        else
+            return 400, {message = "Operation not supported"}
+        end
+    end,
+    ["xml"] = function(content_type, req_body, to)
+        if "text/xml" ~= content_type then
+            return 400, {message = "Operation not supported"}
+        end
+        if to == 'json' then
+            return xml2json(req_body)
+        else
+            return 400, {message = "Operation not supported"}
+        end
+    end
+}
+
+function _M.access(conf, ctx)
+    local req_body, err = core.request.get_body()
+    if err or req_body == nil or req_body == '' then
+        core.log.error("failed to read request body: ", err)
+        core.response.exit(400, {error_msg = "invalid request body: " .. err})
+    end
+
+    local from = conf.from
+    local to = conf.to
+    if from == to then
+        return req_body
+    end
+
+    local content_type = core.request.headers()["Content-Type"]
+    local _f_anon = _switch_anonymous[from]
+    if _f_anon then
+        return _f_anon(content_type, req_body, to)
+    else
+        return 400, {message = "Operation not supported"}
+    end

Review comment:
       done. Thanks




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [apisix] mangoGoForward commented on a change in pull request #5016: feat: xml-json-conversion plugin convert xml data from request body to json response, and vice versa

Posted by GitBox <gi...@apache.org>.
mangoGoForward commented on a change in pull request #5016:
URL: https://github.com/apache/apisix/pull/5016#discussion_r711426396



##########
File path: apisix/plugins/xml-json-conversion.lua
##########
@@ -0,0 +1,156 @@
+--
+-- 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.
+--
+
+local core    = require("apisix.core")
+local xml2lua = require("xml2lua")
+local handler = require("xmlhandler.tree")
+local cjson   = require('cjson.safe')
+local string  = require("string")
+
+local schema = {
+    type = "object",
+    properties = {
+        from = {
+            type = "string",
+            enum = {"json", "xml"},
+            default = "xml"
+        },
+        to = {
+            type = "string",
+            enum = {"json", "xml"},
+            default = "json"
+        }
+    },
+    additionalProperties = false,
+}
+
+local plugin_name = "xml-json-conversion"
+
+local _M = {
+    version = 0.1,
+    priority = 90,
+    name = plugin_name,
+    schema = schema,
+}
+
+function _M.check_schema(conf)
+    return core.schema.check(schema, conf)
+end
+
+local function xml2json(xml_data)
+    local convertHandler = handler:new()

Review comment:
       done.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [apisix] mangoGoForward commented on pull request #5016: feat: xml-json-conversion plugin convert xml data from request body to json response, and vice versa

Posted by GitBox <gi...@apache.org>.
mangoGoForward commented on pull request #5016:
URL: https://github.com/apache/apisix/pull/5016#issuecomment-927023808


   > > > What are scenes can this plugin be used?
   > > 
   > > 
   > > The plugin can rewrite the content returned upstream as well as APACHE APISIX itself. Users can specify input and output protocol what they want, it only support xml and json protocol conversion mutually at now
   > 
   > why not make a plugin to transform the body before return the response instead of send it again to apisix. Based on `Content-Type` header
   
   Yes,  But you may must have to parse the content from http request body which does not match you wish, this plugin can process this scene, and you don't make a plugin in anywhere you may use


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [apisix] arthur-zhang commented on pull request #5016: feat: xml-json-conversion plugin convert xml data from request body to json response, and vice versa

Posted by GitBox <gi...@apache.org>.
arthur-zhang commented on pull request #5016:
URL: https://github.com/apache/apisix/pull/5016#issuecomment-924522662


   What are scenes can this plugin be used?


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [apisix] mangoGoForward commented on pull request #5016: feat: xml-json-conversion plugin convert xml data from request body to json response, and vice versa

Posted by GitBox <gi...@apache.org>.
mangoGoForward commented on pull request #5016:
URL: https://github.com/apache/apisix/pull/5016#issuecomment-924739769


   > What are scenes can this plugin be used?
   
   The plugin can rewrite the content returned upstream as well as APACHE APISIX itself. Users can specify input and output protocol what they want, it only support xml and json protocol conversion mutually at now


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [apisix] spacewander commented on pull request #5016: feat: xml-json-conversion plugin convert xml data from request body to json response, and vice versa

Posted by GitBox <gi...@apache.org>.
spacewander commented on pull request #5016:
URL: https://github.com/apache/apisix/pull/5016#issuecomment-947296603


   I am curious about the use case of this plugin. If it is just converting XML to JSON online, and echoing it back, why should we add it as a feature of API gateway? Maybe we should pass the converted body to the upstream, which could be useful.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [apisix] mangoGoForward commented on pull request #5016: feat: xml-json-conversion plugin convert xml data from request body to json response, and vice versa

Posted by GitBox <gi...@apache.org>.
mangoGoForward commented on pull request #5016:
URL: https://github.com/apache/apisix/pull/5016#issuecomment-917763404


   @tzssangglass @tokers Can you help me review this PR? Thanks


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [apisix] arthur-zhang commented on pull request #5016: feat: xml-json-conversion plugin convert xml data from request body to json response, and vice versa

Posted by GitBox <gi...@apache.org>.
arthur-zhang commented on pull request #5016:
URL: https://github.com/apache/apisix/pull/5016#issuecomment-925462244


   > > What are scenes can this plugin be used?
   > 
   > The plugin can rewrite the content returned upstream as well as APACHE APISIX itself. Users can specify input and output protocol what they want, it only support xml and json protocol conversion mutually at now
   
   why not make a plugin to transform the body before return the response instead of send it again to apisix. Based on `Content-Type` header
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [apisix] arthur-zhang commented on a change in pull request #5016: feat: xml-json-conversion plugin convert xml data from request body to json response, and vice versa

Posted by GitBox <gi...@apache.org>.
arthur-zhang commented on a change in pull request #5016:
URL: https://github.com/apache/apisix/pull/5016#discussion_r709725387



##########
File path: apisix/plugins/xml-json-conversion.lua
##########
@@ -0,0 +1,156 @@
+--
+-- 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.
+--
+
+local core    = require("apisix.core")
+local xml2lua = require("xml2lua")
+local handler = require("xmlhandler.tree")
+local cjson   = require('cjson.safe')
+local string  = require("string")
+
+local schema = {
+    type = "object",
+    properties = {
+        from = {
+            type = "string",
+            enum = {"json", "xml"},
+            default = "xml"
+        },
+        to = {
+            type = "string",
+            enum = {"json", "xml"},
+            default = "json"
+        }
+    },
+    additionalProperties = false,
+}
+
+local plugin_name = "xml-json-conversion"
+
+local _M = {
+    version = 0.1,
+    priority = 90,
+    name = plugin_name,
+    schema = schema,
+}
+
+function _M.check_schema(conf)
+    return core.schema.check(schema, conf)
+end
+
+local function xml2json(xml_data)
+    local convertHandler = handler:new()

Review comment:
       I think Camel-Case naming is not good here




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [apisix] mangoGoForward commented on pull request #5016: feat: xml-json-conversion plugin convert xml data from request body to json response, and vice versa

Posted by GitBox <gi...@apache.org>.
mangoGoForward commented on pull request #5016:
URL: https://github.com/apache/apisix/pull/5016#issuecomment-927019742


   > test cases failed: https://github.com/apache/apisix/runs/3682877016#step:16:579
   
   I got some errors like :
   `Error: [error] [lua] config_yaml.lua:66: read_apisix_yaml(): failed to fetch /home/runner/work/apisix/apisix/t/servroot/conf/apisix.yaml attributes: cannot obtain information from file '/home/runner/work/apisix/apisix/t/servroot/conf/apisix.yaml': No such file or directory
   `
   But I have no change with those plugin's test cases in my forked branch


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [apisix] mangoGoForward commented on pull request #5016: feat: xml-json-conversion plugin convert xml data from request body to json response, and vice versa

Posted by GitBox <gi...@apache.org>.
mangoGoForward commented on pull request #5016:
URL: https://github.com/apache/apisix/pull/5016#issuecomment-948387323


   > ```
   > "xml-json-conversion": {
   >             "from": "xml",
   >             "to": "json",
   > 			"echo_back": true
   >         }
   > ```
   > 
   > will give us the current behavior.
   > 
   > ```
   > "xml-json-conversion": {
   >             "from": "xml",
   >             "to": "json"
   >         }
   > ```
   > 
   > will pass the rewritten body to the upstream.
   
   I think `echo_back` is unneccessary, we can registry a route or consumer with this plugin if need to process, and if not need, why we should use this plugin. I'm confused that the plugin's scenes mainly, maybe close this PR is better.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [apisix] tzssangglass commented on pull request #5016: feat: xml-json-conversion plugin convert xml data from request body to json response, and vice versa

Posted by GitBox <gi...@apache.org>.
tzssangglass commented on pull request #5016:
URL: https://github.com/apache/apisix/pull/5016#issuecomment-924518065


   @mangoGoForward hi, pls resolve the conflicting files


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [apisix] mangoGoForward commented on a change in pull request #5016: feat: xml-json-conversion plugin convert xml data from request body to json response, and vice versa

Posted by GitBox <gi...@apache.org>.
mangoGoForward commented on a change in pull request #5016:
URL: https://github.com/apache/apisix/pull/5016#discussion_r711433788



##########
File path: apisix/plugins/xml-json-conversion.lua
##########
@@ -0,0 +1,156 @@
+--
+-- 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.
+--
+
+local core    = require("apisix.core")
+local xml2lua = require("xml2lua")
+local handler = require("xmlhandler.tree")
+local cjson   = require('cjson.safe')
+local string  = require("string")
+
+local schema = {
+    type = "object",
+    properties = {
+        from = {
+            type = "string",
+            enum = {"json", "xml"},
+            default = "xml"
+        },
+        to = {
+            type = "string",
+            enum = {"json", "xml"},
+            default = "json"
+        }
+    },
+    additionalProperties = false,
+}
+
+local plugin_name = "xml-json-conversion"
+
+local _M = {
+    version = 0.1,
+    priority = 90,
+    name = plugin_name,
+    schema = schema,
+}
+
+function _M.check_schema(conf)
+    return core.schema.check(schema, conf)
+end
+
+local function xml2json(xml_data)
+    local convertHandler = handler:new()
+    local parser = xml2lua.parser(convertHandler)
+    parser:parse(xml_data)
+    return 200, cjson.encode(convertHandler.root)
+end
+
+local function json2xml(table_data)
+    local xmlStr = xml2lua.toXml(cjson.decode(table_data))
+    xmlStr = string.gsub(xmlStr, "%s+", "")

Review comment:
       done. Thanks




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [apisix] mangoGoForward commented on pull request #5016: feat: xml-json-conversion plugin convert xml data from request body to json response, and vice versa

Posted by GitBox <gi...@apache.org>.
mangoGoForward commented on pull request #5016:
URL: https://github.com/apache/apisix/pull/5016#issuecomment-927018146


   The fuzzing failed, I'm confused about this error, who can I fix it. could you please give me some advices?
   `Run python $PWD/t/fuzzing/client_abort.py
   [61886464, 62038016, 61984768, 61833216, 61861888, 61861888, 61927424, 61902848, 61865984, 61865984, 61865984, 61874176, 61874176, 61874176, 61923328, 61882368, 61861888, 61882368, 61890560, 61894656, 61853696, 61853696, 61853696, 61927424, 61972480, 61972480, 61906944, 61997056, 62017536, 62038016, 61947904, 61947904, 61911040, 62025728, 61935616, 62025728, 62038016, 62038016, 61943808, 61943808, 61915136, 61915136, 61956096, 61956096, 61952000, 62025728, 61931520, 61931520, 61874176, 61874176, 61890560, 61890560, 61915136, 61997056, 62021632, 62287872, 62386176, 62521344, 62513152, 62513152, 62742528, 62742528, 62758912, 62849024, 62767104, 62849024, 62779392, 62779392, 62697472, 62779392, 62779392, 62779392, 62697472, 62779392, 62783488, 62783488, 62758912, 62795776, 62771200, 62857216, 62779392, 62844928, 62803968, 62803968, 62767104, 62767104, 62734336, 62767104, 62820352, 62734336, 62775296, 62861312, 62787584, 62701568, 62775296, 62775296, 62689280, 62689280, 62689280, 6268
 9280]
   12516.579657965796
   0.4
   Error in log:  b''
   Error in log:  b''
   Error in log:  b''
   Traceback (most recent call last):
     File "/home/runner/work/apisix/apisix/t/fuzzing/simple_http.py", line 121, in <module>
       run_test(create_route, run)
     File "/home/runner/work/apisix/apisix/t/fuzzing/public.py", line 126, in run_test
       create_route()
     File "/home/runner/work/apisix/apisix/t/fuzzing/simple_http.py", line 49, in create_route
       assert response.status <= 300, response.read()
   AssertionError: b'{"error_msg":"invalid plugins configuration: unknown plugin [jwt-auth]"}\n'
   Error: Process completed with exit code 1.`


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [apisix] mangoGoForward commented on a change in pull request #5016: feat: xml-json-conversion plugin convert xml data from request body to json response, and vice versa

Posted by GitBox <gi...@apache.org>.
mangoGoForward commented on a change in pull request #5016:
URL: https://github.com/apache/apisix/pull/5016#discussion_r711425454



##########
File path: rockspec/apisix-master-0.rockspec
##########
@@ -70,6 +70,8 @@ dependencies = {
     "ext-plugin-proto = 0.3.0",
     "casbin = 1.26.0",
     "api7-snowflake = 2.0-1",
+    "xml2lua = 1.5-2",
+    "lua-cjson = 2.1.0.6"

Review comment:
       done. lua-cjson is unneccessary




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [apisix] spacewander commented on a change in pull request #5016: feat: xml-json-conversion plugin convert xml data from request body to json response, and vice versa

Posted by GitBox <gi...@apache.org>.
spacewander commented on a change in pull request #5016:
URL: https://github.com/apache/apisix/pull/5016#discussion_r732386842



##########
File path: apisix/plugins/xml-json-conversion.lua
##########
@@ -0,0 +1,149 @@
+--
+-- 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.
+--
+
+local core    = require("apisix.core")
+local handler = require("xmlhandler.tree")
+local string  = require("string")
+local parser  = require("xml2lua").parser
+local table_to_xml  = require("xml2lua").toXml
+local json_decode   = require('cjson.safe').decode

Review comment:
       Better to use core.json

##########
File path: apisix/plugins/xml-json-conversion.lua
##########
@@ -0,0 +1,149 @@
+--
+-- 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.
+--
+
+local core    = require("apisix.core")
+local handler = require("xmlhandler.tree")
+local string  = require("string")
+local parser  = require("xml2lua").parser
+local table_to_xml  = require("xml2lua").toXml
+local json_decode   = require('cjson.safe').decode
+local json_encode   = require('cjson.safe').encode
+
+
+local schema = {
+    type = "object",
+    properties = {
+        from = {
+            type = "string",
+            enum = {"json", "xml"},
+            default = "xml"
+        },
+        to = {
+            type = "string",
+            enum = {"json", "xml"},
+            default = "json"
+        }
+    },
+    additionalProperties = false,
+}
+
+local plugin_name = "xml-json-conversion"
+
+local _M = {
+    version = 0.1,
+    priority = 9,
+    name = plugin_name,
+    schema = schema,
+}
+
+function _M.check_schema(conf)
+    return core.schema.check(schema, conf)
+end
+
+local function xml2json(xml_data)
+    local convert_handler = handler:new()
+    local parser_handler = parser(convert_handler)
+    parser_handler:parse(xml_data)
+    return 200, json_encode(convert_handler.root)
+end
+
+local function json2xml(table_data)
+    local xmlStr = table_to_xml(json_decode(table_data))
+    xmlStr = string.gsub(xmlStr, "%s+", "")

Review comment:
       Could we use `re.gsub`?

##########
File path: conf/config-default.yaml
##########
@@ -347,6 +347,7 @@ plugins:                          # plugin list (sorted by priority)
   #- log-rotate                    # priority: 100
   # <- recommend to use priority (0, 100) for your custom plugins
   - example-plugin                 # priority: 0
+  - xml-json-conversion            # priority: 9

Review comment:
       Why "priority: 9" is after "priority: 0"? And we should avoid '(0, 100)' for builtin plugins

##########
File path: apisix/plugins/xml-json-conversion.lua
##########
@@ -0,0 +1,149 @@
+--
+-- 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.
+--
+
+local core    = require("apisix.core")
+local handler = require("xmlhandler.tree")
+local string  = require("string")
+local parser  = require("xml2lua").parser
+local table_to_xml  = require("xml2lua").toXml
+local json_decode   = require('cjson.safe').decode
+local json_encode   = require('cjson.safe').encode
+
+
+local schema = {
+    type = "object",
+    properties = {
+        from = {
+            type = "string",
+            enum = {"json", "xml"},
+            default = "xml"
+        },
+        to = {
+            type = "string",
+            enum = {"json", "xml"},
+            default = "json"
+        }
+    },
+    additionalProperties = false,
+}
+
+local plugin_name = "xml-json-conversion"
+
+local _M = {
+    version = 0.1,
+    priority = 9,
+    name = plugin_name,
+    schema = schema,
+}
+
+function _M.check_schema(conf)
+    return core.schema.check(schema, conf)
+end
+
+local function xml2json(xml_data)
+    local convert_handler = handler:new()
+    local parser_handler = parser(convert_handler)
+    parser_handler:parse(xml_data)

Review comment:
       We should check if the parse fails.

##########
File path: apisix/plugins/xml-json-conversion.lua
##########
@@ -0,0 +1,149 @@
+--
+-- 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.
+--
+
+local core    = require("apisix.core")
+local handler = require("xmlhandler.tree")
+local string  = require("string")
+local parser  = require("xml2lua").parser
+local table_to_xml  = require("xml2lua").toXml
+local json_decode   = require('cjson.safe').decode
+local json_encode   = require('cjson.safe').encode
+
+
+local schema = {
+    type = "object",
+    properties = {
+        from = {
+            type = "string",
+            enum = {"json", "xml"},
+            default = "xml"
+        },
+        to = {
+            type = "string",
+            enum = {"json", "xml"},
+            default = "json"
+        }
+    },
+    additionalProperties = false,
+}
+
+local plugin_name = "xml-json-conversion"
+
+local _M = {
+    version = 0.1,
+    priority = 9,
+    name = plugin_name,
+    schema = schema,
+}
+
+function _M.check_schema(conf)
+    return core.schema.check(schema, conf)
+end
+
+local function xml2json(xml_data)
+    local convert_handler = handler:new()
+    local parser_handler = parser(convert_handler)
+    parser_handler:parse(xml_data)
+    return 200, json_encode(convert_handler.root)
+end
+
+local function json2xml(table_data)
+    local xmlStr = table_to_xml(json_decode(table_data))
+    xmlStr = string.gsub(xmlStr, "%s+", "")
+    return 200, xmlStr
+end
+
+local _switch_anonymous = {
+    ["json"] = function(content_type, req_body, to)
+        if string.find(content_type, "application/json", 1, true) == nil then

Review comment:
       Better to use core.string.find

##########
File path: apisix/plugins/xml-json-conversion.lua
##########
@@ -0,0 +1,149 @@
+--
+-- 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.
+--
+
+local core    = require("apisix.core")
+local handler = require("xmlhandler.tree")
+local string  = require("string")
+local parser  = require("xml2lua").parser
+local table_to_xml  = require("xml2lua").toXml
+local json_decode   = require('cjson.safe').decode
+local json_encode   = require('cjson.safe').encode
+
+
+local schema = {
+    type = "object",
+    properties = {
+        from = {
+            type = "string",
+            enum = {"json", "xml"},
+            default = "xml"
+        },
+        to = {
+            type = "string",
+            enum = {"json", "xml"},
+            default = "json"
+        }
+    },
+    additionalProperties = false,

Review comment:
       We have already removed `additionalProperties` from other plugins.

##########
File path: apisix/plugins/xml-json-conversion.lua
##########
@@ -0,0 +1,149 @@
+--
+-- 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.
+--
+
+local core    = require("apisix.core")
+local handler = require("xmlhandler.tree")
+local string  = require("string")
+local parser  = require("xml2lua").parser
+local table_to_xml  = require("xml2lua").toXml
+local json_decode   = require('cjson.safe').decode
+local json_encode   = require('cjson.safe').encode
+
+
+local schema = {
+    type = "object",
+    properties = {
+        from = {
+            type = "string",
+            enum = {"json", "xml"},
+            default = "xml"
+        },
+        to = {
+            type = "string",
+            enum = {"json", "xml"},
+            default = "json"
+        }
+    },
+    additionalProperties = false,
+}
+
+local plugin_name = "xml-json-conversion"
+
+local _M = {
+    version = 0.1,
+    priority = 9,
+    name = plugin_name,
+    schema = schema,
+}
+
+function _M.check_schema(conf)
+    return core.schema.check(schema, conf)
+end
+
+local function xml2json(xml_data)
+    local convert_handler = handler:new()
+    local parser_handler = parser(convert_handler)
+    parser_handler:parse(xml_data)
+    return 200, json_encode(convert_handler.root)
+end
+
+local function json2xml(table_data)
+    local xmlStr = table_to_xml(json_decode(table_data))
+    xmlStr = string.gsub(xmlStr, "%s+", "")
+    return 200, xmlStr
+end
+
+local _switch_anonymous = {
+    ["json"] = function(content_type, req_body, to)
+        if string.find(content_type, "application/json", 1, true) == nil then
+            return 400, {message = "Operation not supported"}
+        end
+
+        local data, err = core.json.decode(req_body)
+        if not data then
+            core.log.error("invalid request body: ", req_body, " err: ", err)
+            return 400, {error_msg = "invalid request body: " .. err,
+                         req_body = req_body}
+        end
+        if to == 'xml' then
+            return json2xml(req_body)
+        else
+            return 400, {message = "Operation not supported"}
+        end
+    end,
+    ["xml"] = function(content_type, req_body, to)
+        if string.find(content_type, "text/xml", 1, true) == nil then
+            return 400, {message = "Operation not supported"}
+        end
+        if to == 'json' then
+            return xml2json(req_body)
+        else
+            return 400, {message = "Operation not supported"}
+        end
+    end
+}
+
+local function process(from, to)
+    local req_body, err = core.request.get_body()
+    if err or req_body == nil or req_body == '' then
+        core.log.error("failed to read request body: ", err)
+        core.response.exit(400, {error_msg = "invalid request body: " .. err})
+    end
+
+    if from == to then
+        return req_body
+    end
+
+    local content_type = core.request.headers()["Content-Type"]
+    local _f_anon = _switch_anonymous[from]
+    if _f_anon then
+        return _f_anon(content_type, req_body, to)
+    else
+        return 400, {message = "Operation not supported"}
+    end
+end
+
+function _M.access(conf, ctx)
+    local from = conf.from
+    local to = conf.to
+
+    return process(from, to)
+end
+
+local function conversion()
+    local args = core.request.get_uri_args()
+    if not args or not args.from or not args.to then
+        return core.response.exit(400)
+    end
+
+    local from = args.from
+    local to = args.to
+
+    return process(from, to)
+end
+
+function _M.api()
+    return {
+        {
+            methods = {"GET"},
+            uri = "/apisix/plugin/xml-json-conversion",

Review comment:
       Could we remove the API? We don't want to expose more APIs to the public network.

##########
File path: apisix/plugins/xml-json-conversion.lua
##########
@@ -0,0 +1,149 @@
+--
+-- 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.
+--
+
+local core    = require("apisix.core")
+local handler = require("xmlhandler.tree")
+local string  = require("string")
+local parser  = require("xml2lua").parser
+local table_to_xml  = require("xml2lua").toXml
+local json_decode   = require('cjson.safe').decode
+local json_encode   = require('cjson.safe').encode
+
+
+local schema = {
+    type = "object",
+    properties = {
+        from = {
+            type = "string",
+            enum = {"json", "xml"},
+            default = "xml"
+        },
+        to = {
+            type = "string",
+            enum = {"json", "xml"},
+            default = "json"
+        }
+    },
+    additionalProperties = false,
+}
+
+local plugin_name = "xml-json-conversion"
+
+local _M = {
+    version = 0.1,
+    priority = 9,
+    name = plugin_name,
+    schema = schema,
+}
+
+function _M.check_schema(conf)
+    return core.schema.check(schema, conf)
+end
+
+local function xml2json(xml_data)
+    local convert_handler = handler:new()
+    local parser_handler = parser(convert_handler)
+    parser_handler:parse(xml_data)
+    return 200, json_encode(convert_handler.root)
+end
+
+local function json2xml(table_data)
+    local xmlStr = table_to_xml(json_decode(table_data))

Review comment:
       Ditto




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [apisix] spacewander commented on a change in pull request #5016: feat: xml-json-conversion plugin convert xml data from request body to json response, and vice versa

Posted by GitBox <gi...@apache.org>.
spacewander commented on a change in pull request #5016:
URL: https://github.com/apache/apisix/pull/5016#discussion_r732391129



##########
File path: t/plugin/xml-json-conversion.t
##########
@@ -0,0 +1,206 @@
+#
+# 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';
+
+repeat_each(1);
+no_long_string();
+no_root_location();
+no_shuffle();
+log_level('info');
+run_tests;
+
+__DATA__
+
+=== TEST 1: sanity
+--- config
+    location /t {
+        content_by_lua_block {
+            local plugin = require("apisix.plugins.xml-json-conversion")
+            local conf = {from = "xml", to = "json"}
+
+            local ok, err = plugin.check_schema(conf)
+            if not ok then
+                ngx.say(err)
+            end
+
+            ngx.say("done")
+        }
+    }
+--- request
+GET /t
+--- response_body
+done
+--- no_error_log

Review comment:
       We can check no_error_log by default like:
   https://github.com/apache/apisix/blob/b5d72cbf1315f1efc80689246a7dcf124c9e6443/t/plugin/gzip.t#L40




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org