Bladeren bron

[*]修复swagger文档必填字段显示问题,responseBody属性字段过滤level字段

Lianjy 3 jaren geleden
bovenliggende
commit
a5e92dc100

+ 7 - 3
magic-api/src/main/java/org/ssssssss/magicapi/swagger/SwaggerProvider.java

@@ -188,7 +188,7 @@ public class SwaggerProvider {
 	}
 
 	private Map<String, Object> doProcessDefinition(BaseDefinition target, ApiInfo info, String parentName, String definitionType, int level) {
-		Map<String, Object> result = new HashMap<>(3);
+		Map<String, Object> result = new HashMap<>(4);
 		result.put("description", target.getDescription());
 		if (DataType.Array == target.getDataType()) {
 			if (!CollectionUtils.isEmpty(target.getChildren())) {
@@ -201,15 +201,19 @@ public class SwaggerProvider {
 			String groupName = groupServiceProvider.getFullName(info.getGroupId()).replace("/", "-");
 			String voName = groupName + "«" + info.getPath().replaceFirst("/", "").replaceAll("/", "_") + (StringUtils.equals("response", definitionType) ? "«response«" : "«request«") + parentName + target.getName() + "»»»";
 
-			Map<String, Object> definition = new HashMap<>(3);
+			Map<String, Object> definition = new HashMap<>(4);
 			Map<String, Map<String, Object>> properties = new HashMap<>(target.getChildren().size());
+			Set<String> requiredSet = new HashSet <>(target.getChildren().size());
 			for (BaseDefinition obj : target.getChildren()) {
 				properties.put(obj.getName(), doProcessDefinition(obj, info, parentName + target.getName() + "_", definitionType, level + 1));
+				if (obj.isRequired()) {
+					requiredSet.add(obj.getName());
+				}
 			}
 			definition.put("properties", properties);
 			definition.put("description", target.getDescription());
 			definition.put("type", target.getDataType().getJavascriptType());
-
+			definition.put("required", requiredSet);
 			if (this.DEFINITION_MAP.containsKey(voName)) {
 				// TODO 应该不会出现名字都一样的
 				voName = voName.replace("»»»", "_" + level + "»»»");

+ 1 - 1
magic-editor/src/console/src/components/layout/magic-run.vue

@@ -41,7 +41,7 @@ export default {
   watch: {
     responseBody: {
       handler(responseBodyArr) {
-        this.info.responseBodyDefinition = responseBodyArr[0]
+        this.info.responseBodyDefinition = deepClone(responseBodyArr[0], ['level'])
       },
       deep: true
     }

+ 21 - 14
magic-editor/src/console/src/scripts/utils.js

@@ -68,21 +68,28 @@ const isArray = (arr) => {
   return Object.prototype.toString.call(arr) === '[object Array]';
 }
 
-// 深度克隆
-const deepClone = (obj) => {
-  // 对常见的“非”值,直接返回原来值
-  if([null, undefined, NaN, false].includes(obj)) return obj;
-  if(typeof obj !== "object" && typeof obj !== 'function') {
-    //原始类型直接返回
-    return obj;
-  }
-  var o = isArray(obj) ? [] : {};
-  for(let i in obj) {
-    if(obj.hasOwnProperty(i)){
-      o[i] = typeof obj[i] === "object" ? deepClone(obj[i]) : obj[i];
+/*
+ * @Description 深度克隆
+ * ignoreFields 忽略克隆对象字段,只针对对象有效
+ */
+const deepClone = (obj, ignoreFields = []) => {
+    // 对常见的“非”值,直接返回原来值
+    if([null, undefined, NaN, false].includes(obj)) return obj;
+    if(typeof obj !== "object" && typeof obj !== 'function') {
+        //原始类型直接返回
+        return obj;
     }
-  }
-  return o;
+    var o = isArray(obj) ? [] : {};
+    for(let i in obj) {
+        if(obj.hasOwnProperty(i)){
+            o[i] = typeof obj[i] === "object" ? deepClone(obj[i], ignoreFields) : obj[i];
+        }
+    }
+    // 清除忽略字段
+    ignoreFields.forEach(i => {
+        delete o[i]
+    })
+    return o;
 }
 
 // 展示锚点对象