fix: 修复插件导入kv类型body报错

This commit is contained in:
chenjianxing 2020-08-05 13:08:18 +08:00
parent 1faa4b6e8c
commit 3a8dc9ee04
1 changed files with 36 additions and 13 deletions

View File

@ -11,7 +11,6 @@ import org.apache.commons.lang3.StringUtils;
import org.eclipse.jetty.http.HttpMethod;
import java.io.InputStream;
import java.util.Map;
public class MsParser extends ApiImportAbstractParser {
@ -49,18 +48,7 @@ public class MsParser extends ApiImportAbstractParser {
requestObject.put(key, requestTmpObject.get(key));
});;
requestObject.put("name", requestName);
JSONArray bodies = requestObject.getJSONArray("body");
if (StringUtils.equalsIgnoreCase(requestObject.getString("method"), HttpMethod.POST.name()) && bodies != null) {
StringBuilder bodyStr = new StringBuilder();
for (int i = 0; i < bodies.size(); i++) {
String body = bodies.getString(i);
bodyStr.append(body);
}
JSONObject bodyObject = new JSONObject();
bodyObject.put("raw", bodyStr);
bodyObject.put("type", MsRequestBodyType.RAW.value());
requestObject.put("body", bodyObject);
}
parseBody(requestObject);
requestsObjects.add(requestObject);
});
scenario.put("requests", requestsObjects);
@ -71,4 +59,39 @@ public class MsParser extends ApiImportAbstractParser {
return result.toJSONString();
}
}
private void parseBody(JSONObject requestObject) {
if (requestObject.containsKey("body")) {
Object body = requestObject.get("body");
if (body instanceof JSONArray) {
JSONArray bodies = requestObject.getJSONArray("body");
if (StringUtils.equalsIgnoreCase(requestObject.getString("method"), HttpMethod.POST.name()) && bodies != null) {
StringBuilder bodyStr = new StringBuilder();
for (int i = 0; i < bodies.size(); i++) {
String tmp = bodies.getString(i);
bodyStr.append(tmp);
}
JSONObject bodyObject = new JSONObject();
bodyObject.put("raw", bodyStr);
bodyObject.put("type", MsRequestBodyType.RAW.value());
requestObject.put("body", bodyObject);
}
} else if (body instanceof JSONObject) {
JSONObject bodyObj = requestObject.getJSONObject("body");
if (StringUtils.equalsIgnoreCase(requestObject.getString("method"), HttpMethod.POST.name()) && bodyObj != null) {
JSONArray kvs = new JSONArray();
bodyObj.keySet().forEach(key -> {
JSONObject kv = new JSONObject();
kv.put("name", key);
kv.put("value", bodyObj.getString(key));
kvs.add(kv);
});
JSONObject bodyRes = new JSONObject();
bodyRes.put("kvs", kvs);
bodyRes.put("type", MsRequestBodyType.KV.value());
requestObject.put("body", bodyRes);
}
}
}
}
}