Example #1
0
        /// <summary>
        /// 递归获取 Body 参数说明
        /// </summary>
        /// <param name="key"></param>
        /// <param name="isShowRequired"></param>
        /// <returns></returns>
        private object GetModelInfo(string key, bool isShowRequired = true)
        {
            if (key == null)
            {
                return(null);
            }
            if (key != null && Schemas.ContainsKey(key) == false)
            {
                return(key);
            }
            var schema = Schemas.SingleOrDefault(x => x.Key == key).Value;

            if (schema.Properties.Any() == false)
            {
                return new EnumInfo()
                       {
                           枚举范围 = GetEnumValues(key),
                           枚举描述 = schema.Description,
                           枚举类型 = schema.Format,
                           枚举名称 = key
                       }
            }
            ;
            var properties = new Dictionary <string, object>();

            foreach (var item in schema.Properties)
            {
                object obj = "object";

                if (item.Value.IsObject(Schemas))
                {
                    var objKey = item.Value.Reference.Id;
                    if (objKey == key)
                    {
                        obj = objKey;
                    }
                    else
                    {
                        obj = GetModelInfo(objKey, isShowRequired);
                    }
                }
                else if (item.Value.IsArray())
                {
                    var arrayKey = "";
                    if (item.Value.IsBaseTypeArray())
                    {
                        arrayKey = item.Value.Items.Type;
                    }
                    else
                    {
                        arrayKey = item.Value.Items.Reference.Id;
                    }
                    obj = new[] { GetModelInfo(arrayKey, isShowRequired) };
                }
                else if (item.Value.IsEnum(Schemas))
                {
                    var enumKey = item.Value.Reference.Id;
                    var enumObj = GetEnumSchema(enumKey);
                    obj = new EnumInfo()
                    {
                        枚举范围 = GetEnumValues(enumKey),
                        枚举类型 = enumObj.Format,
                        枚举名称 = enumKey,
                        枚举描述 = enumObj.Description
                    };
                }
                else
                {
                    obj = item.Value.Format ?? item.Value.Type;
                }

                if (isShowRequired)
                {
                    var requestModelInfo = new RequestModelInfo
                    {
                        参数类型 = obj,
                        描述   = item.Value.Description,
                        是否必传 = schema.Required.Any(x => x == item.Key),
                        可空类型 = item.Value.Nullable
                    };
                    properties.Add(item.Key, requestModelInfo);
                }
                else
                {
                    var responseModelInfo = new ResponseModelInfo
                    {
                        参数类型 = obj,
                        描述   = item.Value.Description,
                        可空类型 = item.Value.Nullable
                    };
                    properties.Add(item.Key, responseModelInfo);
                }
            }
            return(properties);
        }
 /// <summary>
 /// 递归获取 Body 参数说明
 /// </summary>
 /// <param name="key"></param>
 /// <param name="isShowRequired"></param>
 /// <returns></returns>
 private object GetModelInfo(string key, bool isShowRequired = true)
 {
     if (key != null)
     {
         if (Schemas.ContainsKey(key))
         {
             var schema = Schemas.FirstOrDefault(x => x.Key == key).Value;
             var info   = new Dictionary <string, object>();
             if (schema.Properties.Any())
             {
                 foreach (var item in schema.Properties)
                 {
                     object obj = item.Value.Format ?? item.Value.Type ?? "object";
                     if (item.Value.IsObject(Schemas))
                     {
                         var objKey = item.Value.Reference.Id;
                         if (objKey == key)
                         {
                             obj = objKey;
                         }
                         else
                         {
                             obj = GetModelInfo(objKey, isShowRequired);
                         }
                     }
                     else if (item.Value.IsArray())
                     {
                         var arrayKey = "";
                         if (item.Value.IsBaseTypeArray())
                         {
                             arrayKey = item.Value.Items.Type;
                         }
                         else
                         {
                             arrayKey = item.Value.Items.Reference.Id;
                         }
                         obj = new[] { GetModelInfo(arrayKey, isShowRequired) };
                     }
                     else
                     {
                         if (item.Value.IsEnum(Schemas))
                         {
                             obj = item.Value.Reference.Id;
                         }
                     }
                     if (isShowRequired)
                     {
                         var requestModelInfo = new RequestModelInfo
                         {
                             参数类型 = obj,
                             描述   = item.Value.Description,
                             是否必传 = schema.Required.Any(x => x == item.Key),
                             可空类型 = item.Value.Nullable
                         };
                         info.Add(item.Key, requestModelInfo);
                     }
                     else
                     {
                         var responseModelInfo = new ResponseModelInfo
                         {
                             参数类型 = obj,
                             描述   = item.Value.Description,
                             可空类型 = item.Value.Nullable
                         };
                         info.Add(item.Key, responseModelInfo);
                     }
                 }
             }
             return(info);
         }
         else
         {
             return(key);
         }
     }
     return(null);
 }