Esempio n. 1
0
        private static void ProcessTypeAttributes(Type definitionType, DefinitionSchema schema)
        {
            DescriptionAttribute descAttr = definitionType.GetCustomAttribute <DescriptionAttribute>();

            if (descAttr != null)
            {
                schema.Description = descAttr.Description;
            }

            SwaggerWcfDefinitionAttribute definitionAttr =
                definitionType.GetCustomAttribute <SwaggerWcfDefinitionAttribute>();

            if (definitionAttr != null)
            {
                if (!string.IsNullOrWhiteSpace(definitionAttr.ExternalDocsDescription) ||
                    !string.IsNullOrWhiteSpace(definitionAttr.ExternalDocsUrl))
                {
                    schema.ExternalDocumentation = new ExternalDocumentation
                    {
                        Description = definitionAttr.ExternalDocsDescription,
                        Url         = definitionAttr.ExternalDocsUrl
                    };
                }
            }
        }
        public static void ProcessProperties(Type definitionType, DefinitionSchema schema, IList <string> hiddenTags,
                                             Stack <Type> typesStack)
        {
            PropertyInfo[] properties = definitionType.GetProperties();

            foreach (PropertyInfo propertyInfo in properties)
            {
                DefinitionProperty prop = null;
                try
                {
                    prop = ProcessProperty(propertyInfo, hiddenTags, typesStack);
                }
                catch
                {
                    continue;
                }

                if (prop == null)
                {
                    continue;
                }

                if (prop.TypeFormat.Type == ParameterType.Array)
                {
                    Type propType = propertyInfo.PropertyType;

                    Type t = propType.GetElementType() ?? DefinitionsBuilder.GetEnumerableType(propType);

                    if (t != null)
                    {
                        //prop.TypeFormat = new TypeFormat(prop.TypeFormat.Type, HttpUtility.HtmlEncode(t.FullName));
                        prop.TypeFormat = new TypeFormat(prop.TypeFormat.Type, null);

                        TypeFormat st = Helpers.MapSwaggerType(t);
                        if (st.Type == ParameterType.Array || st.Type == ParameterType.Object)
                        {
                            prop.Items.TypeFormat = new TypeFormat(ParameterType.Unknown, null);
                            prop.Items.Ref        = t.GetModelName();
                        }
                        else
                        {
                            prop.Items.TypeFormat = st;
                        }
                    }
                }

                if (prop.Required)
                {
                    if (schema.Required == null)
                    {
                        schema.Required = new List <string>();
                    }

                    schema.Required.Add(prop.Title);
                }
                schema.Properties.Add(prop);
            }
        }
        private static Definition ConvertTypeToDefinition(Type definitionType, IList <string> hiddenTags,
                                                          Stack <Type> typesStack)
        {
            DefinitionSchema schema = new DefinitionSchema
            {
                Name = definitionType.FullName
            };

            ProcessTypeAttributes(definitionType, schema);

            // process
            schema.TypeFormat = Helpers.MapSwaggerType(definitionType, null);


            if (schema.TypeFormat.IsPrimitiveType)
            {
                return(null);
            }


            if (schema.TypeFormat.Type == ParameterType.String && schema.TypeFormat.Format == "enum")
            {
                schema.Enum = new List <string>();

                Type propType = definitionType;

                if (propType.IsGenericType && propType.GetGenericTypeDefinition() == typeof(Nullable <>))
                {
                    propType = propType.GetEnumerableType();
                }

                List <string> listOfEnumNames = propType.GetEnumNames().ToList();
                foreach (string enumName in listOfEnumNames)
                {
                    schema.Enum.Add(GetEnumMemberValue(propType, enumName));
                }
            }
            else if (schema.TypeFormat.Type == ParameterType.Array)
            {
                Type t = GetEnumerableType(definitionType);

                if (t != null)
                {
                    schema.Ref = t.FullName;
                    typesStack.Push(t);
                }
            }
            else
            {
                ProcessProperties(definitionType, schema, hiddenTags, typesStack);
            }

            return(new Definition
            {
                Schema = schema
            });
        }
Esempio n. 4
0
        private static void ProcessProperties(Type definitionType, DefinitionSchema schema, IList <string> hiddenTags,
                                              Stack <Type> typesStack)
        {
            PropertyInfo[] properties = definitionType.GetProperties();
            schema.Properties = new List <DefinitionProperty>();

            foreach (PropertyInfo propertyInfo in properties)
            {
                DefinitionProperty prop = ProcessProperty(propertyInfo, hiddenTags, typesStack);

                if (prop == null)
                {
                    continue;
                }

                if (prop.TypeFormat.Type == ParameterType.Array)
                {
                    Type propType = propertyInfo.PropertyType;

                    Type t = propType.GetElementType() ?? GetEnumerableType(propType);

                    if (t != null)
                    {
                        //prop.TypeFormat = new TypeFormat(prop.TypeFormat.Type, HttpUtility.HtmlEncode(t.FullName));
                        prop.TypeFormat = new TypeFormat(prop.TypeFormat.Type, null);

                        prop.Items.TypeFormat = new TypeFormat(ParameterType.Unknown, null);
                        prop.Items.Ref        = t.FullName;
                    }
                }

                if (prop.Required)
                {
                    if (schema.Required == null)
                    {
                        schema.Required = new List <string>();
                    }

                    schema.Required.Add(prop.Title);
                }
                schema.Properties.Add(prop);
            }
        }
Esempio n. 5
0
        private static Definition ConvertTypeToDefinition(Type definitionType, IList <string> hiddenTags,
                                                          Stack <Type> typesStack)
        {
            DefinitionSchema schema = new DefinitionSchema();

            //Get DataContractAttribute to set Schema name to match TypeFormat
            DataContractAttribute dca = definitionType.GetCustomAttribute <DataContractAttribute>();

            //If DataContractAttribute.Name is set use that, otherwise use definitionType.Name
            if (dca != null)
            {
                if (!String.IsNullOrWhiteSpace(dca.Name))
                {
                    schema.Name = dca.Name;
                }
                else
                {
                    schema.Name = definitionType.Name;
                }
            }

            ProcessTypeAttributes(definitionType, schema);

            // process
            schema.TypeFormat = Helpers.MapSwaggerType(definitionType, null);

            if (schema.TypeFormat.IsPrimitiveType)
            {
                return(null);
            }

            if (schema.TypeFormat.Type == ParameterType.String && schema.TypeFormat.Format == "enum")
            {
                schema.Enum = new List <string>();

                Type propType = definitionType;

                if (propType.IsGenericType && propType.GetGenericTypeDefinition() == typeof(Nullable <>))
                {
                    propType = propType.GetEnumerableType();
                }

                List <string> listOfEnumNames = propType.GetEnumNames().ToList();
                foreach (string enumName in listOfEnumNames)
                {
                    schema.Enum.Add(GetEnumMemberValue(propType, enumName));
                }
            }
            else if (schema.TypeFormat.Type == ParameterType.Array)
            {
                Type t = GetEnumerableType(definitionType);

                if (t != null)
                {
                    schema.Ref = t.FullName;
                    typesStack.Push(t);
                }
            }
            else
            {
                ProcessProperties(definitionType, schema, hiddenTags, typesStack);
            }

            return(new Definition
            {
                Schema = schema
            });
        }
        private static void ProcessProperties(Type definitionType, DefinitionSchema schema, IList <string> hiddenTags,
                                              Stack <Type> typesStack)
        {
            List <PropertyInfo> properties = new List <PropertyInfo>();
            var type = definitionType;

            while (type != null)
            {
                var props = type.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
                foreach (var prop in props)
                {
                    if (properties.Any(p => p.Name == prop.Name))
                    {
                        continue;
                    }

                    properties.Add(prop);
                }

                type = type.BaseType;
            }


            //PropertyInfo[] properties = definitionType.GetProperties(BindingFlags.Instance | BindingFlags.FlattenHierarchy | BindingFlags.Public | BindingFlags.NonPublic);
            schema.Properties = new List <DefinitionProperty>();

            foreach (PropertyInfo propertyInfo in properties)
            {
                DefinitionProperty prop = ProcessProperty(propertyInfo, hiddenTags, typesStack);

                if (prop == null)
                {
                    continue;
                }

                if (prop.TypeFormat.Type == ParameterType.Array)
                {
                    Type propType = propertyInfo.PropertyType;

                    Type t = propType.GetElementType() ?? GetEnumerableType(propType);

                    if (t != null)
                    {
                        //prop.TypeFormat = new TypeFormat(prop.TypeFormat.Type, HttpUtility.HtmlEncode(t.FullName));
                        prop.TypeFormat = new TypeFormat(prop.TypeFormat.Type, null);

                        TypeFormat st = Helpers.MapSwaggerType(t);
                        if (st.Type == ParameterType.Array || st.Type == ParameterType.Object)
                        {
                            prop.Items.TypeFormat = new TypeFormat(ParameterType.Unknown, null);
                            prop.Items.Ref        = t.FullName;
                        }
                        else
                        {
                            prop.Items.TypeFormat = st;
                        }
                    }
                }

                if (prop.Required)
                {
                    if (schema.Required == null)
                    {
                        schema.Required = new List <string>();
                    }

                    schema.Required.Add(prop.Title);
                }
                schema.Properties.Add(prop);
            }
        }
Esempio n. 7
0
        private static Definition ConvertTypeToDefinition(Type definitionType, IList <string> hiddenTags,
                                                          Stack <Type> typesStack)
        {
            DefinitionSchema schema = new DefinitionSchema
            {
                Name = definitionType.GetModelName()
            };

            ProcessTypeAttributes(definitionType, schema);

            // process
            schema.TypeFormat = Helpers.MapSwaggerType(definitionType, null);

            if (schema.TypeFormat.IsPrimitiveType)
            {
                return(null);
            }

            if (schema.TypeFormat.Type == ParameterType.Integer && schema.TypeFormat.Format == "enum")
            {
                schema.Enum = new List <int>();

                Type propType = definitionType;

                if (propType.IsGenericType && propType.GetGenericTypeDefinition() == typeof(Nullable <>))
                {
                    propType = propType.GetEnumerableType();
                }

                string        enumDescription = "";
                List <string> listOfEnumNames = propType.GetEnumNames().ToList();
                foreach (string enumName in listOfEnumNames)
                {
                    var enumMemberValue = DefinitionsBuilder.GetEnumMemberValue(propType, enumName);
                    var enumMemberItem  = Enum.Parse(propType, enumName, true);

                    string enumMemberDescription = DefinitionsBuilder.GetEnumDescription((Enum)enumMemberItem);
                    enumMemberDescription = (string.IsNullOrWhiteSpace(enumMemberDescription)) ? "" : $"({enumMemberDescription})";

                    if (schema.Description != null)
                    {
                        enumDescription += $"{Environment.NewLine}* `{enumMemberValue}` - {enumName} {enumMemberDescription}";
                        schema.Enum.Add(enumMemberValue);
                    }
                }

                if (schema.Description != null && enumDescription != "")
                {
                    schema.Description += $":{Environment.NewLine}{enumDescription}";
                }
            }
            else if (schema.TypeFormat.Type == ParameterType.Array)
            {
                Type t = GetEnumerableType(definitionType);

                if (t != null)
                {
                    schema.Ref = definitionType.GetModelName();
                    typesStack.Push(t);
                }
            }
            else
            {
                schema.Properties = new List <DefinitionProperty>();
                TypePropertiesProcessor.ProcessProperties(definitionType, schema, hiddenTags, typesStack);
                TypeFieldsProcessor.ProcessFields(definitionType, schema, hiddenTags, typesStack);
            }

            return(new Definition
            {
                Schema = schema
            });
        }