From 6227d856bf234a7fa28bb3d7d095638db81f5b74 Mon Sep 17 00:00:00 2001 From: hyc Date: Tue, 18 Jun 2024 10:34:43 +0800 Subject: [PATCH] =?UTF-8?q?=20feat:=20=E6=B7=BB=E5=8A=A0=E5=AF=B9OAS3=20al?= =?UTF-8?q?lOf=E7=9A=84=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- knife4j-vue/src/core/Knife4jAsync.js | 90 ++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) diff --git a/knife4j-vue/src/core/Knife4jAsync.js b/knife4j-vue/src/core/Knife4jAsync.js index d91f2562..7e9cbde6 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 { -- Gitee