Example #1
0
        public static void TryToAddModels(ConcurrentDictionary<string, TypeInfo> models, Type type, XmlCommentDocumentationProvider docProvider, ConcurrentDictionary<string, string> typesToReturn = null, int level = 0)
        {
            var _type = type;
            if (type.IsArray)
                _type = type.GetElementType();
            else if (type.IsGenericType)
                _type = type.GetGenericArguments().First();

            string typeName = GetTypeName(_type);

            if (models.Any(m => m.Key == typeName)) return;
            
            if (IsOutputable(_type))
            {
                var typeInfo = new TypeInfo { id = typeName };
                if (!IgnoreTypes.Contains(_type.Name.ToLower()))
                {
                    typeInfo.description = docProvider.GetSummary(_type);
                }
                //Ignore properties for .net types
                if (!_type.Assembly.FullName.Contains("System") && !_type.Assembly.FullName.Contains("mscorlib"))
                {
                    var modelInfoDic = new Dictionary<string, PropInfo>();
                    foreach (var propertyInfo in _type.GetProperties(BindingFlags.Public | BindingFlags.Instance))
                    {
                        if (propertyInfo.GetCustomAttributes(typeof(JsonIgnoreAttribute), false).FirstOrDefault() != null)
                            continue;

                        var propInfo = new PropInfo();

                        string propName = GetPropertyName(propertyInfo);
                        Type propType = docProvider.GetType(propertyInfo);
                        SwaggerType swaggerType = GetSwaggerType(propType);
                        propInfo.type = swaggerType.Name;
                        propInfo.items = swaggerType.Items;
                        propInfo.required = IsRequired(propertyInfo, docProvider);


                        if (!modelInfoDic.Keys.Contains(propName))
                            modelInfoDic.Add(propName, propInfo);

                        if (!IgnoreTypes.Contains(propInfo.type))
                        {
                            propInfo.description = docProvider.GetSummary(propertyInfo);
                            if (propertyInfo.PropertyType.IsEnum)
                            {
                                modelInfoDic[propName].@enum = propertyInfo.PropertyType.GetEnumNames();
                            }
                            //Don't go too deep
                            if (level < 10)
                            {
                                TryToAddModels(models, swaggerType.type, docProvider, typesToReturn, ++level);
                            }
                        }
                    }
                    typeInfo.properties = modelInfoDic;
                }
                if (_type.IsEnum)
                {
                    typeInfo.values = _type.GetEnumNames();
                }

                models.TryAdd(typeName, typeInfo);
            }
        }
Example #2
0
        public static void TryToAddModels(ConcurrentDictionary <string, TypeInfo> models, Type type, XmlCommentDocumentationProvider docProvider, ConcurrentDictionary <string, string> typesToReturn = null, int level = 0)
        {
            var _type = type;

            if (type.IsArray)
            {
                _type = type.GetElementType();
            }
            else if (type.IsGenericType)
            {
                _type = type.GetGenericArguments().First();
            }

            string typeName = GetTypeName(_type);

            if (models.Any(m => m.Key == typeName))
            {
                return;
            }

            if (IsOutputable(_type))
            {
                var typeInfo = new TypeInfo {
                    id = typeName
                };
                if (!IgnoreTypes.Contains(_type.Name.ToLower()))
                {
                    typeInfo.description = docProvider.GetSummary(_type);
                }
                //Ignore properties for .net types
                if (!_type.Assembly.FullName.Contains("System") && !_type.Assembly.FullName.Contains("mscorlib"))
                {
                    var modelInfoDic = new Dictionary <string, PropInfo>();
                    foreach (var propertyInfo in _type.GetProperties(BindingFlags.Public | BindingFlags.Instance))
                    {
                        if (propertyInfo.GetCustomAttributes(typeof(JsonIgnoreAttribute), false).FirstOrDefault() != null)
                        {
                            continue;
                        }

                        var propInfo = new PropInfo();

                        string      propName    = GetPropertyName(propertyInfo);
                        Type        propType    = docProvider.GetType(propertyInfo);
                        SwaggerType swaggerType = GetSwaggerType(propType);
                        propInfo.type     = swaggerType.Name;
                        propInfo.items    = swaggerType.Items;
                        propInfo.required = IsRequired(propertyInfo, docProvider);


                        if (!modelInfoDic.Keys.Contains(propName))
                        {
                            modelInfoDic.Add(propName, propInfo);
                        }

                        if (!IgnoreTypes.Contains(propInfo.type))
                        {
                            propInfo.description = docProvider.GetSummary(propertyInfo);
                            if (propertyInfo.PropertyType.IsEnum)
                            {
                                modelInfoDic[propName].@enum = propertyInfo.PropertyType.GetEnumNames();
                            }
                            //Don't go too deep
                            if (level < 10)
                            {
                                TryToAddModels(models, swaggerType.type, docProvider, typesToReturn, ++level);
                            }
                        }
                    }
                    typeInfo.properties = modelInfoDic;
                }
                if (_type.IsEnum)
                {
                    typeInfo.values = _type.GetEnumNames();
                }

                models.TryAdd(typeName, typeInfo);
            }
        }