diff --git a/api-test/backend/src/main/java/io/metersphere/api/dto/scenario/Body.java b/api-test/backend/src/main/java/io/metersphere/api/dto/scenario/Body.java index 8b1c96413d..f37f53fc8e 100644 --- a/api-test/backend/src/main/java/io/metersphere/api/dto/scenario/Body.java +++ b/api-test/backend/src/main/java/io/metersphere/api/dto/scenario/Body.java @@ -4,9 +4,9 @@ import io.metersphere.api.exec.generator.JSONSchemaRunTest; import io.metersphere.commons.constants.StorageConstants; import io.metersphere.commons.utils.FileUtils; import io.metersphere.commons.utils.JSON; +import io.metersphere.commons.utils.JSONUtil; import io.metersphere.jmeter.utils.ScriptEngineUtils; import io.metersphere.request.BodyFile; -import io.metersphere.commons.utils.JSONUtil; import io.metersphere.utils.LoggerUtil; import lombok.Data; import org.apache.commons.collections4.CollectionUtils; @@ -20,6 +20,7 @@ import org.json.JSONObject; import java.util.ArrayList; import java.util.List; +import java.util.Map; import java.util.stream.Collectors; @Data @@ -38,6 +39,7 @@ public class Body { public final static String BINARY = "BINARY"; public final static String JSON_STR = "JSON"; public final static String XML = "XML"; + public final static String JSON_SCHEMA = "JSON-SCHEMA"; public boolean isValid() { if (this.isKV()) { @@ -82,7 +84,7 @@ public class Body { } else { if (StringUtils.isNotEmpty(this.getRaw()) || this.getJsonSchema() != null) { 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); keyValue.setEnable(true); keyValue.setUrlEncode(false); @@ -93,18 +95,18 @@ public class Body { } 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 - && "JSON-SCHEMA".equals(this.format)) { + && JSON_SCHEMA.equals(this.format)) { this.raw = StringEscapeUtils.unescapeJava(JSONSchemaRunTest.getJson(JSON.toJSONString(this.getJsonSchema()))); } else { try { if (StringUtils.isNotEmpty(this.getRaw())) { - JSONObject jsonObject = JSONUtil.parseObject(this.getRaw()); + Map map = JSON.parseObject(this.getRaw(), Map.class); if (!this.getRaw().contains("$ref")) { - jsonMockParse(jsonObject); + jsonMockParse(map); } - this.raw = JSONUtil.parser(jsonObject.toString()); + this.raw = JSONUtil.parser(map.toString()); } } catch (Exception e) { LoggerUtil.error("json mock value is abnormal", e); @@ -113,16 +115,16 @@ public class Body { } } - private void jsonMockParse(JSONObject jsonObject) { - for (String key : jsonObject.keySet()) { - Object value = jsonObject.get(key); + private void jsonMockParse(Map map) { + for (Object key : map.keySet()) { + Object value = map.get(key); if (value instanceof JSONObject) { - jsonMockParse((JSONObject) value); + jsonMockParse((Map) value); } else if (value instanceof String) { if (StringUtils.isNotBlank((String) value)) { value = ScriptEngineUtils.buildFunctionCallString((String) value); } - jsonObject.put(key, value); + map.put(key, value); } } } diff --git a/api-test/backend/src/main/java/io/metersphere/api/exec/generator/JSONSchemaGenerator.java b/api-test/backend/src/main/java/io/metersphere/api/exec/generator/JSONSchemaGenerator.java index 80a9c14afa..d1feffe545 100644 --- a/api-test/backend/src/main/java/io/metersphere/api/exec/generator/JSONSchemaGenerator.java +++ b/api-test/backend/src/main/java/io/metersphere/api/exec/generator/JSONSchemaGenerator.java @@ -241,7 +241,7 @@ public class JSONSchemaGenerator { concept.put(propertyName, obj); analyzeObject(object, obj); } else if (StringUtils.equalsIgnoreCase(propertyObjType, "null")) { - concept.put(propertyName, StringUtils.EMPTY); + concept.put(propertyName, JSONObject.NULL); } } } diff --git a/api-test/backend/src/main/java/io/metersphere/api/exec/generator/JSONSchemaRunTest.java b/api-test/backend/src/main/java/io/metersphere/api/exec/generator/JSONSchemaRunTest.java index 519759f689..6770dfa4fc 100644 --- a/api-test/backend/src/main/java/io/metersphere/api/exec/generator/JSONSchemaRunTest.java +++ b/api-test/backend/src/main/java/io/metersphere/api/exec/generator/JSONSchemaRunTest.java @@ -240,7 +240,7 @@ public class JSONSchemaRunTest { concept.put(propertyName, obj); analyzeObject(object, obj, map); } else if (StringUtils.equalsIgnoreCase(propertyObjType, "null")) { - concept.put(propertyName, StringUtils.EMPTY); + concept.put(propertyName, JSONObject.NULL); } } diff --git a/api-test/backend/src/main/java/io/metersphere/commons/utils/JSONUtil.java b/api-test/backend/src/main/java/io/metersphere/commons/utils/JSONUtil.java index b153e7d5f4..47b764e34f 100644 --- a/api-test/backend/src/main/java/io/metersphere/commons/utils/JSONUtil.java +++ b/api-test/backend/src/main/java/io/metersphere/commons/utils/JSONUtil.java @@ -243,7 +243,7 @@ public class JSONUtil { public static String parser(String content) { try { - Gson gson = new GsonBuilder().setPrettyPrinting().create(); + Gson gson = new GsonBuilder().setPrettyPrinting().serializeNulls().create(); return gson.toJson(JsonParser.parseString(content).getAsJsonObject()); } catch (Exception e) { return content; diff --git a/api-test/frontend/src/business/commons/json-schema/convert/convert.js b/api-test/frontend/src/business/commons/json-schema/convert/convert.js index a35546c8cf..c68e277cee 100644 --- a/api-test/frontend/src/business/commons/json-schema/convert/convert.js +++ b/api-test/frontend/src/business/commons/json-schema/convert/convert.js @@ -230,8 +230,8 @@ class Convert { } else if (isString(value)) { objectTemplate.type = 'string'; } else if (isNull(value)) { - objectTemplate.type = 'string'; - objectTemplate.mock = { mock: '' }; + objectTemplate.type = 'null'; + objectTemplate['mock'] = undefined; } else if (isArray(value)) { objectTemplate.type = 'array'; objectTemplate['mock'] = undefined; diff --git a/api-test/frontend/src/business/commons/json-schema/schema/editor/main.vue b/api-test/frontend/src/business/commons/json-schema/schema/editor/main.vue index 3c2e34f64c..3304fc2bb3 100644 --- a/api-test/frontend/src/business/commons/json-schema/schema/editor/main.vue +++ b/api-test/frontend/src/business/commons/json-schema/schema/editor/main.vue @@ -475,6 +475,10 @@ export default { if (this.isArray(this.pickValue)) { this.$set(this.pickValue, 'items', [{ type: 'string', mock: { mock: '' } }]); } + if (this.pickValue.type === 'null') { + this.$set(this.pickValue, 'mock', { mock: '' }); + this.reloadItems(); + } } }, changeAllItemsType(changeType) { diff --git a/api-test/frontend/src/business/commons/json-schema/schema/editor/type/type.js b/api-test/frontend/src/business/commons/json-schema/schema/editor/type/type.js index f71c6a86ab..4530678a50 100644 --- a/api-test/frontend/src/business/commons/json-schema/schema/editor/type/type.js +++ b/api-test/frontend/src/business/commons/json-schema/schema/editor/type/type.js @@ -5,7 +5,7 @@ import _boolean from './boolean'; import _integer from './integer'; 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 = { object: _object, @@ -14,6 +14,7 @@ const TYPE = { boolean: _boolean, integer: _integer, number: _number, + null: { description: null }, }; export { TYPE, TYPE_NAME };