fix(接口定义): 修复jsonSchema预览格式未格式化的缺陷
--bug=1021690 --user=王孝刚 【接口测试】github #21168开启json-schema,后预览json数据显示不全,且数据未格式化 https://www.tapd.cn/55049933/s/1324583
This commit is contained in:
parent
429e4c4e4e
commit
230a3b3c2c
|
@ -99,7 +99,7 @@ public class Body {
|
||||||
if (StringUtils.isNotBlank(this.type) && StringUtils.equals(this.type, JSON_STR)) {
|
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(JSONUtil.toJSONString(this.getJsonSchema())));
|
||||||
} else {
|
} else {
|
||||||
try {
|
try {
|
||||||
if (StringUtils.isNotEmpty(this.getRaw())) {
|
if (StringUtils.isNotEmpty(this.getRaw())) {
|
||||||
|
@ -109,13 +109,13 @@ public class Body {
|
||||||
if (!this.getRaw().contains("$ref")) {
|
if (!this.getRaw().contains("$ref")) {
|
||||||
jsonMockParse(list);
|
jsonMockParse(list);
|
||||||
}
|
}
|
||||||
this.raw = JSONUtil.parser(JSONUtil.toJSONString(list));
|
this.raw = JSONUtil.parserArray(JSONUtil.toJSONString(list));
|
||||||
} else {
|
} else {
|
||||||
Map<String, Object> map = JSON.parseObject(this.getRaw(), Map.class);
|
Map<String, Object> map = JSON.parseObject(this.getRaw(), Map.class);
|
||||||
if (!this.getRaw().contains("$ref")) {
|
if (!this.getRaw().contains("$ref")) {
|
||||||
jsonMockParse(map);
|
jsonMockParse(map);
|
||||||
}
|
}
|
||||||
this.raw = JSONUtil.parser(JSONUtil.toJSONString(map));
|
this.raw = JSONUtil.parserObject(JSONUtil.toJSONString(map));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
|
|
@ -272,7 +272,12 @@ public class JSONSchemaGenerator {
|
||||||
if (StringUtils.isEmpty(jsonSchema)) {
|
if (StringUtils.isEmpty(jsonSchema)) {
|
||||||
return null;
|
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) {
|
} catch (Exception ex) {
|
||||||
return jsonSchema;
|
return jsonSchema;
|
||||||
}
|
}
|
||||||
|
|
|
@ -291,7 +291,12 @@ public class JSONSchemaRunTest {
|
||||||
json = json.replace(str, map.get(str));
|
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) {
|
} catch (Exception ex) {
|
||||||
return jsonSchema;
|
return jsonSchema;
|
||||||
}
|
}
|
||||||
|
|
|
@ -32,6 +32,13 @@ public class JSONUtil {
|
||||||
private static final ObjectMapper objectMapper = new ObjectMapper();
|
private static final ObjectMapper objectMapper = new ObjectMapper();
|
||||||
private static final TypeFactory typeFactory = objectMapper.getTypeFactory();
|
private static final TypeFactory typeFactory = objectMapper.getTypeFactory();
|
||||||
|
|
||||||
|
private static final Gson gson = new GsonBuilder()
|
||||||
|
.setPrettyPrinting()
|
||||||
|
.disableHtmlEscaping()
|
||||||
|
.serializeNulls()
|
||||||
|
.create();
|
||||||
|
;
|
||||||
|
|
||||||
static {
|
static {
|
||||||
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
||||||
// 自动检测所有类的全部属性
|
// 自动检测所有类的全部属性
|
||||||
|
@ -39,6 +46,7 @@ public class JSONUtil {
|
||||||
// 如果一个对象中没有任何的属性,那么在序列化的时候就会报错
|
// 如果一个对象中没有任何的属性,那么在序列化的时候就会报错
|
||||||
objectMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
|
objectMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
|
||||||
objectMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
|
objectMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static <T> T parseObject(String content, Class<T> valueType) {
|
public static <T> T parseObject(String content, Class<T> valueType) {
|
||||||
|
@ -241,19 +249,22 @@ public class JSONUtil {
|
||||||
return objectMapper.createObjectNode();
|
return objectMapper.createObjectNode();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String parser(String content) {
|
public static String parserObject(String content) {
|
||||||
try {
|
try {
|
||||||
Gson gson = new GsonBuilder()
|
|
||||||
.setPrettyPrinting()
|
|
||||||
.disableHtmlEscaping()
|
|
||||||
.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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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) {
|
public static LinkedList<MsTestElement> readValue(String content) {
|
||||||
try {
|
try {
|
||||||
return objectMapper.readValue(content, new TypeReference<LinkedList<MsTestElement>>() {
|
return objectMapper.readValue(content, new TypeReference<LinkedList<MsTestElement>>() {
|
||||||
|
|
|
@ -43,7 +43,7 @@
|
||||||
</el-tab-pane>
|
</el-tab-pane>
|
||||||
<el-tab-pane v-if="showPreview" :label="$t('schema.preview')" name="preview">
|
<el-tab-pane v-if="showPreview" :label="$t('schema.preview')" name="preview">
|
||||||
<div style="min-height: 200px">
|
<div style="min-height: 200px">
|
||||||
<pre>{{ this.preview }}</pre>
|
<pre style="width: 100%; white-space: pre-wrap;">{{ this.preview }}</pre>
|
||||||
</div>
|
</div>
|
||||||
</el-tab-pane>
|
</el-tab-pane>
|
||||||
</el-tabs>
|
</el-tabs>
|
||||||
|
|
Loading…
Reference in New Issue