fix(接口定义): 修复jsonSchema预览格式未格式化的缺陷

--bug=1021690 --user=王孝刚 【接口测试】github
#21168开启json-schema,后预览json数据显示不全,且数据未格式化
https://www.tapd.cn/55049933/s/1324583
This commit is contained in:
wxg0103 2023-01-09 17:13:45 +08:00 committed by fit2-zhao
parent 71336dda57
commit 6dcb84fc2a
5 changed files with 33 additions and 12 deletions

View File

@ -99,7 +99,7 @@ public class Body {
if (StringUtils.isNotBlank(this.type) && StringUtils.equals(this.type, JSON_STR)) {
if (StringUtils.isNotEmpty(this.format) && this.getJsonSchema() != null
&& JSON_SCHEMA.equals(this.format)) {
this.raw = StringEscapeUtils.unescapeJava(JSONSchemaRunTest.getJson(JSON.toJSONString(this.getJsonSchema())));
this.raw = StringEscapeUtils.unescapeJava(JSONSchemaRunTest.getJson(JSONUtil.toJSONString(this.getJsonSchema())));
} else {
try {
if (StringUtils.isNotEmpty(this.getRaw())) {
@ -109,13 +109,13 @@ public class Body {
if (!this.getRaw().contains("$ref")) {
jsonMockParse(list);
}
this.raw = JSONUtil.parser(JSONUtil.toJSONString(list));
this.raw = JSONUtil.parserArray(JSONUtil.toJSONString(list));
} else {
Map<String, Object> map = JSON.parseObject(this.getRaw(), Map.class);
if (!this.getRaw().contains("$ref")) {
jsonMockParse(map);
}
this.raw = JSONUtil.parser(JSONUtil.toJSONString(map));
this.raw = JSONUtil.parserObject(JSONUtil.toJSONString(map));
}
}
} catch (Exception e) {

View File

@ -272,7 +272,12 @@ public class JSONSchemaGenerator {
if (StringUtils.isEmpty(jsonSchema)) {
return null;
}
return formerJson(jsonSchema);
String value = formerJson(jsonSchema);
if (StringUtils.startsWith(value, "[") && StringUtils.endsWith(value, "]")) {
return JSONUtil.parserArray(value);
} else {
return JSONUtil.parserObject(value);
}
} catch (Exception ex) {
return jsonSchema;
}

View File

@ -291,7 +291,12 @@ public class JSONSchemaRunTest {
json = json.replace(str, map.get(str));
}
}
return JSONUtil.parser(json);
String value = StringUtils.chomp(json.trim());
if (StringUtils.startsWith(value, "[") && StringUtils.endsWith(value, "]")) {
return JSONUtil.parserArray(value);
} else {
return JSONUtil.parserObject(value);
}
} catch (Exception ex) {
return jsonSchema;
}

View File

@ -32,6 +32,13 @@ public class JSONUtil {
private static final ObjectMapper objectMapper = new ObjectMapper();
private static final TypeFactory typeFactory = objectMapper.getTypeFactory();
private static final Gson gson = new GsonBuilder()
.setPrettyPrinting()
.disableHtmlEscaping()
.serializeNulls()
.create();
;
static {
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
// 自动检测所有类的全部属性
@ -39,6 +46,7 @@ public class JSONUtil {
// 如果一个对象中没有任何的属性那么在序列化的时候就会报错
objectMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
objectMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
}
public static <T> T parseObject(String content, Class<T> valueType) {
@ -241,19 +249,22 @@ public class JSONUtil {
return objectMapper.createObjectNode();
}
public static String parser(String content) {
public static String parserObject(String content) {
try {
Gson gson = new GsonBuilder()
.setPrettyPrinting()
.disableHtmlEscaping()
.serializeNulls()
.create();
return gson.toJson(JsonParser.parseString(content).getAsJsonObject());
} catch (Exception e) {
return content;
}
}
public static String parserArray(String content) {
try {
return gson.toJson(JsonParser.parseString(content).getAsJsonArray());
} catch (Exception e) {
return content;
}
}
public static LinkedList<MsTestElement> readValue(String content) {
try {
return objectMapper.readValue(content, new TypeReference<LinkedList<MsTestElement>>() {

View File

@ -43,7 +43,7 @@
</el-tab-pane>
<el-tab-pane v-if="showPreview" :label="$t('schema.preview')" name="preview">
<div style="min-height: 200px">
<pre>{{ this.preview }}</pre>
<pre style="width: 100%; white-space: pre-wrap;">{{ this.preview }}</pre>
</div>
</el-tab-pane>
</el-tabs>