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: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
