fix(JSON-SCHEMA): 修复JSON-SCHEMA多层数组下转换错误

--bug=1009239 --user=宋天阳 [github #8832]接口参数 JSON-SCHEMA 转换失败
https://www.tapd.cn/55049933/s/1090723
This commit is contained in:
song-tianyang 2022-01-11 19:53:58 +08:00 committed by 刘瑞斌
parent 6e64e57e3a
commit 474658cbf6
3 changed files with 64 additions and 7 deletions

View File

@ -186,8 +186,13 @@ public class JSONSchemaGenerator {
if (object.has("items")) {
if (itemsObject.has("mock") && itemsObject.get("mock").getAsJsonObject() != null && StringUtils.isNotEmpty(itemsObject.get("mock").getAsJsonObject().get("mock").getAsString())) {
try {
if(itemsObject.has("type") && itemsObject.get("type").getAsString().equals("integer")){
int value = itemsObject.get("mock").getAsJsonObject().get("mock").getAsInt();
array.add(value);
}else {
String value = ScriptEngineUtils.buildFunctionCallString(itemsObject.get("mock").getAsJsonObject().get("mock").getAsString());
array.add(value);
}
} catch (Exception e) {
String value = ScriptEngineUtils.buildFunctionCallString(itemsObject.get("mock").getAsJsonObject().get("mock").getAsString());
array.add(value);

View File

@ -110,7 +110,7 @@ class Convert {
}
let $id = `${name}/properties/${key}`
// 判断当前 element 的值 是否也是对象如果是就继续递归不是就赋值给result
if(!result["properties"]){
if (!result["properties"]) {
continue;
}
if (isObject(element)) {
@ -124,14 +124,20 @@ class Convert {
for (let index = 0; index < element.length; index++) {
let elementItem = element[index];
// 创建items对象的基本信息
if (isArray(elementItem)) {
let innerItemArr = this._deepTraversal(elementItem, `${$id}/items`, key + 'items');
// let item = this._value2object(element, `${$id}/items`, key + 'items');
// item["items"] = innerItemArr;
itemArr.push(innerItemArr);
} else if (isObject(elementItem) && !isEmpty(elementItem)) {
let item = this._value2object(elementItem, `${$id}/items`, key + 'items');
// 判断第一项是否是对象,且对象属性不为空
if (isObject(elementItem) && !isEmpty(elementItem)) {
// 新增的properties才合并进来
item = Object.assign(item, this._json2schema(elementItem, `${$id}/items`));
}
itemArr.push(item);
}
}
result["properties"][key]["items"] = itemArr;
}
} else {
@ -149,6 +155,51 @@ class Convert {
return result;
}
/**
* 深度遍历array中的元素
* @private
*/
_deepTraversal(element, name = "", key) {
// 处理当前路径$id
if (name === "" || name == undefined) {
name = "#"
}
let $id = `${name}/`;
let innerItemArr = [];
let innerIsObject = false;
let innerIsArray = false;
element.forEach(f => {
if (isArray(f)) {
innerIsArray = true;
innerIsObject = true;
} else if (isObject(f)) {
innerIsObject = true;
}
});
if (innerIsArray) {
element.forEach(f => {
let innerArr = this._deepTraversal(f);
innerItemArr.push(innerArr);
});
} else if (innerIsObject) {
let item = this._value2object(element, `${$id}/items`, key + 'items');
// 判断第一项是否是对象,且对象属性不为空
// 新增的properties才合并进来
item = Object.assign(item, this._json2schema(element, `${$id}/items`));
innerItemArr.push(item);
} else {
element.forEach(f => {
let innerItem = this._value2object(f, `${$id}/items`, key + 'items');
innerItemArr.push(innerItem);
});
}
let item = this._value2object(element, `${$id}/items`, key + 'items');
item["items"] = innerItemArr;
return item;
}
/**
* 把json的值转换成对象类型
* @param {*} value

View File

@ -46,6 +46,7 @@
if (this.schema.type === 'object') {
this.$delete(this.schema, 'mock')
}
this.mock.mock = this.mock.mock + "";
},
watch: {
schema: {