public static void InitializeSwagger(InitializeSwaggerArgs args) { var swagger = new SwaggerDefinition { Host = args.HostName, Schemes = args.Schemes, BasePath = args.BasePath, Info = new SwaggerInfo { Description = args.Description, Title = args.Title, Version = args.Version }, Consumes = new string[] { "application/json" }, Produces = new string[] { "application/json" }, }; swagger.SaveToDisk(args.SwaggerFilePath); }
private void CreateSwaggerDefinition(string name, Type definitionType, Dictionary <string, SwaggerDefinition> definitions) { if (definitions.ContainsKey(name)) { return; } SwaggerDefinition definition = new SwaggerDefinition { Type = "object", Properties = new Dictionary <string, SwaggerPropertyDefinition>() }; var properties = definitionType.GetProperties(); properties.ForEach(p => { SwaggerPropertyDefinition pd = new SwaggerPropertyDefinition { Description = this._resolver.GetMemberInfoComment(p) }; if (p.PropertyType == typeof(string) || p.PropertyType.IsValueType) { pd.Type = GetSwaggerType(p.PropertyType); } else if (p.PropertyType.IsArray && p.PropertyType.HasElementType) { pd.Type = "array"; pd.Items = GetSwaggerItemSchema(p.PropertyType.GetElementType(), definitions); } else { pd.Ref = "#/definitions/" + p.PropertyType.Name; CreateSwaggerDefinition(p.PropertyType.Name, p.PropertyType, definitions); } definition.Properties.Add(p.Name.ToCamelCase(), pd); }); definitions.Add(name, definition); }
private void CreateSwaggerDefinition(string name, Type definitionType, Dictionary <string, SwaggerDefinition> definitions, SwaggerConfig config) { if (definitions.ContainsKey(name)) { return; } if ("Type".Equals(name)) { return; } SwaggerDefinition definition = new SwaggerDefinition { Type = "object", Properties = new Dictionary <string, SwaggerPropertyDefinition>() }; definitions.Add(name, definition); var properties = definitionType.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly); properties.ForEach(p => { SwaggerPropertyDefinition pd = new SwaggerPropertyDefinition { Description = this._resolver.GetMemberInfoComment(p) }; if (p.PropertyType == typeof(string) || p.PropertyType.IsValueType) { pd.Type = GetSwaggerType(p.PropertyType); } else if (p.PropertyType.IsArray && p.PropertyType.HasElementType) { pd.Type = "array"; pd.Items = GetSwaggerItemSchema(p.PropertyType.GetElementType(), definitions, config); } else if ( typeof(System.Collections.ICollection).IsAssignableFrom(p.PropertyType) || typeof(System.Collections.IEnumerable).IsAssignableFrom(p.PropertyType) ) { if (p.PropertyType.IsGenericType) { pd.Type = "array"; pd.Items = GetSwaggerItemSchema(p.PropertyType.GenericTypeArguments[0], definitions, config); } else { pd.Type = "array"; } } else { //Console.WriteLine(p.PropertyType.Name); pd.Ref = "#/definitions/" + p.PropertyType.Name; CreateSwaggerDefinition(p.PropertyType.Name, p.PropertyType, definitions, config); } definition.Properties.Add(p.Name.ToCamelCase(), pd); }); }
/// <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()); }