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/02 13:17:16 UTC

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

tzssangglass commented on a change in pull request #4819:
URL: https://github.com/apache/apisix/pull/4819#discussion_r701061541



##########
File path: apisix/plugins/xml-json-conversion.lua
##########
@@ -0,0 +1,132 @@
+--
+-- 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  = string
+
+local schema = {
+    type = "object",
+    properties = {},
+    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)
+    return 200, xml2lua.toXml(cjson.decode(table_data))
+end
+
+function _M.access(conf, ctx)
+    local request_header = core.request.headers()
+    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
+

Review comment:
       ```suggestion
       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 request_header = core.request.headers()
   ```

##########
File path: apisix/plugins/xml-json-conversion.lua
##########
@@ -0,0 +1,132 @@
+--
+-- 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  = string
+
+local schema = {
+    type = "object",
+    properties = {},
+    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)
+    return 200, xml2lua.toXml(cjson.decode(table_data))
+end
+
+function _M.access(conf, ctx)
+    local request_header = core.request.headers()
+    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 request_header["Content-Type"] == "application/json" then

Review comment:
       I think the plugin just needs to focus on the `Content-Type` is `text/xml`, and if it is, then call `xml2json`, and if it isn't, then it doesn't handle it.
   
   And don't need to care about `Accept`, @spacewander @tokers What's your opinion?
   
   




-- 
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