feat(接口测试): json结构化支持传null值
--story=1010600 --user=王孝刚 【接口测试】json结构化的时候会提供null类型来支持传null值 https://www.tapd.cn/55049933/s/1313490
This commit is contained in:
parent
fabaffad25
commit
f03dbd20a1
|
@ -4,9 +4,9 @@ import io.metersphere.api.exec.generator.JSONSchemaRunTest;
|
||||||
import io.metersphere.commons.constants.StorageConstants;
|
import io.metersphere.commons.constants.StorageConstants;
|
||||||
import io.metersphere.commons.utils.FileUtils;
|
import io.metersphere.commons.utils.FileUtils;
|
||||||
import io.metersphere.commons.utils.JSON;
|
import io.metersphere.commons.utils.JSON;
|
||||||
|
import io.metersphere.commons.utils.JSONUtil;
|
||||||
import io.metersphere.jmeter.utils.ScriptEngineUtils;
|
import io.metersphere.jmeter.utils.ScriptEngineUtils;
|
||||||
import io.metersphere.request.BodyFile;
|
import io.metersphere.request.BodyFile;
|
||||||
import io.metersphere.commons.utils.JSONUtil;
|
|
||||||
import io.metersphere.utils.LoggerUtil;
|
import io.metersphere.utils.LoggerUtil;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import org.apache.commons.collections4.CollectionUtils;
|
import org.apache.commons.collections4.CollectionUtils;
|
||||||
|
@ -20,6 +20,7 @@ import org.json.JSONObject;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
|
@ -38,6 +39,7 @@ public class Body {
|
||||||
public final static String BINARY = "BINARY";
|
public final static String BINARY = "BINARY";
|
||||||
public final static String JSON_STR = "JSON";
|
public final static String JSON_STR = "JSON";
|
||||||
public final static String XML = "XML";
|
public final static String XML = "XML";
|
||||||
|
public final static String JSON_SCHEMA = "JSON-SCHEMA";
|
||||||
|
|
||||||
public boolean isValid() {
|
public boolean isValid() {
|
||||||
if (this.isKV()) {
|
if (this.isKV()) {
|
||||||
|
@ -82,7 +84,7 @@ public class Body {
|
||||||
} else {
|
} else {
|
||||||
if (StringUtils.isNotEmpty(this.getRaw()) || this.getJsonSchema() != null) {
|
if (StringUtils.isNotEmpty(this.getRaw()) || this.getJsonSchema() != null) {
|
||||||
parseJonBodyMock();
|
parseJonBodyMock();
|
||||||
KeyValue keyValue = new KeyValue(StringUtils.EMPTY, "JSON-SCHEMA", this.getRaw(), true, true);
|
KeyValue keyValue = new KeyValue(StringUtils.EMPTY, JSON_SCHEMA, this.getRaw(), true, true);
|
||||||
sampler.setPostBodyRaw(true);
|
sampler.setPostBodyRaw(true);
|
||||||
keyValue.setEnable(true);
|
keyValue.setEnable(true);
|
||||||
keyValue.setUrlEncode(false);
|
keyValue.setUrlEncode(false);
|
||||||
|
@ -93,18 +95,18 @@ 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_STR)) {
|
||||||
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 = StringEscapeUtils.unescapeJava(JSONSchemaRunTest.getJson(JSON.toJSONString(this.getJsonSchema())));
|
this.raw = StringEscapeUtils.unescapeJava(JSONSchemaRunTest.getJson(JSON.toJSONString(this.getJsonSchema())));
|
||||||
} else {
|
} else {
|
||||||
try {
|
try {
|
||||||
if (StringUtils.isNotEmpty(this.getRaw())) {
|
if (StringUtils.isNotEmpty(this.getRaw())) {
|
||||||
JSONObject jsonObject = JSONUtil.parseObject(this.getRaw());
|
Map<String, Object> map = JSON.parseObject(this.getRaw(), Map.class);
|
||||||
if (!this.getRaw().contains("$ref")) {
|
if (!this.getRaw().contains("$ref")) {
|
||||||
jsonMockParse(jsonObject);
|
jsonMockParse(map);
|
||||||
}
|
}
|
||||||
this.raw = JSONUtil.parser(jsonObject.toString());
|
this.raw = JSONUtil.parser(map.toString());
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
LoggerUtil.error("json mock value is abnormal", e);
|
LoggerUtil.error("json mock value is abnormal", e);
|
||||||
|
@ -113,16 +115,16 @@ public class Body {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void jsonMockParse(JSONObject jsonObject) {
|
private void jsonMockParse(Map map) {
|
||||||
for (String key : jsonObject.keySet()) {
|
for (Object key : map.keySet()) {
|
||||||
Object value = jsonObject.get(key);
|
Object value = map.get(key);
|
||||||
if (value instanceof JSONObject) {
|
if (value instanceof JSONObject) {
|
||||||
jsonMockParse((JSONObject) value);
|
jsonMockParse((Map) 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);
|
||||||
}
|
}
|
||||||
jsonObject.put(key, value);
|
map.put(key, value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -241,7 +241,7 @@ public class JSONSchemaGenerator {
|
||||||
concept.put(propertyName, obj);
|
concept.put(propertyName, obj);
|
||||||
analyzeObject(object, obj);
|
analyzeObject(object, obj);
|
||||||
} else if (StringUtils.equalsIgnoreCase(propertyObjType, "null")) {
|
} else if (StringUtils.equalsIgnoreCase(propertyObjType, "null")) {
|
||||||
concept.put(propertyName, StringUtils.EMPTY);
|
concept.put(propertyName, JSONObject.NULL);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -240,7 +240,7 @@ public class JSONSchemaRunTest {
|
||||||
concept.put(propertyName, obj);
|
concept.put(propertyName, obj);
|
||||||
analyzeObject(object, obj, map);
|
analyzeObject(object, obj, map);
|
||||||
} else if (StringUtils.equalsIgnoreCase(propertyObjType, "null")) {
|
} else if (StringUtils.equalsIgnoreCase(propertyObjType, "null")) {
|
||||||
concept.put(propertyName, StringUtils.EMPTY);
|
concept.put(propertyName, JSONObject.NULL);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -243,7 +243,7 @@ public class JSONUtil {
|
||||||
|
|
||||||
public static String parser(String content) {
|
public static String parser(String content) {
|
||||||
try {
|
try {
|
||||||
Gson gson = new GsonBuilder().setPrettyPrinting().create();
|
Gson gson = new GsonBuilder().setPrettyPrinting().serializeNulls().create();
|
||||||
return gson.toJson(JsonParser.parseString(content).getAsJsonObject());
|
return gson.toJson(JsonParser.parseString(content).getAsJsonObject());
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
return content;
|
return content;
|
||||||
|
|
|
@ -230,8 +230,8 @@ class Convert {
|
||||||
} else if (isString(value)) {
|
} else if (isString(value)) {
|
||||||
objectTemplate.type = 'string';
|
objectTemplate.type = 'string';
|
||||||
} else if (isNull(value)) {
|
} else if (isNull(value)) {
|
||||||
objectTemplate.type = 'string';
|
objectTemplate.type = 'null';
|
||||||
objectTemplate.mock = { mock: '' };
|
objectTemplate['mock'] = undefined;
|
||||||
} else if (isArray(value)) {
|
} else if (isArray(value)) {
|
||||||
objectTemplate.type = 'array';
|
objectTemplate.type = 'array';
|
||||||
objectTemplate['mock'] = undefined;
|
objectTemplate['mock'] = undefined;
|
||||||
|
|
|
@ -475,6 +475,10 @@ export default {
|
||||||
if (this.isArray(this.pickValue)) {
|
if (this.isArray(this.pickValue)) {
|
||||||
this.$set(this.pickValue, 'items', [{ type: 'string', mock: { mock: '' } }]);
|
this.$set(this.pickValue, 'items', [{ type: 'string', mock: { mock: '' } }]);
|
||||||
}
|
}
|
||||||
|
if (this.pickValue.type === 'null') {
|
||||||
|
this.$set(this.pickValue, 'mock', { mock: '' });
|
||||||
|
this.reloadItems();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
changeAllItemsType(changeType) {
|
changeAllItemsType(changeType) {
|
||||||
|
|
|
@ -5,7 +5,7 @@ import _boolean from './boolean';
|
||||||
import _integer from './integer';
|
import _integer from './integer';
|
||||||
import _number from './number';
|
import _number from './number';
|
||||||
|
|
||||||
const TYPE_NAME = ['string', 'number', 'integer', 'object', 'array', 'boolean'];
|
const TYPE_NAME = ['string', 'number', 'integer', 'object', 'array', 'boolean', 'null'];
|
||||||
|
|
||||||
const TYPE = {
|
const TYPE = {
|
||||||
object: _object,
|
object: _object,
|
||||||
|
@ -14,6 +14,7 @@ const TYPE = {
|
||||||
boolean: _boolean,
|
boolean: _boolean,
|
||||||
integer: _integer,
|
integer: _integer,
|
||||||
number: _number,
|
number: _number,
|
||||||
|
null: { description: null },
|
||||||
};
|
};
|
||||||
export { TYPE, TYPE_NAME };
|
export { TYPE, TYPE_NAME };
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue