Exemplo n.º 1
0
        private static List <Service> BuildService()
        {
            const string      sectionName = "swaggerwcf";
            SwaggerWcfSection config      =
                (SwaggerWcfSection)(ConfigurationManager.GetSection(sectionName) ?? new SwaggerWcfSection());

            List <string> hiddenTags  = GetHiddenTags(config);
            List <string> visibleTags = GetVisibleTags(config);
            IReadOnlyDictionary <string, string> settings = GetSettings(config);

            var serviceInfos = BuildPaths(hiddenTags);
            var result       = new List <Service>();

            foreach (var serviceInfo in serviceInfos)
            {
                var service = new Service();

                ProcessSettings(service, settings);
                service.Paths       = serviceInfo.Value.Paths;
                service.Definitions = DefinitionsBuilder.Process(hiddenTags, visibleTags,
                                                                 serviceInfo.Value.DefinitionsTypesList);
                service.Name = serviceInfo.Value.Name ?? string.Empty;

                result.Add(service);
            }

            return(result);
        }
        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);
            }
        }
Exemplo n.º 3
0
        private static Service BuildService()
        {
            const string      sectionName = "swaggerwcf";
            SwaggerWcfSection config      =
                (SwaggerWcfSection)(ConfigurationManager.GetSection(sectionName) ?? new SwaggerWcfSection());
            List <Type>   definitionsTypesList = new List <Type>();
            Service       service    = new Service();
            List <string> hiddenTags = GetHiddenTags(config);
            IReadOnlyDictionary <string, string> settings = GetSettings(config);

            ProcessSettings(service, settings);

            BuildPaths(service, hiddenTags, definitionsTypesList);

            service.Definitions = DefinitionsBuilder.Process(hiddenTags, definitionsTypesList);

            return(service);
        }
Exemplo n.º 4
0
        private static SwaggerSchema BuildServiceCommon(string path, Action <SwaggerSchema, IList <string>, List <string>, IList <Type> > buildPaths)
        {
            const string      sectionName = "swaggerwcf";
            SwaggerWcfSection config      =
                (SwaggerWcfSection)(ConfigurationManager.GetSection(sectionName) ?? new SwaggerWcfSection());

            List <Type>   definitionsTypesList = new List <Type>();
            SwaggerSchema service     = new SwaggerSchema();
            List <string> hiddenTags  = SwaggerWcfEndpoint.FilterHiddenTags(path, GetHiddenTags(config));
            List <string> visibleTags = SwaggerWcfEndpoint.FilterVisibleTags(path, GetVisibleTags(config));
            IReadOnlyDictionary <string, string> settings = GetSettings(config);

            ProcessSettings(service, settings);

            buildPaths(service, hiddenTags, visibleTags, definitionsTypesList);

            service.Definitions = DefinitionsBuilder.Process(hiddenTags, visibleTags, definitionsTypesList);

            return(service);
        }
Exemplo n.º 5
0
        private static Service BuildServiceCommon(string path, Action <Service, IList <string>, List <TagElement>, IList <Type> > buildPaths)
        {
            const string      sectionName = "swaggerwcf";
            SwaggerWcfSection config      =
                (SwaggerWcfSection)(ConfigurationManager.GetSection(sectionName) ?? new SwaggerWcfSection());

            List <Type> definitionsTypesList = new List <Type>();
            Service     service     = new Service();
            var         hiddenTags  = GetHiddenTags(config);
            var         visibleTags = GetVisibleTags(config);
            IReadOnlyDictionary <string, string> settings = GetSettings(config);

            ProcessSettings(service, settings);

            buildPaths(service, hiddenTags, visibleTags, definitionsTypesList);

            service.Definitions = DefinitionsBuilder.Process(hiddenTags, visibleTags, definitionsTypesList);

            var distinctTags = service.Paths.SelectMany(p => p.Actions).SelectMany(act => act.Tags)
                               .Distinct();


            var tagsToOrder = distinctTags.Select(tagName =>
            {
                // If tag listed as visible, try to take Description and SortOrder from there
                var existingVisibleDef = visibleTags.FirstOrDefault(vt => vt.Name == tagName);
                return(new TagDeffinition
                {
                    Name = tagName,
                    Description = existingVisibleDef?.Description ?? string.Empty,
                    SortOrder = existingVisibleDef?.SortOrder ?? 0
                });
            });

            service.Tags = tagsToOrder.OrderBy(t => t.SortOrder)
                           .ThenBy(t => t.Name)
                           .ToList();

            return(service);
        }
        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);
        }
Exemplo 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
            });
        }
Exemplo n.º 8
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>();
            var useDataMember = SwaggerWcfEndpoint.NotUseDataMemberAttributeExceptedType.Contains(propertyInfo.ReflectedType) || !SwaggerWcfEndpoint.NotUseDataMemberAttribute;

            if (useDataMember && 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.PropertyType.IsGenericType &&
                propertyInfo.PropertyType.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.PropertyType);

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

                return(prop);
            }

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

                    if (subType == typeof(byte) || subType == typeof(sbyte))
                    {
                        prop.TypeFormat = subTypeFormat;
                    }
                    else
                    {
                        if (subTypeFormat.Type == ParameterType.Object)
                        {
                            typesStack.Push(subType);
                        }

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

            if (prop.TypeFormat.Format == "enum")
            {
                prop.Enum = new Dictionary <string, int>();

                Type propType = propertyInfo.PropertyType;

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

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

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

            return(prop);
        }