Beispiel #1
0
        public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context)
        {
            var pathString = hcx.HttpContext.Request.Path;

            if (pathString.Value.Contains("/redoc/"))
            {
                swaggerDoc.Info.Title   = docOptions.Title;
                swaggerDoc.Info.Version = docOptions.Version;
                swaggerDoc.Tags         = swaggerDoc.Tags ?? new List <OpenApiTag>();
                var existingTags = new List <string>(swaggerDoc.Tags.Select(x => x.Name));
                swaggerDoc.Info.Description = null;
                var markdown  = markdownHandler.GetMarkdownDocuments();
                var tagGroups = new List <TagGroup>();
                foreach (var folder in markdown.OfType <WikiMarkdownFolder>())
                {
                    tagGroups.Add(new TagGroup
                    {
                        Name = folder.Name,
                        Tags = folder.Select(x => x.Name).ToOpenApiArray()
                    });

                    foreach (var file in folder)
                    {
                        var tag = new OpenApiTag
                        {
                            Name        = file.Name,
                            Description = file.Content
                        };
                        swaggerDoc.Tags.Add(tag);
                    }
                }

                if (markdown.OfType <WikiMarkdownFile>().Any())
                {
                    var miscTagGroup = new TagGroup {
                        Name = options.WikiRootFilesFolderName, Tags = new OpenApiArray()
                    };
                    tagGroups.Add(miscTagGroup);
                    foreach (var file in markdown.OfType <WikiMarkdownFile>())
                    {
                        var tag = new OpenApiTag
                        {
                            Name        = file.Name,
                            Description = file.Content
                        };
                        miscTagGroup.Tags.Add(new OpenApiString(file.Name));
                        swaggerDoc.Tags.Add(tag);
                    }
                }

                var additions = options.AdditionalControllersToInclude.Except(existingTags, System.StringComparer.OrdinalIgnoreCase).ToList();
                existingTags.AddRange(additions);
                tagGroups.Add(
                    new TagGroup
                {
                    Name = options.ApiReferenceGroupName,
                    Tags = existingTags.ToOpenApiArray()
                });

                swaggerDoc.Extensions.Add("x-tagGroups", tagGroups.ToOpenApiArray());

                if (!swaggerDoc.Info.Extensions.ContainsKey("x-logo") && !string.IsNullOrWhiteSpace(options.LogoUrl))
                {
                    swaggerDoc.Info.Extensions.Add("x-logo", new Logo
                    {
                        AltText         = options.LogoAltText,
                        BackgroundColor = options.LogoBackgroundColor,
                        Url             = options.LogoUrl
                    });
                }
            }
        }
Beispiel #2
0
        public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context)
        {
            var pathString = hcx.HttpContext.Request.Path;

            if (pathString.Value.Contains("/redoc/"))
            {
                swaggerDoc.Info.Title   = docOptions.Title;
                swaggerDoc.Info.Version = docOptions.Version;
                swaggerDoc.Tags         = swaggerDoc.Tags ?? new List <OpenApiTag>();
                var existingTags = new List <string>(swaggerDoc.Tags.Select(x => x.Name));
                swaggerDoc.Info.Description = null;
                var markdown = markdownHandler.GetMarkdownDocuments();
                var tagGroup = new TagGroup
                {
                    Name = options.WikiGroupName,
                    Tags = new OpenApiArray()
                };
                foreach (var node in markdown)
                {
                    var content = new StringBuilder();
                    switch (node)
                    {
                    case WikiMarkdownFolder folder:
                        tagGroup.Tags.Add(new OpenApiString(folder.Name));
                        foreach (var file in folder.Where(x => x.Name == folder.Name))
                        {
                            content.AppendLine(file.Content);
                            content.AppendLine();
                            content.AppendLine();
                        }

                        foreach (var file in folder.Where(x => x.Name != folder.Name))
                        {
                            content.AppendLine($"# {file.Name}");
                            content.AppendLine(file.Content);
                            content.AppendLine();
                            content.AppendLine();
                        }
                        swaggerDoc.Tags.Add(new OpenApiTag
                        {
                            Name        = folder.Name,
                            Description = content.ToString()
                        });
                        break;

                    case WikiMarkdownFile rootFile:
                        tagGroup.Tags.Add(new OpenApiString(rootFile.Name));
                        content.AppendLine(rootFile.Content);
                        content.AppendLine();
                        content.AppendLine();
                        swaggerDoc.Tags.Add(new OpenApiTag
                        {
                            Name        = rootFile.Name,
                            Description = content.ToString()
                        });
                        break;
                    }
                }

                var additions = options.AdditionalControllersToInclude.Except(existingTags, System.StringComparer.OrdinalIgnoreCase).ToList();
                existingTags.AddRange(additions);

                swaggerDoc.Extensions.Add("x-tagGroups", new OpenApiArray
                {
                    tagGroup,
                    new TagGroup
                    {
                        Name = options.ApiReferenceGroupName,
                        Tags = existingTags.ToOpenApiArray()
                    }
                });

                if (!swaggerDoc.Info.Extensions.ContainsKey("x-logo") && !string.IsNullOrWhiteSpace(options.LogoUrl))
                {
                    swaggerDoc.Info.Extensions.Add("x-logo", new Logo
                    {
                        AltText         = options.LogoAltText,
                        BackgroundColor = options.LogoBackgroundColor,
                        Url             = options.LogoUrl
                    });
                }
            }
        }