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);
            }
        }
Пример #2
0
        public static string GetDefinitionPropertyDescription(DefinitionProperty definitionProperty)
        {
            string description = ContactDescription(definitionProperty.Title, definitionProperty.Description);

            if (!string.IsNullOrEmpty(description))
            {
                return(description);
            }
            else if (definitionProperty.DefinitionObjectType == DefinitionObjectType.Array)
            {
                var subDescription = ContactDescription(definitionProperty.SubTitle, definitionProperty.SubDescription);
                if (!string.IsNullOrEmpty(subDescription))
                {
                    return(subDescription);
                }
            }
            return(string.Empty);
        }
Пример #3
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);
            }
        }
Пример #4
0
        public static void ApplyAttributeOptions(FieldInfo fieldInfo, DefinitionProperty prop)
        {
            // Use the DataContract [DefaultValue] as the default, by default
            var defAttr = fieldInfo.GetCustomAttributes <DefaultValueAttribute>().LastOrDefault();

            if (defAttr != null)
            {
                prop.Default = defAttr.Value.ToString();
            }

            // Apply any [SwaggerWcfProperty]s in order.
            var attrs = fieldInfo.GetCustomAttributes <SwaggerWcfPropertyAttribute>().ToList();

            if (!attrs.Any())
            {
                return;
            }
            ApplyAttributeOptions(attrs, prop);
        }
Пример #5
0
        private static void ApplyAttributeOptions(PropertyInfo propertyInfo, DefinitionProperty prop)
        {
            // Use the DataContract [DefaultValue] as the default, by default
            var defAttr = propertyInfo.GetCustomAttributes <DefaultValueAttribute>().LastOrDefault();

            if (defAttr != null)
            {
                prop.Default = defAttr.Value.ToString();
            }

            // Apply any [SwaggerWcfProperty]s in order.
            var attrs = propertyInfo.GetCustomAttributes <SwaggerWcfPropertyAttribute>().ToList();

            if (!attrs.Any())
            {
                return;
            }

            ApplyIfValid(LastValidValue(attrs, a => a.Title), x => prop.Title             = x);
            ApplyIfValid(LastValidValue(attrs, a => a.Description), x => prop.Description = x);
            ApplyIfValid(LastValidValue(attrs, a => a._Required), x => prop.Required      = x.Value);
            //ApplyIfValid(LastValidValue(attrs, a => a._AllowEmptyValue),  x => prop.AllowEmptyValue  = x.Value);
            //ApplyIfValid(LastValidValue(attrs, a => a._CollectionFormat), x => prop.CollectionFormat = x.Value);
            ApplyIfValid(LastValidValue(attrs, a => a.Default), x => prop.Default  = x);
            ApplyIfValid(LastValidValue(attrs, a => a.Example), x => prop.Example  = x);
            ApplyIfValid(LastValidValue(attrs, a => a._Maximum), x => prop.Maximum = x.Value);
            ApplyIfValid(LastValidValue(attrs, a => a._ExclusiveMaximum), x => prop.ExclusiveMaximum = x.Value);
            ApplyIfValid(LastValidValue(attrs, a => a._Minimum), x => prop.Minimum = x.Value);
            ApplyIfValid(LastValidValue(attrs, a => a._ExclusiveMinimum), x => prop.ExclusiveMinimum = x.Value);
            ApplyIfValid(LastValidValue(attrs, a => a._MaxLength), x => prop.MaxLength     = x.Value);
            ApplyIfValid(LastValidValue(attrs, a => a._MinLength), x => prop.MinLength     = x.Value);
            ApplyIfValid(LastValidValue(attrs, a => a.Pattern), x => prop.Pattern          = x);
            ApplyIfValid(LastValidValue(attrs, a => a._MaxItems), x => prop.MaxItems       = x.Value);
            ApplyIfValid(LastValidValue(attrs, a => a._MinItems), x => prop.MinItems       = x.Value);
            ApplyIfValid(LastValidValue(attrs, a => a._UniqueItems), x => prop.UniqueItems = x.Value);
            ApplyIfValid(LastValidValue(attrs, a => a._MultipleOf), x => prop.MultipleOf   = x.Value);
        }
        private static DefinitionProperty ProcessField(FieldInfo propertyInfo, IList <string> hiddenTags,
                                                       Stack <Type> typesStack)
        {
            if (propertyInfo.GetCustomAttribute <SwaggerWcfHiddenAttribute>() != null ||
                propertyInfo.GetCustomAttributes <SwaggerWcfTagAttribute>()
                .Select(t => t.TagName)
                .Any(hiddenTags.Contains))
            {
                return(null);
            }

            TypeFormat typeFormat = Helpers.MapSwaggerType(propertyInfo.FieldType, null);

            DefinitionProperty prop = new DefinitionProperty {
                Title = propertyInfo.Name
            };

            DataMemberAttribute dataMemberAttribute = propertyInfo.GetCustomAttribute <DataMemberAttribute>();

            if (dataMemberAttribute != null)
            {
                if (!string.IsNullOrEmpty(dataMemberAttribute.Name))
                {
                    prop.Title = dataMemberAttribute.Name;
                }

                prop.Required = dataMemberAttribute.IsRequired;
            }

            // Special case - if it came out required, but we unwrapped a null-able type,
            // then it's necessarily not required.  Ideally this would only set the default,
            // but we can't tell the difference between an explicit declaration of
            // IsRequired =false on the DataMember attribute and no declaration at all.
            if (prop.Required && propertyInfo.FieldType.IsGenericType &&
                propertyInfo.FieldType.GetGenericTypeDefinition() == typeof(Nullable <>))
            {
                prop.Required = false;
            }

            DescriptionAttribute descriptionAttribute = propertyInfo.GetCustomAttribute <DescriptionAttribute>();

            if (descriptionAttribute != null)
            {
                prop.Description = descriptionAttribute.Description;
            }

            SwaggerWcfRegexAttribute regexAttr = propertyInfo.GetCustomAttribute <SwaggerWcfRegexAttribute>();

            if (regexAttr != null)
            {
                prop.Pattern = regexAttr.Regex;
            }

            prop.TypeFormat = typeFormat;

            if (prop.TypeFormat.Type == ParameterType.Object)
            {
                typesStack.Push(propertyInfo.FieldType);

                prop.Ref = propertyInfo.FieldType.GetModelName();

                return(prop);
            }

            if (prop.TypeFormat.Type == ParameterType.Array)
            {
                Type subType = DefinitionsBuilder.GetEnumerableType(propertyInfo.FieldType);
                if (subType != null)
                {
                    TypeFormat subTypeFormat = Helpers.MapSwaggerType(subType, null);

                    if (subTypeFormat.Type == ParameterType.Object)
                    {
                        typesStack.Push(subType);
                    }

                    prop.Items = new ParameterItems
                    {
                        TypeFormat = subTypeFormat
                    };
                }
            }

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

                Type propType = propertyInfo.FieldType;

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

                string        enumDescription = "";
                List <string> listOfEnumNames = propType.GetEnumNames().ToList();
                foreach (string enumName in listOfEnumNames)
                {
                    var    enumMemberItem        = Enum.Parse(propType, enumName, true);
                    string enumMemberDescription = DefinitionsBuilder.GetEnumDescription((Enum)enumMemberItem);
                    enumMemberDescription = (string.IsNullOrWhiteSpace(enumMemberDescription)) ? "" : $"({enumMemberDescription})";
                    int enumMemberValue = DefinitionsBuilder.GetEnumMemberValue(propType, enumName);
                    if (prop.Description != null)
                    {
                        prop.Enum.Add(enumMemberValue);
                    }
                    enumDescription += $"    {enumName}{System.Web.HttpUtility.HtmlEncode(" = ")}{enumMemberValue} {enumMemberDescription}\r\n";
                }

                if (enumDescription != "")
                {
                    prop.Description += $"\r\n\r\n{enumDescription}";
                }
            }

            // Apply any options set in a [SwaggerWcfProperty]
            DefinitionsBuilder.ApplyAttributeOptions(propertyInfo, prop);

            return(prop);
        }
Пример #7
0
        private static DefinitionProperty ProcessProperty(PropertyInfo propertyInfo, IList <string> hiddenTags,
                                                          Stack <Type> typesStack)
        {
            if (propertyInfo.GetCustomAttribute <SwaggerWcfHiddenAttribute>() != null ||
                propertyInfo.GetCustomAttributes <SwaggerWcfTagAttribute>()
                .Select(t => t.TagName)
                .Any(hiddenTags.Contains))
            {
                return(null);
            }

            TypeFormat typeFormat = Helpers.MapSwaggerType(propertyInfo.PropertyType, null);

            DefinitionProperty prop = new DefinitionProperty {
                Title = propertyInfo.Name
            };

            DataMemberAttribute dataMemberAttribute = propertyInfo.GetCustomAttribute <DataMemberAttribute>();

            if (dataMemberAttribute != null)
            {
                if (!string.IsNullOrEmpty(dataMemberAttribute.Name))
                {
                    prop.Title = dataMemberAttribute.Name;
                }

                prop.Required = dataMemberAttribute.IsRequired;
            }

            // Special case - if it came out required, but we unwrapped a null-able type,
            // then it's necessarily not required.  Ideally this would only set the default,
            // but we can't tell the difference between an explicit delaration of
            // IsRequired =false on the DataMember attribute and no declaration at all.
            if (prop.Required && propertyInfo.PropertyType.IsGenericType &&
                propertyInfo.PropertyType.GetGenericTypeDefinition() == typeof(Nullable <>))
            {
                prop.Required = false;
            }

            DescriptionAttribute descriptionAttribute = propertyInfo.GetCustomAttribute <DescriptionAttribute>();

            if (descriptionAttribute != null)
            {
                prop.Description = descriptionAttribute.Description;
            }

            prop.TypeFormat = typeFormat;

            if (prop.TypeFormat.Type == ParameterType.Object)
            {
                typesStack.Push(propertyInfo.PropertyType);

                prop.Ref = propertyInfo.PropertyType.FullName;

                return(prop);
            }

            if (prop.TypeFormat.Type == ParameterType.Array)
            {
                Type subType = GetEnumerableType(propertyInfo.PropertyType);
                if (subType != null)
                {
                    TypeFormat subTypeFormat = Helpers.MapSwaggerType(subType, null);

                    if (subTypeFormat.Type == ParameterType.Object)
                    {
                        typesStack.Push(subType);
                    }

                    prop.Items = new ParameterItems
                    {
                        TypeFormat = subTypeFormat
                    };
                }
            }

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

                Type propType = propertyInfo.PropertyType;

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

                List <string> listOfEnumNames = propType.GetEnumNames().ToList();
                foreach (string enumName in listOfEnumNames)
                {
                    prop.Enum.Add(GetEnumMemberValue(propType, enumName));
                }
            }

            // Apply any options set in a [SwaggerWcfProperty]
            ApplyAttributeOptions(propertyInfo, prop);

            return(prop);
        }
Пример #8
0
        private static DefinitionProperty ProcessProperty(PropertyInfo propertyInfo, IList <string> hiddenTags,
                                                          Stack <Type> typesStack)
        {
            if (propertyInfo.GetCustomAttribute <DataMemberAttribute>() == null ||
                propertyInfo.GetCustomAttribute <SwaggerWcfHiddenAttribute>() != null ||
                propertyInfo.GetCustomAttributes <SwaggerWcfTagAttribute>()
                .Select(t => t.TagName)
                .Any(hiddenTags.Contains))
            {
                return(null);
            }

            TypeFormat typeFormat = Helpers.MapSwaggerType(propertyInfo.PropertyType, null);

            DefinitionProperty prop = new DefinitionProperty {
                Title = propertyInfo.Name
            };

            DataMemberAttribute dataMemberAttribute = propertyInfo.GetCustomAttribute <DataMemberAttribute>();

            if (dataMemberAttribute != null)
            {
                if (!string.IsNullOrEmpty(dataMemberAttribute.Name))
                {
                    prop.Title = dataMemberAttribute.Name;
                }

                prop.Required = dataMemberAttribute.IsRequired;
            }

            DescriptionAttribute descriptionAttribute = propertyInfo.GetCustomAttribute <DescriptionAttribute>();

            if (descriptionAttribute != null)
            {
                prop.Description = descriptionAttribute.Description;
            }

            prop.TypeFormat = typeFormat;

            if (prop.TypeFormat.Type == ParameterType.Object)
            {
                typesStack.Push(propertyInfo.PropertyType);

                prop.Ref = propertyInfo.PropertyType.FullName;

                return(prop);
            }

            if (prop.TypeFormat.Type == ParameterType.Array)
            {
                Type subType = propertyInfo.PropertyType.GetEnumerableType();
                if (subType != null)
                {
                    TypeFormat subTypeFormat = Helpers.MapSwaggerType(subType, null);

                    if (subTypeFormat.Type == ParameterType.Object)
                    {
                        typesStack.Push(subType);
                    }

                    prop.Items = new ParameterItems
                    {
                        TypeFormat = subTypeFormat
                    };
                }
            }

            if (prop.TypeFormat.Type == ParameterType.String && prop.TypeFormat.Format == "enum")
            {
                prop.Enum = new List <string>();
                List <string> listOfEnumNames = propertyInfo.PropertyType.GetEnumNames().ToList();
                foreach (string enumName in listOfEnumNames)
                {
                    prop.Enum.Add(GetEnumMemberValue(propertyInfo.PropertyType, enumName));
                }
            }

            return(prop);
        }
        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);
            }
        }
Пример #10
0
 public static void ApplyAttributeOptions(IEnumerable <SwaggerWcfPropertyAttribute> attrs, DefinitionProperty prop)
 {
     ApplyIfValid(LastValidValue(attrs, a => a.Title), x => prop.Title             = x);
     ApplyIfValid(LastValidValue(attrs, a => a.Description), x => prop.Description = x);
     ApplyIfValid(LastValidValue(attrs, a => a._Required), x => prop.Required      = x.Value);
     //ApplyIfValid(LastValidValue(attrs, a => a._AllowEmptyValue),  x => prop.AllowEmptyValue  = x.Value);
     //ApplyIfValid(LastValidValue(attrs, a => a._CollectionFormat), x => prop.CollectionFormat = x.Value);
     ApplyIfValid(LastValidValue(attrs, a => a.Default), x => prop.Default  = x);
     ApplyIfValid(LastValidValue(attrs, a => a.Example), x => prop.Example  = x);
     ApplyIfValid(LastValidValue(attrs, a => a._Maximum), x => prop.Maximum = x.Value);
     ApplyIfValid(LastValidValue(attrs, a => a._ExclusiveMaximum), x => prop.ExclusiveMaximum = x.Value);
     ApplyIfValid(LastValidValue(attrs, a => a._Minimum), x => prop.Minimum = x.Value);
     ApplyIfValid(LastValidValue(attrs, a => a._ExclusiveMinimum), x => prop.ExclusiveMinimum = x.Value);
     ApplyIfValid(LastValidValue(attrs, a => a._MaxLength), x => prop.MaxLength     = x.Value);
     ApplyIfValid(LastValidValue(attrs, a => a._MinLength), x => prop.MinLength     = x.Value);
     ApplyIfValid(LastValidValue(attrs, a => a.Pattern), x => prop.Pattern          = x);
     ApplyIfValid(LastValidValue(attrs, a => a._MaxItems), x => prop.MaxItems       = x.Value);
     ApplyIfValid(LastValidValue(attrs, a => a._MinItems), x => prop.MinItems       = x.Value);
     ApplyIfValid(LastValidValue(attrs, a => a._UniqueItems), x => prop.UniqueItems = x.Value);
     ApplyIfValid(LastValidValue(attrs, a => a._MultipleOf), x => prop.MultipleOf   = x.Value);
 }
Пример #11
0
 set => SetValue(DefinitionProperty, value);