diff --git a/knife4j-vue/src/core/Knife4jAsync.js b/knife4j-vue/src/core/Knife4jAsync.js index d91f25621070d38b7769eafdd91052beb60622ac..7e9cbde61028e7e5bcfe26def9f8910cc44755f0 100644 --- a/knife4j-vue/src/core/Knife4jAsync.js +++ b/knife4j-vue/src/core/Knife4jAsync.js @@ -3664,6 +3664,84 @@ SwaggerBootstrapUi.prototype.analysisAllOfOAS2 = function (allOf) { return newModelName; } +/** + * analysisAllOfOAS3 解析allOf OAS3 + * @param {*} allOf + * @returns {string} 返回新的模型名 + */ +SwaggerBootstrapUi.prototype.analysisAllOfOAS3 = function (allOf) { + var that = this; + // 原对象 + const originalRef = allOf[0].$ref; + const regex = new RegExp('#/components/schemas/(.*)$', 'ig'); + const originalObjNameMatch = regex.exec(originalRef); + if (!originalObjNameMatch) { + that.error("Unable to parse original object name from " + originalRef); + return; + } + const originalObjName = originalObjNameMatch[1]; + const menu = that.currentInstance.swaggerData; + const componentsSchemas = menu.components.schemas; + const originalObjCopy = JSON.parse(JSON.stringify(componentsSchemas[originalObjName])); + // 需要修补的属性 + const patchProperties = allOf[1].properties; + // 合并 + originalObjCopy.properties = Object.assign(originalObjCopy.properties, patchProperties); + // 新的模型名 + const patchPropertiesNames = [] + for (const key in patchProperties) { + if (patchProperties.hasOwnProperty(key)) { + const element = patchProperties[key]; + // 一般对象 + if (element.hasOwnProperty("$ref")) { + const regex = new RegExp('#/components/schemas/(.*)$', 'ig'); + const elementObjNameMatch = regex.exec(element.$ref); + if (!elementObjNameMatch) { + that.error("Unable to parse element object name from " + element.$ref); + return; + } + const elementObjName = elementObjNameMatch[1]; + patchPropertiesNames.push(elementObjName); + } + // 数组 + if (element.hasOwnProperty("items")) { + const regex = new RegExp('#/components/schemas/(.*)$', 'ig'); + const elementObjNameMatch = regex.exec(element.items.$ref); + if (!elementObjNameMatch) { + that.error("Unable to parse element object name from " + element.items.$ref); + return; + } + const elementObjName = elementObjNameMatch[1]; + patchPropertiesNames.push("[]" + elementObjName); + } + // 基本类型 + if (element.hasOwnProperty("type" && element.type != "array")) { + patchPropertiesNames.push(element.type); + } + } + } + const patchPropertiesName = patchPropertiesNames.join(','); + const newModelName = `${originalObjName}<${patchPropertiesName}>`; + // 检查componentsSchemas是否已有 + if (componentsSchemas[newModelName]) + return newModelName; + + // 放入原始componentsSchemas + componentsSchemas[newModelName] = originalObjCopy; + // 放入difarrs对象中 + const swud = new SwaggerBootstrapUiDefinition(); + swud.name = newModelName; + swud.ignoreFilterName = newModelName; + that.currentInstance.difArrs.push(swud); + // 所有类classModel的treeTable参数 + const swudTree = new SwaggerBootstrapUiTreeTableRefParameter(); + swudTree.name = newModelName; + swudTree.id = md5(newModelName); + // 存放值 + that.currentInstance.swaggerTreeTableModels[newModelName] = swudTree; + return newModelName; +} + /** * 解析oas2的接口 @@ -4592,6 +4670,18 @@ SwaggerBootstrapUi.prototype.initApiInfoAsyncOAS3 = function (swpinfo) { } } } + } else if (schema.hasOwnProperty('allOf')) { + // allOf 类型 + const allOf = schema['allOf']; + const newName = that.analysisAllOfOAS3(allOf); + if (newName) { + // 重新赋值 + rptype = newName; + swpinfo.responseParameterRefName = rptype; + swaggerResp.responseParameterRefName = rptype; + definitionType = rptype; + swaggerResp.schema = rptype; + } } } else {