private static Response GetResponse(string returnDescStr) { if (string.IsNullOrEmpty(returnDescStr) || !returnDescStr.StartsWith("{")) { return(new Response { Description = "Success", Schema = new Schema { Type = returnDescStr } }); } ServiceReturnDesc returnDesc = JsonConvert.DeserializeObject <ServiceReturnDesc>(TypeHelper.ReplaceTypeToJsType(returnDescStr)); bool isObject = TypeHelper.CheckIsObject(returnDesc.ReturnType); Response response = new Response { Description = string.IsNullOrEmpty(returnDesc.Comment) ? "Success" : returnDesc.Comment, Schema = new Schema { Type = isObject ? "object" : returnDesc.ReturnType, Example = (isObject && returnDesc.ReturnFormat.StartsWith("{")) ? JsonConvert.DeserializeObject <dynamic>(returnDesc.ReturnFormat) : returnDesc.ReturnFormat, } }; bool isArray = TypeHelper.CheckIsArray(returnDesc.ReturnType); if (isArray) { response.Schema.Example = (isObject && returnDesc.ReturnFormat.StartsWith("{")) ? JsonConvert.DeserializeObject <dynamic>($"[{returnDesc.ReturnFormat}]") : $"[{returnDesc.ReturnFormat}]"; } return(response); }
private string GetReturnDesc(MethodInfo methodInfo) { ServiceReturnDesc desc = new ServiceReturnDesc(); List <Type> customTypes = new List <Type>(); //判断是否为task泛型 if (methodInfo.ReturnType.ToString().IndexOf("System.Threading.Tasks.Task", StringComparison.Ordinal) == 0 && methodInfo.ReturnType.IsGenericType) { desc.ReturnType = string.Join(",", methodInfo.ReturnType.GenericTypeArguments.Select(x => x.FullName)); customTypes = (from type in methodInfo.ReturnType.GenericTypeArguments from childType in type.GenericTypeArguments select childType).ToList(); } else if (methodInfo.ReturnType.IsGenericType) { desc.ReturnType = methodInfo.ReturnType.ToString(); customTypes = methodInfo.ReturnType.GenericTypeArguments.ToList(); } else { desc.ReturnType = methodInfo.ReturnType.ToString(); customTypes = new List <Type> { methodInfo.ReturnType }; } if (string.IsNullOrEmpty(desc.ReturnFormat)) { desc.ReturnFormat = GetReturnFormat(customTypes); } return(_serializer.Serialize <string>(desc)); }