Example #1
0
        /// <summary>
        /// Attempts to combine two Swagger Paths, as best as possible.
        /// </summary>
        /// <param name="destination">The destination.</param>
        /// <param name="source">The source.</param>
        /// <remarks>Swagger 2.0 does not support multiple paths with the same url. This may result in incorrect specifications for ApiActions with the same url.</remarks>
        private static void Combine(SwaggerPath destination, SwaggerPath source)
        {
            var duplicateDictionary = destination.Item.Methods.ToDictionary(i => i.Method, i => i);

            foreach (var method in source.Item.Methods)
            {
                if (!duplicateDictionary.ContainsKey(method.Method))
                {
                    duplicateDictionary.Add(method.Method, method);
                }
            }

            destination.Item.Parameters = (destination.Item.Parameters ?? new SwaggerParameter[0])
                                          .Union(source.Item.Parameters ?? new SwaggerParameter[0]).ToArray();

            foreach (var param in destination.Item.Parameters)
            {
                if (param.In != SwaggerRequestLocation.path)
                {
                    param.Required = false;
                }
            }

            destination.Item.Methods =
                new SwaggerObjectCollectionFacade <UnofficialPathItemMethod>(duplicateDictionary.Values);
        }
Example #2
0
        protected virtual SwaggerPath GetPath(IApiActionRegistration registration)
        {
            // TODO: Move registration items to info provider
            var info = _infoProvider.GetInfo(registration.ApiActionType);

            var methods = info.Methods;

            if (methods == null || methods.Length == 0)
            {
                methods = _defaultMethods;
            }

            var path = new SwaggerPath
            {
                Path = "/" + RemoveTypesFromRoute(registration.Route),
                Item = new SwaggerPathItem
                {
                    Methods = new SwaggerObjectCollectionFacade <UnofficialPathItemMethod>(methods.Select(m =>
                                                                                                          new UnofficialPathItemMethod
                    {
                        Method    = m,
                        Operation = new SwaggerOperation
                        {
                            Description = info.Description,
                            Tags        = info.Categories,
                            Summary     = info.Summary,
                            Deprecated  = info.IsDeprecated,
                            Responses   = new SwaggerObjectCollectionFacade <SwaggerResponse>(
                                _responseFactory.Create(info.Responses))
                        }
                    })),
                    Parameters = GetParameters(registration, info)
                }
            };

            return(path);
        }
Example #3
0
        /// <summary>
        /// 转换为swagger
        /// </summary>
        /// <param name="dll"></param>
        /// <returns></returns>
        public SwaggerHandler ConvertServiceToSwagger(List <Service> dll)
        {
            var list = new SwaggerHandler();

            foreach (var service in dll)
            {
                var path = new SwaggerPath();
                //请求参数
                path.parameters = new List <SwaggerParam>()
                {
                    new SwaggerParam()
                    {
                        In          = SwaggerParamInEnum.Body,
                        description = "",
                        name        = "request",
                        required    = true,
                        schema      = service.RequestModel.TypeModel != null
                            ? new SwaggerPro()
                        {
                            ModelName = service.RequestModel.Type
                        }
                            : new SwaggerPro()
                        {
                            type = service.RequestModel.Type
                        }
                    }
                };
                //响应参数
                path.responses = new Dictionary <string, SwaggerResponse>()
                {
                    {
                        "200", new SwaggerResponse()
                    }
                };
                if (service.ResponseModel.TypeModel != null)
                {
                    path.responses["200"].schema = new SwaggerPro()
                    {
                        ModelName = service.ResponseModel.FieldName
                    };
                }
                else
                {
                    if (service.ResponseModel.IsArray)
                    {
                        path.responses["200"].schema = new SwaggerSchemaAry()
                        {
                            type  = FieldType.Array,
                            items = new SwaggerPro()
                            {
                                ModelName = service.ResponseModel.FieldName
                            }
                        };
                    }
                    else
                    {
                        path.responses["200"].schema = new SwaggerPro()
                        {
                            type = service.ResponseModel.FieldName
                        };
                    }
                }
                //模型
                if (service.RequestModel.TypeModel != null)
                {
                    foreach (var definion in ConverDefinions(service.RequestModel))
                    {
                        if (!list.definitions.ContainsKey(definion.Key))
                        {
                            list.definitions.Add(definion.Key, definion.Value);
                        }
                    }
                }
                if (service.ResponseModel.TypeModel != null)
                {
                    foreach (var definion in ConverDefinions(service.ResponseModel))
                    {
                        if (!list.definitions.ContainsKey(definion.Key))
                        {
                            list.definitions.Add(definion.Key, definion.Value);
                        }
                    }
                }

                list.paths.Add(service.ActionName, new Dictionary <string, SwaggerPath>()
                {
                    {
                        ConverToHttpMethod(service.HttpMethod), path
                    }
                });
            }
            return(list);
        }
Example #4
0
        /// <summary>
        /// Create the template output
        /// </summary>
        public virtual string TransformText()
        {
            #line 8 "E:\My-GitHub\my-csharp-project\MyWebApiClientBuilder\Template\MyJavaScriptService.tt"

            Dictionary <string, SwaggerPath> paths = DocInfo.Paths;


            #line default
            #line hidden
            this.Write("\"use strict\";\r\n\r\n\r\nvar MyJavaScriptService = {\r\n\r\n\t// 登录后的 Token.\r\n\taccessToken :" +
                       " \"\",\r\n\r\n\r\n");

            #line 20 "E:\My-GitHub\my-csharp-project\MyWebApiClientBuilder\Template\MyJavaScriptService.tt"

            foreach (var key in paths.Keys)
            {
                SwaggerPath pathData = paths[key];

                string[] apiPaths = key.Split('/');
                string   lastPath = apiPaths[apiPaths.Length - 1];

                // url 地址.
                string url = key;

                // 是否是动态的 url.
                bool isDynamic = false;

                // 检查地址的最后一个
                if (lastPath.StartsWith("{") && lastPath.EndsWith("}"))
                {
                    // 最后一个是动态地址.
                    isDynamic = true;

                    // 变更 url.
                    url = url.Replace(lastPath, "");

                    // 变更 js 方法的路径.
                    lastPath = apiPaths[apiPaths.Length - 2];
                }


            #line default
            #line hidden
                this.Write("\r\n");

            #line 47 "E:\My-GitHub\my-csharp-project\MyWebApiClientBuilder\Template\MyJavaScriptService.tt"

                // ---------- GET 处理.
                if (pathData.Get != null)
                {
                    // 路径的参数.
                    List <string> pathParameterNameList = pathData.Get.GetParameterNameList("path");
                    // 查询的参数.
                    List <string> queryParameterNameList = pathData.Get.GetParameterNameList("query");
                    // hearder 的参数.
                    List <string> headerParameterNameList = pathData.Get.GetParameterNameList("header");

                    // js 的方法名.
                    string jsFuncName = lastPath;
                    if (isDynamic)
                    {
                        // 动态路径的情况下. 变更方法名.
                        jsFuncName = "get" + jsFuncName;
                    }

                    jsFuncName = Renamer.RenameJavaScriptFunctionName(jsFuncName);


                    // js 方法的参数.
                    string jsFuncParam = "";
                    if (isDynamic)
                    {
                        // 动态路径的情况下. 参数 = 路径的参数
                        jsFuncParam = string.Join(",", pathParameterNameList);
                    }
                    else
                    {
                        // 非动态路径的情况下. 参数 = 查询的参数
                        jsFuncParam = string.Join(",", queryParameterNameList);
                    }



            #line default
            #line hidden
                    this.Write("\r\n");

            #line 80 "E:\My-GitHub\my-csharp-project\MyWebApiClientBuilder\Template\MyJavaScriptService.tt"
                    if (!String.IsNullOrEmpty(pathData.Get.Summary))
                    {
            #line default
            #line hidden
                        this.Write(" \r\n\t// ");

            #line 81 "E:\My-GitHub\my-csharp-project\MyWebApiClientBuilder\Template\MyJavaScriptService.tt"
                        this.Write(this.ToStringHelper.ToStringWithCulture(pathData.Get.Summary));

            #line default
            #line hidden
                        this.Write("\r\n");

            #line 82 "E:\My-GitHub\my-csharp-project\MyWebApiClientBuilder\Template\MyJavaScriptService.tt"
                    }

            #line default
            #line hidden
                    this.Write("\t");

            #line 83 "E:\My-GitHub\my-csharp-project\MyWebApiClientBuilder\Template\MyJavaScriptService.tt"
                    this.Write(this.ToStringHelper.ToStringWithCulture(jsFuncName));

            #line default
            #line hidden
                    this.Write(": function(");

            #line 83 "E:\My-GitHub\my-csharp-project\MyWebApiClientBuilder\Template\MyJavaScriptService.tt"
                    this.Write(this.ToStringHelper.ToStringWithCulture(jsFuncParam));

            #line default
            #line hidden
                    this.Write(") {\r\n\t\treturn axios({\r\n\t\t\tmethod : \'get\',\r\n");

            #line 86 "E:\My-GitHub\my-csharp-project\MyWebApiClientBuilder\Template\MyJavaScriptService.tt"
                    if (isDynamic)
                    {
            #line default
            #line hidden
                        this.Write("\t\t\turl: \'");

            #line 87 "E:\My-GitHub\my-csharp-project\MyWebApiClientBuilder\Template\MyJavaScriptService.tt"
                        this.Write(this.ToStringHelper.ToStringWithCulture(url));

            #line default
            #line hidden
                        this.Write("\' + ");

            #line 87 "E:\My-GitHub\my-csharp-project\MyWebApiClientBuilder\Template\MyJavaScriptService.tt"
                        this.Write(this.ToStringHelper.ToStringWithCulture(jsFuncParam));

            #line default
            #line hidden
                        this.Write(", \r\n");

            #line 88 "E:\My-GitHub\my-csharp-project\MyWebApiClientBuilder\Template\MyJavaScriptService.tt"
                    }
                    else
                    {
            #line default
            #line hidden
                        this.Write("\t\t\turl: \'");

            #line 89 "E:\My-GitHub\my-csharp-project\MyWebApiClientBuilder\Template\MyJavaScriptService.tt"
                        this.Write(this.ToStringHelper.ToStringWithCulture(url));

            #line default
            #line hidden
                        this.Write("\', \r\n");

            #line 90 "E:\My-GitHub\my-csharp-project\MyWebApiClientBuilder\Template\MyJavaScriptService.tt"
                    }

            #line default
            #line hidden

            #line 91 "E:\My-GitHub\my-csharp-project\MyWebApiClientBuilder\Template\MyJavaScriptService.tt"
                    if (headerParameterNameList.Count() > 0)
                    {
            #line default
            #line hidden
                        this.Write("\t\t\theaders:{\r\n\t\t\t\t\'Authorization\': \'Bearer \' + this.accessToken\r\n\t\t\t},\r\n");

            #line 95 "E:\My-GitHub\my-csharp-project\MyWebApiClientBuilder\Template\MyJavaScriptService.tt"
                    }

            #line default
            #line hidden
                    this.Write("            params: {\r\n");

            #line 97 "E:\My-GitHub\my-csharp-project\MyWebApiClientBuilder\Template\MyJavaScriptService.tt"
                    foreach (string param in queryParameterNameList)
                    {
            #line default
            #line hidden
                        this.Write("                ");

            #line 98 "E:\My-GitHub\my-csharp-project\MyWebApiClientBuilder\Template\MyJavaScriptService.tt"
                        this.Write(this.ToStringHelper.ToStringWithCulture(param));

            #line default
            #line hidden
                        this.Write(": ");

            #line 98 "E:\My-GitHub\my-csharp-project\MyWebApiClientBuilder\Template\MyJavaScriptService.tt"
                        this.Write(this.ToStringHelper.ToStringWithCulture(param));

            #line default
            #line hidden
                        this.Write(",\r\n");

            #line 99 "E:\My-GitHub\my-csharp-project\MyWebApiClientBuilder\Template\MyJavaScriptService.tt"
                    }

            #line default
            #line hidden
                    this.Write("            },\r\n\t\t\tresponseType: \'json\',\r\n        });\r\n\t},\r\n\r\n");

            #line 105 "E:\My-GitHub\my-csharp-project\MyWebApiClientBuilder\Template\MyJavaScriptService.tt"
                }

                // ---------- POST 处理.
                if (pathData.Post != null)
                {
                    // Post 的参数.
                    List <SwaggerActionParameter> bodyParameterList = pathData.Post.GetParameterList("body");
                    // 取得唯一的参数.
                    SwaggerActionParameter bodyParameter = bodyParameterList.FirstOrDefault();

                    // 取得引用的属性.
                    string refName = null;

                    if (bodyParameter != null)
                    {
                        refName = bodyParameter.SchemaRefName;
                    }

                    // 模型的属性定义.
                    SwaggerDefinition modelDefinition = null;

                    if (!string.IsNullOrEmpty(refName))
                    {
                        modelDefinition = DocInfo.GetSwaggerDefinition(refName);
                    }



                    // hearder 的参数.
                    List <string> headerParameterNameList = pathData.Post.GetParameterNameList("header");

                    // js 的方法名.
                    string jsFuncName = lastPath;
                    jsFuncName = Renamer.RenameJavaScriptFunctionName(jsFuncName);

                    List <string> propertyNameList = new List <string>();

                    if (modelDefinition != null)
                    {
                        propertyNameList = modelDefinition.PropertyNameList;
                    }

                    // js 方法的参数.
                    string jsFuncParam = string.Join(",", propertyNameList);



            #line default
            #line hidden
                    this.Write("\r\n");

            #line 152 "E:\My-GitHub\my-csharp-project\MyWebApiClientBuilder\Template\MyJavaScriptService.tt"
                    if (!String.IsNullOrEmpty(pathData.Post.Summary))
                    {
            #line default
            #line hidden
                        this.Write(" \r\n\t// ");

            #line 153 "E:\My-GitHub\my-csharp-project\MyWebApiClientBuilder\Template\MyJavaScriptService.tt"
                        this.Write(this.ToStringHelper.ToStringWithCulture(pathData.Post.Summary));

            #line default
            #line hidden
                        this.Write("\r\n");

            #line 154 "E:\My-GitHub\my-csharp-project\MyWebApiClientBuilder\Template\MyJavaScriptService.tt"
                    }

            #line default
            #line hidden
                    this.Write("\t");

            #line 155 "E:\My-GitHub\my-csharp-project\MyWebApiClientBuilder\Template\MyJavaScriptService.tt"
                    this.Write(this.ToStringHelper.ToStringWithCulture(jsFuncName));

            #line default
            #line hidden
                    this.Write(": function(");

            #line 155 "E:\My-GitHub\my-csharp-project\MyWebApiClientBuilder\Template\MyJavaScriptService.tt"
                    this.Write(this.ToStringHelper.ToStringWithCulture(jsFuncParam));

            #line default
            #line hidden
                    this.Write(") {\r\n\t\treturn axios({\r\n\t\t\tmethod : \'post\',\r\n");

            #line 158 "E:\My-GitHub\my-csharp-project\MyWebApiClientBuilder\Template\MyJavaScriptService.tt"
                    if (isDynamic)
                    {
            #line default
            #line hidden
                        this.Write("\t\t\turl: \'");

            #line 159 "E:\My-GitHub\my-csharp-project\MyWebApiClientBuilder\Template\MyJavaScriptService.tt"
                        this.Write(this.ToStringHelper.ToStringWithCulture(url));

            #line default
            #line hidden
                        this.Write("\' + ");

            #line 159 "E:\My-GitHub\my-csharp-project\MyWebApiClientBuilder\Template\MyJavaScriptService.tt"
                        this.Write(this.ToStringHelper.ToStringWithCulture(jsFuncParam));

            #line default
            #line hidden
                        this.Write(", \r\n");

            #line 160 "E:\My-GitHub\my-csharp-project\MyWebApiClientBuilder\Template\MyJavaScriptService.tt"
                    }
                    else
                    {
            #line default
            #line hidden
                        this.Write("\t\t\turl: \'");

            #line 161 "E:\My-GitHub\my-csharp-project\MyWebApiClientBuilder\Template\MyJavaScriptService.tt"
                        this.Write(this.ToStringHelper.ToStringWithCulture(url));

            #line default
            #line hidden
                        this.Write("\', \r\n");

            #line 162 "E:\My-GitHub\my-csharp-project\MyWebApiClientBuilder\Template\MyJavaScriptService.tt"
                    }

            #line default
            #line hidden

            #line 163 "E:\My-GitHub\my-csharp-project\MyWebApiClientBuilder\Template\MyJavaScriptService.tt"
                    if (headerParameterNameList.Count() > 0)
                    {
            #line default
            #line hidden
                        this.Write("\t\t\theaders:{\r\n\t\t\t\t\'Authorization\': \'Bearer \' + this.accessToken\r\n\t\t\t},\r\n");

            #line 167 "E:\My-GitHub\my-csharp-project\MyWebApiClientBuilder\Template\MyJavaScriptService.tt"
                    }

            #line default
            #line hidden

            #line 168 "E:\My-GitHub\my-csharp-project\MyWebApiClientBuilder\Template\MyJavaScriptService.tt"
                    if (propertyNameList != null && propertyNameList.Count() > 0)
                    {
            #line default
            #line hidden
                        this.Write("            data: {\r\n");

            #line 170 "E:\My-GitHub\my-csharp-project\MyWebApiClientBuilder\Template\MyJavaScriptService.tt"
                        foreach (string param in propertyNameList)
                        {
            #line default
            #line hidden
                            this.Write("                ");

            #line 171 "E:\My-GitHub\my-csharp-project\MyWebApiClientBuilder\Template\MyJavaScriptService.tt"
                            this.Write(this.ToStringHelper.ToStringWithCulture(param));

            #line default
            #line hidden
                            this.Write(": ");

            #line 171 "E:\My-GitHub\my-csharp-project\MyWebApiClientBuilder\Template\MyJavaScriptService.tt"
                            this.Write(this.ToStringHelper.ToStringWithCulture(param));

            #line default
            #line hidden
                            this.Write(",\r\n");

            #line 172 "E:\My-GitHub\my-csharp-project\MyWebApiClientBuilder\Template\MyJavaScriptService.tt"
                        }

            #line default
            #line hidden
                        this.Write("            },\r\n");

            #line 174 "E:\My-GitHub\my-csharp-project\MyWebApiClientBuilder\Template\MyJavaScriptService.tt"
                    }

            #line default
            #line hidden
                    this.Write("\t\t\tresponseType: \'json\',\r\n        });\r\n\t},\r\n\r\n");

            #line 179 "E:\My-GitHub\my-csharp-project\MyWebApiClientBuilder\Template\MyJavaScriptService.tt"
                }


            #line default
            #line hidden
                this.Write("\r\n\r\n");

            #line 184 "E:\My-GitHub\my-csharp-project\MyWebApiClientBuilder\Template\MyJavaScriptService.tt"
            }


            #line default
            #line hidden
            this.Write("\r\n}");
            return(this.GenerationEnvironment.ToString());
        }