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)) { if (CollectionUtils.isNotEmpty(headers)) {
for (KeyValue header : headers) { for (KeyValue header : headers) {
if (StringUtils.equals(header.getName(), "Content-Type") && StringUtils.equals(header.getValue(), "application/json")) { 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; jsonType = true;
break; break;
} }

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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