Exemple #1
0
        private static void AppendEntityDataLine(StringBuilder sb, Type type)
        {
            foreach (var entityField in type.GetFields())
            {
                string key   = $"{type.Namespace}.{type.Name}.{entityField.Name}";
                string value = SwaggerCustomDescription.GetComments(key);
                if (value == null)
                {
                    // 상속 받은 경우 부모 클래스의 정보 가져옴
                    key = $"{type.Namespace}.{entityField.DeclaringType.Name}.{entityField.Name}";
                }

                AppendLastLine(sb, entityField.FieldType, entityField.Name, key);
            }

            foreach (var propertiesField in type.GetProperties())
            {
                string key   = $"{type.Namespace}.{type.Name}.{propertiesField.Name}";
                string value = SwaggerCustomDescription.GetComments(key);
                if (value == null)
                {
                    // 상속 받은 경우 부모 클래스의 정보 가져옴
                    key = $"{type.Namespace}.{propertiesField.DeclaringType.Name}.{propertiesField.Name}";
                }

                AppendLastLine(sb, propertiesField.PropertyType, propertiesField.Name, key);
            }
        }
Exemple #2
0
        private static void AppendDataLine(StringBuilder sb, Type type, string name, string key, bool isInner = false)
        {
            if (type.Namespace.Contains("System.Collections"))
            {
                if (type.Name.Contains("List"))
                {
                    if (type.GenericTypeArguments.Length > 0)
                    {
                        Type argType = type.GenericTypeArguments[0];

                        string typeName = $"List [{argType.Name}]";
                        sb.AppendLine($"| {name} | {typeName} | | {SwaggerCustomDescription.GetComments(key)} |");

                        if (argType.Namespace.Contains("ApiServer.Models"))
                        {
                            AppendEntityDataLine(sb, argType);
                        }
                    }
                }
                else if (type.Name.Contains("Dictionary"))
                {
                    if (type.GenericTypeArguments.Length > 1)
                    {
                        Type argTypeKey   = type.GenericTypeArguments[0];
                        Type argTypeValue = type.GenericTypeArguments[1];

                        string typeName = $"Dictionary <{argTypeKey.Name},{argTypeValue.Name}>";
                        sb.AppendLine($"| {name} | {typeName} | | {SwaggerCustomDescription.GetComments(key)} |");

                        // value 타입에 대해서만 추가 문서 작성
                        if (argTypeValue.Namespace.Contains("ApiServer.Models"))
                        {
                            AppendEntityDataLine(sb, argTypeValue);
                        }
                    }
                }
            }
            else if (type.Namespace.Contains("ApiServer.Models"))
            {
                sb.AppendLine($"| {name} | {type.Name} | | {SwaggerCustomDescription.GetComments(key)} |");
                AppendEntityDataLine(sb, type);
            }
            else
            {
                if (isInner != true)
                {
                    sb.AppendLine($"| {name} | {type.Name} | | {SwaggerCustomDescription.GetComments(key)} |");
                }
            }
        }
Exemple #3
0
        private static void AppendLastLine(StringBuilder sb, Type type, string name, string key)
        {
            if (type.Namespace.Contains("System.Collections"))
            {
                if (type.Name.Contains("List"))
                {
                    if (type.GenericTypeArguments.Length > 0)
                    {
                        Type   argType  = type.GenericTypeArguments[0];
                        string typeName = $"List [{argType.Name}]";
                        sb.AppendLine($"| &nbsp; | | {name} [{typeName}] | {SwaggerCustomDescription.GetComments(key)} |");
                    }
                }
                else if (type.Name.Contains("Dictionary"))
                {
                    if (type.GenericTypeArguments.Length > 1)
                    {
                        Type   argTypeKey   = type.GenericTypeArguments[0];
                        Type   argTypeValue = type.GenericTypeArguments[1];
                        string typeName     = string.Empty;

                        if (argTypeValue.Namespace.Contains("System.Collections") && argTypeValue.Name.Contains("List"))
                        {
                            if (argTypeValue.GenericTypeArguments.Length > 0)
                            {
                                Type argType = argTypeValue.GenericTypeArguments[0];
                                typeName = $"Dictionary <{argTypeKey.Name},List [{argType.Name}]>";
                            }
                        }
                        else
                        {
                            typeName = $"Dictionary <{argTypeKey.Name},{argTypeValue.Name}>";
                        }
                        sb.AppendLine($"| &nbsp; | | {name} [{typeName}] | {SwaggerCustomDescription.GetComments(key)} |");
                    }
                }
            }
            else
            {
                sb.AppendLine($"| &nbsp; | | {name} [{type.Name}] | {SwaggerCustomDescription.GetComments(key)} |");
            }
        }
Exemple #4
0
        private static StringBuilder MakeTableDescription(Type type, StringBuilder sb)
        {
            sb.AppendLine($"## {type.FullName} - {SwaggerCustomDescription.GetComments($"{type.FullName}")} ##");
            sb.AppendLine("| Variable Name | Data Type | Data Detail | Description |");
            sb.AppendLine("| --- | --- | --- | --- |");

            foreach (var field in type.GetFields())
            {
                string key = $"{type.Namespace}.{type.Name}.{field.Name}";
                AppendDataLine(sb, field.FieldType, field.Name, key);
            }

            foreach (var properties in type.GetProperties())
            {
                string key = $"{type.Namespace}.{type.Name}.{properties.Name}";
                AppendDataLine(sb, properties.PropertyType, properties.Name, key);
            }

            return(sb);
        }
        public static IApplicationBuilder UseCustomSwagger(this IApplicationBuilder app)
        {
            app.UseSwagger(options =>
            {
                options.PreSerializeFilters.Add((swaggerDoc, httpReq) =>
                {
                    swaggerDoc.Host = httpReq.Host.Value;
                    IPAddress addr  = httpReq.HttpContext.Connection.RemoteIpAddress;
                });
            });
            app.UseSwaggerUI(c =>
            {
                c.SwaggerEndpoint(v1Path, "v1 Documents");
                c.SwaggerEndpoint(adminPath, "admin Documents");
            });

            SwaggerCustomDescription.Initialize();

            return(app);
        }