예제 #1
0
        private static object ParamToJson(MsDocEntryPointMethodParams param, TypeDescription desc, bool asString = false)
        {
            if (desc.JsonParam != null)
            {
                return(desc.JsonParam);
            }

            object obj;

            if (param.Type.StartsWith("System.Collections.Generic.IEnumerable`1[") ||
                param.Type.StartsWith("System.Collections.Generic.List`1") ||
                param.Type.EndsWith("[]") && (param.PureType.IsGenericType || param.PureType.IsArray))
            {
                var elementType = param.PureType.IsArray ? param.PureType.GetElementType() : param.PureType.GenericTypeArguments[0];

                Type listType = typeof(List <>).MakeGenericType(new [] { elementType });
                var  list     = (System.Collections.IList)Activator.CreateInstance(listType);

                var el = ClassNamePluralizer.ToHumanName(elementType.ToString(), elementType);

                if (el != null && el.JsonParam != null && el.JsonParam.GetType() == elementType)
                {
                    list.Add(el.JsonParam);
                }
                else if (!elementType.IsAbstract)
                {
                    list.Add(Activator.CreateInstance(elementType));
                }

                obj = list;
            }
            else
            {
                obj = Activator.CreateInstance(param.PureType);
            }

            if (obj is Enum)
            {
                var names = param.PureType.GetEnumNames();
                obj = names.Length > 0 ? names[0].ToLower() : obj.ToString();
            }

            if (asString)
            {
                return(obj == null ? "null" : (obj is string?(string)obj : JsonConvert.SerializeObject(obj)).Trim('"'));
            }

            return(obj);
        }
예제 #2
0
        public static void GenerateRequestExample(MsDocEntryPointMethod method)
        {
            var sb = new StringBuilder();

            var visible    = method.Params.Where(p => p.Visible).ToDictionary(p => p, p => ClassNamePluralizer.ToHumanName(p.Type, p.PureType));
            var urlParams  = visible.Where(p => p.Key.Method == "url").ToList();
            var bodyParams = visible.Except(urlParams).ToList();

            var path  = method.Path.ToLowerInvariant();
            var query = false;

            urlParams.ForEach(p => {
                if (path.IndexOf('{' + p.Key.Name.ToLowerInvariant() + '}') > 0)
                {
                    path = path.Replace('{' + p.Key.Name.ToLowerInvariant() + '}', HttpUtility.UrlEncode(ParamToJson(p.Key, p.Value, true).ToString()));
                }
                else
                {
                    var value = ParamToJson(p.Key, p.Value, true).ToString();
                    if (value == "null")
                    {
                        return;
                    }

                    path += string.Format("{0}{1}={2}", query ? "&" : "?", p.Key.Name, HttpUtility.UrlEncode(value));
                    query = true;
                }
            });

            var args = new Dictionary <string, object>();

            bodyParams.ForEach(p => args.Add(p.Key.Name, ParamToJson(p.Key, p.Value)));

            sb.AppendFormat("{0} {1}", method.HttpMethod, path).AppendLine()
            .AppendLine("Host: yourportal.onlyoffice.com")
            .AppendLine("Content-Type: application/json")
            .Append("Accept: application/json");

            if (args.Any())
            {
                sb.AppendLine().AppendLine()
                .Append(JsonConvert.SerializeObject(args, Formatting.Indented));
            }

            method.Example = sb.ToString();
        }