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,7 +34,7 @@ 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() {
@ -90,13 +92,18 @@ public class Body {
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);
}
} }
} }
} }
@ -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

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

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