Esempio n. 1
0
        private static string ResolveCategoryAuth(MetaFolder categoryFolder)
        {
            var categoryAuth = categoryFolder.item.Where(x => !string.IsNullOrWhiteSpace(x.request?.auth))
                               .Select(x => x.request.auth).Distinct();

            return(string.Join(",", categoryAuth));
        }
Esempio n. 2
0
        public void Populate(string baseUri, MetaCollection collection, IServiceProvider serviceProvider)
        {
            var foldersByType      = new Dictionary <TypeInfo, MetaFolder>();
            var foldersByGroupName = new Dictionary <string, List <MetaFolder> >();

            // var rootVersion = _explorer.ApiDescriptionGroups.Version;

            var groupNames = _explorer.ApiDescriptionGroups.Items.Where(x => !string.IsNullOrWhiteSpace(x.GroupName))
                             .SelectMany(x => x.GroupName.Contains(",")
                                        ? x.GroupName.Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries)
                                        : new[] { x.GroupName }).Distinct().ToList();

            var groupFolders = groupNames.Select(x => new MetaFolder
            {
                name        = x,
                description = new MetaDescription {
                    content = "", type = "", version = null
                },
                variable = new List <dynamic>(),
                item     = new List <MetaItem>(),
                @event   = new List <dynamic>(),
                auth     = null,
                protocolProfileBehavior = new { }
            }).ToDictionary(k => k.name, v => v);

            //
            // Map all folders:
            foreach (var descriptionGroup in _explorer.ApiDescriptionGroups.Items)
            {
                var groupName      = descriptionGroup.GroupName;
                var orderedActions = descriptionGroup.Items.OrderBy(x => x.RelativePath).ThenBy(x => x.HttpMethod);

                foreach (var description in orderedActions)
                {
                    var controllerDescriptor = (ControllerActionDescriptor)description.ActionDescriptor;
                    var controllerTypeInfo   = controllerDescriptor.ControllerTypeInfo;

                    if (!ResolveControllerFeatureEnabled(controllerTypeInfo.AsType(), serviceProvider))
                    {
                        continue;
                    }

                    var controllerName = ResolveControllerName(controllerTypeInfo);

                    var item = CreateOperationMetaItem(baseUri, description, serviceProvider);

                    if (!foldersByType.TryGetValue(controllerTypeInfo, out var folder))
                    {
                        foldersByType.Add(controllerTypeInfo,
                                          folder = new MetaFolder
                        {
                            name        = controllerName,
                            description = new MetaDescription
                            {
                                content = ResolveControllerDescription(controllerTypeInfo)?.Content,
                                type    = ResolveControllerDescription(controllerTypeInfo)?.MediaType,
                                version = null
                            },
                            variable = new List <dynamic>(),
                            item     = new List <MetaItem>(),
                            @event   = new List <dynamic>(),
                            auth     = ResolveAuth(controllerDescriptor),
                            protocolProfileBehavior = new { }
                        });
                    }

                    folder.item.Add(item);

                    if (groupName == null)
                    {
                        continue;
                    }

                    var groups = groupName.Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                    foreach (var group in groups)
                    {
                        if (!foldersByGroupName.TryGetValue(group, out var list))
                        {
                            foldersByGroupName.Add(group, list = new List <MetaFolder>());
                        }
                        if (list.Contains(folder))
                        {
                            continue;
                        }
                        list.Add(folder);
                    }
                }
            }

            //
            // Create folder hierarchy:
            var roots      = new List <MetaFolder>();
            var categories = new Dictionary <MetaCategoryAttribute, List <MetaFolder> >();

            foreach (var folder in foldersByType)
            {
                var controllerType = folder.Key;
                var category       = ResolveControllerCategory(controllerType);
                if (category != null)
                {
                    if (!categories.TryGetValue(category, out var list))
                    {
                        categories.Add(category, list = new List <MetaFolder>());
                    }

                    // Does this folder belong to a group?
                    var inGroup = false;
                    foreach (var groupName in groupNames)
                    {
                        if (!foldersByGroupName[groupName].Contains(folder.Value))
                        {
                            continue;
                        }

                        var groupFolder = groupFolders[groupName];
                        groupFolder.item.Add(folder.Value);

                        if (!list.Contains(groupFolder))
                        {
                            list.Add(groupFolder);
                        }

                        inGroup = true;
                        break;
                    }

                    if (!inGroup)
                    {
                        list.Add(folder.Value);
                    }
                }
                else
                {
                    roots.Add(folder.Value);
                }
            }

            //
            // Add category folders:
            foreach (var entry in categories)
            {
                var category = entry.Key;

                var folderExists = collection.TryGetFolder(category.Name, out var categoryFolder);
                if (!folderExists)
                {
                    categoryFolder = new MetaFolder
                    {
                        name        = category.Name,
                        description =
                            new MetaDescription
                        {
                            content = category.Description, type = category.DescriptionMediaType, version = null
                        },
                        variable = new List <dynamic>(),
                        item     = new List <MetaItem>(),
                        @event   = new List <dynamic>(),
                        protocolProfileBehavior = new { }
                    };
                    collection.item.Add(categoryFolder);
                }

                foreach (var subFolder in entry.Value.OrderBy(x => x.name))
                {
                    categoryFolder.item.Add(subFolder);
                }

                categoryFolder.auth = ResolveCategoryAuth(categoryFolder);
            }

            //
            // Add root level folders:
            foreach (var folder in roots.OrderBy(x => x.name))
            {
                collection.item.Add(folder);
            }

            //
            // Change group name folder meta:
            foreach (var groupFolder in groupFolders)
            {
                var revisionName = groupFolder.Value.name;
                groupFolder.Value.name = $"Revision {revisionName}";

                if (_versionProvider.Enabled)
                {
                    foreach (var objectGroup in groupFolder.Value.item.OfType <MetaFolder>())
                    {
                        foreach (var item in objectGroup.item)
                        {
                            item.request.url.query ??= (item.request.url.query = new List <MetaParameter>());
                            item.request.url.query.Add(new MetaParameter
                            {
                                key         = _versionProvider.VersionParameter,
                                value       = revisionName,
                                description = "Sets the version revision number for this API request."
                                              // MetaDescription.PlainText("Sets the version revision number for this API request.")
                            });
                        }
                    }
                }
            }
        }