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 (object.has("items")) {
if (itemsObject.has("mock") && itemsObject.get("mock").getAsJsonObject() != null && StringUtils.isNotEmpty(itemsObject.get("mock").getAsJsonObject().get("mock").getAsString())) { if (itemsObject.has("mock") && itemsObject.get("mock").getAsJsonObject() != null && StringUtils.isNotEmpty(itemsObject.get("mock").getAsJsonObject().get("mock").getAsString())) {
try { try {
if(itemsObject.has("type") && itemsObject.get("type").getAsString().equals("integer")){
int value = itemsObject.get("mock").getAsJsonObject().get("mock").getAsInt(); int value = itemsObject.get("mock").getAsJsonObject().get("mock").getAsInt();
array.add(value); array.add(value);
}else {
String value = ScriptEngineUtils.buildFunctionCallString(itemsObject.get("mock").getAsJsonObject().get("mock").getAsString());
array.add(value);
}
} catch (Exception e) { } catch (Exception e) {
String value = ScriptEngineUtils.buildFunctionCallString(itemsObject.get("mock").getAsJsonObject().get("mock").getAsString()); String value = ScriptEngineUtils.buildFunctionCallString(itemsObject.get("mock").getAsJsonObject().get("mock").getAsString());
array.add(value); array.add(value);

View File

@ -124,14 +124,20 @@ class Convert {
for (let index = 0; index < element.length; index++) { for (let index = 0; index < element.length; index++) {
let elementItem = element[index]; let elementItem = element[index];
// 创建items对象的基本信息 // 创建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'); let item = this._value2object(elementItem, `${$id}/items`, key + 'items');
// 判断第一项是否是对象,且对象属性不为空 // 判断第一项是否是对象,且对象属性不为空
if (isObject(elementItem) && !isEmpty(elementItem)) {
// 新增的properties才合并进来 // 新增的properties才合并进来
item = Object.assign(item, this._json2schema(elementItem, `${$id}/items`)); item = Object.assign(item, this._json2schema(elementItem, `${$id}/items`));
}
itemArr.push(item); itemArr.push(item);
} }
}
result["properties"][key]["items"] = itemArr; result["properties"][key]["items"] = itemArr;
} }
} else { } else {
@ -149,6 +155,51 @@ class Convert {
return result; 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的值转换成对象类型 * 把json的值转换成对象类型
* @param {*} value * @param {*} value

View File

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