refactor(接口测试): 优化大文件JSON转Schema格式卡顿现象

--bug=1011847 --user=赵勇 【接口测试】json太大,导入到接口定义和作为请求体发送会报错 https://www.tapd.cn/55049933/s/1128606
This commit is contained in:
fit2-zhao 2022-04-01 10:59:30 +08:00 committed by fit2-zhao
parent d18b3eaf3a
commit 8c35d73a39
10 changed files with 39 additions and 27 deletions

View File

@ -723,7 +723,7 @@ public class JmeterDefinitionParser extends ApiImportAbstractParser<ApiDefinitio
if (CollectionUtils.isNotEmpty(headers)) {
for (KeyValue header : headers) {
if (StringUtils.equals(header.getName(), "Content-Type") && StringUtils.equals(header.getValue(), "application/json")) {
samplerProxy.getBody().setType(Body.JSON);
samplerProxy.getBody().setType(Body.JSON_STR);
jsonType = true;
break;
}

View File

@ -204,7 +204,7 @@ public class Swagger2Parser extends SwaggerAbstractParser {
private String getBodyType(Operation operation) {
if (CollectionUtils.isEmpty(operation.getConsumes())) {
return Body.JSON;
return Body.JSON_STR;
}
String contentType = operation.getConsumes().get(0);
return getBodyType(contentType);
@ -212,7 +212,7 @@ public class Swagger2Parser extends SwaggerAbstractParser {
private String getResponseBodyType(Operation operation) {
if (CollectionUtils.isEmpty(operation.getProduces())) {
return Body.JSON;
return Body.JSON_STR;
}
String contentType = operation.getProduces().get(0);
return getBodyType(contentType);
@ -355,7 +355,7 @@ public class Swagger2Parser extends SwaggerAbstractParser {
private void parseRequestBodyParameters(Parameter parameter, Body body) {
BodyParameter bodyParameter = (BodyParameter) parameter;
if (body.getType().equals(Body.JSON)) {
if (body.getType().equals(Body.JSON_STR)) {
body.setJsonSchema(parseSchema2JsonSchema(bodyParameter.getSchema()));
body.setFormat("JSON-SCHEMA");
} else {

View File

@ -1,11 +1,13 @@
package io.metersphere.api.dto.scenario;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import io.metersphere.api.dto.scenario.request.BodyFile;
import io.metersphere.commons.json.JSONSchemaRunTest;
import io.metersphere.commons.utils.FileUtils;
import io.metersphere.jmeter.utils.ScriptEngineUtils;
import io.metersphere.utils.LoggerUtil;
import lombok.Data;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
@ -32,12 +34,12 @@ public class Body {
public final static String WWW_FROM = "WWW_FORM";
public final static String RAW = "Raw";
public final static String BINARY = "BINARY";
public final static String JSON = "JSON";
public final static String JSON_STR = "JSON";
public final static String XML = "XML";
public boolean isValid() {
if (this.isKV()) {
if(kvs != null){
if (kvs != null) {
return kvs.stream().anyMatch(KeyValue::isValid);
}
} else {
@ -74,7 +76,7 @@ public class Body {
sampler.setDoMultipart(true);
}
} else {
if(StringUtils.isNotEmpty(this.getRaw()) || this.getJsonSchema()!= null ) {
if (StringUtils.isNotEmpty(this.getRaw()) || this.getJsonSchema() != null) {
parseJonBodyMock();
KeyValue keyValue = new KeyValue("", "JSON-SCHEMA", this.getRaw(), true, true);
sampler.setPostBodyRaw(true);
@ -88,25 +90,30 @@ public class Body {
private void parseJonBodyMock() {
if (StringUtils.isNotBlank(this.type) && StringUtils.equals(this.type, "JSON")) {
if(StringUtils.isNotEmpty(this.format) && this.getJsonSchema() != null
if (StringUtils.isNotEmpty(this.format) && this.getJsonSchema() != null
&& "JSON-SCHEMA".equals(this.format)) {
this.raw = JSONSchemaRunTest.getJson(com.alibaba.fastjson.JSON.toJSONString(this.getJsonSchema()));
} else { // json 文本也支持 mock 参数
this.raw = JSONSchemaRunTest.getJson(JSON.toJSONString(this.getJsonSchema()));
} else {
try {
JSONObject jsonObject = com.alibaba.fastjson.JSON.parseObject(this.getRaw());
jsonMockParse(jsonObject);
this.raw = JSONObject.toJSONString(jsonObject, SerializerFeature.WriteMapNullValue);
} catch (Exception e) {}
if (StringUtils.isNotEmpty(this.getRaw())) {
String jsonString = JSON.toJSONString(this.getRaw(), SerializerFeature.DisableCircularReferenceDetect);
JSONObject jsonObject = JSON.parseObject(jsonString);
jsonMockParse(jsonObject);
this.raw = JSONObject.toJSONString(jsonObject, SerializerFeature.WriteMapNullValue);
}
} catch (Exception e) {
LoggerUtil.error("json mock value is abnormal", e);
}
}
}
}
private void jsonMockParse(JSONObject jsonObject) {
for(String key : jsonObject.keySet()) {
for (String key : jsonObject.keySet()) {
Object value = jsonObject.get(key);
if(value instanceof JSONObject) {
if (value instanceof JSONObject) {
jsonMockParse((JSONObject) value);
} else if(value instanceof String) {
} else if (value instanceof String) {
if (StringUtils.isNotBlank((String) value)) {
value = ScriptEngineUtils.buildFunctionCallString((String) value);
}
@ -157,7 +164,7 @@ public class Body {
}
public boolean isJson() {
return StringUtils.equals(type, JSON);
return StringUtils.equals(type, JSON_STR);
}
public boolean isXml() {

View File

@ -75,7 +75,7 @@ public abstract class ApiImportAbstractParser<T> implements ApiImportParser<T> {
bodyType = Body.FORM_DATA;
break;
case "application/json":
bodyType = Body.JSON;
bodyType = Body.JSON_STR;
break;
case "application/xml":
bodyType = Body.XML;
@ -96,7 +96,7 @@ public abstract class ApiImportAbstractParser<T> implements ApiImportParser<T> {
case Body.WWW_FROM:
contentType = "application/x-www-form-urlencoded";
break;
case Body.JSON:
case Body.JSON_STR:
contentType = "application/json";
break;
case Body.XML:

View File

@ -200,7 +200,7 @@ public abstract class HarScenarioAbstractParser<T> extends ApiImportAbstractPars
String bodyType = "";
switch (raw.getString("language")) {
case "json":
bodyType = Body.JSON;
bodyType = Body.JSON_STR;
break;
case "xml":
bodyType = Body.XML;

View File

@ -155,7 +155,7 @@ public abstract class PostmanAbstractParserParser<T> extends ApiImportAbstractPa
String bodyType = "";
switch (raw.getString("language")) {
case "json":
bodyType = Body.JSON;
bodyType = Body.JSON_STR;
break;
case "xml":
bodyType = Body.XML;

View File

@ -131,7 +131,7 @@ public class HistoricalDataUpgradeService {
}
if ("json".equals(request1.getBody().getFormat())) {
if ("Raw".equals(request1.getBody().getType())) {
request1.getBody().setType(Body.JSON);
request1.getBody().setType(Body.JSON_STR);
if (CollectionUtils.isEmpty(request1.getHeaders())) {
List<KeyValue> headers = new LinkedList<>();
headers.add(new KeyValue("Content-Type", "application/json"));

View File

@ -127,7 +127,7 @@ class Convert {
if (isArray(elementItem)) {
let innerItemArr = this._deepTraversal(elementItem, `${$id}/items`, key + 'items');
itemArr.push(innerItemArr);
}else {
} else {
//item不是Array进行统一处理
let item = this._value2object(elementItem, `${$id}/items`, key + 'items');
item = Object.assign(item, this._json2schema(elementItem, `${$id}/items`));
@ -206,6 +206,7 @@ class Convert {
let objectTemplate = {
$id: $id,
title: `The ${key} Schema`,
hidden: true,
mock: {
"mock": value
},

View File

@ -94,7 +94,6 @@
return;
}
let jsonData = MsConvert.format(json5.parse(this.json));
//let jsonData = GenerateSchema(json5.parse(this.json));
this.$emit('jsonData', jsonData);
} else {
if (!this.checkIsJsonSchema(this.jsonSchema)) {

View File

@ -3,7 +3,7 @@
<el-row class="row" :gutter="20">
<el-col :span="8" class="ms-col-name">
<div :style="{marginLeft:`${10*deep}px`}" class="ms-col-name-c"/>
<span v-if="pickValue.type==='object'" :class="hidden? 'el-icon-caret-left ms-transform':
<span v-if="pickValue.type==='object'" :class="hidden ? 'el-icon-caret-left ms-transform':
'el-icon-caret-bottom'" @click="hidden = !hidden"/>
<span v-else style="width:10px;display:inline-block"></span>
<input class="el-input el-input__inner" style="height: 32px" :disabled="disabled || root" :value="pickKey" @blur="onInputName" size="small"/>
@ -171,7 +171,7 @@ export default {
advanced() {
return TYPE[this.pickValue.type]
},
types(){
types() {
return TYPES(this.pickKey);
},
advancedAttr() {
@ -205,6 +205,11 @@ export default {
customing: false
}
},
created() {
if (this.pickValue) {
this.hidden = this.pickValue.hidden;
}
},
methods: {
onInputName(e) {
const val = e.target.value