Git Product home page Git Product logo

hamazon.swagger.ui's People

Contributors

hemiaoio avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

hamazon.swagger.ui's Issues

关于Mvc Controller 的描述不显示问题处理

因为.net mvc 的Controller 都是有 Controller 后缀的,但是swagger.json并没有这个Controller后缀,而 Swashbuckle.AspNetCore 官方作者也没有做这个转换(也有可能涉及到自定义后缀),目前在这里自行处理记录一下:

  • CustomDocumentFilter.cs
using System;
using System.Collections.Generic;
using System.Linq;
using Swashbuckle.AspNetCore.Swagger;
using Swashbuckle.AspNetCore.SwaggerGen;
using System.Xml.XPath;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.Controllers;

namespace xxxxxx
{
    /// <summary>
    /// 
    /// </summary>
    public class SwaggerDocumentFilter : IDocumentFilter
    {
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly IEnumerable<XPathDocument> _xmlDocuments;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="xmlDocuments"></param>
        /// <param name="httpContextAccessor"></param>
        public SwaggerDocumentFilter(IEnumerable<XPathDocument> xmlDocuments, IHttpContextAccessor httpContextAccessor)
        {
            _httpContextAccessor = httpContextAccessor;
            _xmlDocuments = xmlDocuments;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="swaggerDoc"></param>
        /// <param name="context"></param>
        public void Apply(SwaggerDocument swaggerDoc, DocumentFilterContext context)
        {
            swaggerDoc.BasePath = "/";
            swaggerDoc.Schemes = new List<string> { _httpContextAccessor.HttpContext.Request.Scheme };
            swaggerDoc.Host = _httpContextAccessor.HttpContext.Request.Host.Value;
            //增加Controller说明信息
            swaggerDoc.Tags = new List<Tag>();
            foreach (var descriptionGroup in context.ApiDescriptionsGroups.Items)
            {
                var actionDescriptor = descriptionGroup.Items.First().ActionDescriptor;
                if (actionDescriptor is ControllerActionDescriptor)
                {
                    var controllerActionDescriptor = actionDescriptor as ControllerActionDescriptor;
                    var type = controllerActionDescriptor.ControllerTypeInfo.UnderlyingSystemType;
                    swaggerDoc.Tags.Add(new Tag
                    {
                        Name = descriptionGroup.GroupName,
                        Description = GetSummaryByType(type)
                    });
                }
            }
        }

        private const string MemberXPath = "/doc/members/member[@name='{0}']";
        private const string SummaryTag = "summary";

        /// <summary>
        /// 根据类型ID获取XML备注
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        private string GetSummaryByType(Type type)
        {
            var commentId = XmlCommentsIdHelper.GetCommentIdForType(type);
            foreach (var document in _xmlDocuments)
            {
                var xmlNavigator = document.CreateNavigator();
                var typeNode = xmlNavigator.SelectSingleNode(string.Format(MemberXPath, commentId));
                if (typeNode != null)
                {
                    var summaryNode = typeNode.SelectSingleNode(SummaryTag);
                    if (summaryNode != null)
                        return XmlCommentsTextHelper.Humanize(summaryNode.InnerXml);
                }
            }

            return string.Empty;
        }
    }
}
  • Startup.cs
public IServiceProvider ConfigureServices(IServiceCollection services){
    /******Your Self Services Register.........******/

   services.AddSwaggerGen(options =>
            {
                options.SwaggerDoc("v1", new Info
                {
                    Title = "Swagger API Document",
                    Version = "v1",
                    Contact = new Contact()
                    {
                        Email = "[email protected]",
                        Name = "Hemiao"
                    },
                    License = new License()
                    {
                        Name = "Apache 2.0",
                        Url = "http://www.apache.org/licenses/LICENSE-2.0.html"
                    }
                });
                options.AddSecurityDefinition(string.Empty, new BasicAuthScheme() { });
                options.DocInclusionPredicate((docName, description) => true);
                options.DescribeAllEnumsAsStrings();
                //取本项目相关的XML地址,如有其他需要添加的XML需要在这里进行修改
                var xmlFilePaths = Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory,
                    Assembly.GetExecutingAssembly().GetName().Name
                        .Substring(0, Assembly.GetExecutingAssembly().GetName().Name.IndexOf('.')) +
                    ".*.xml",
                    SearchOption.AllDirectories);

                //将XML转成 XPathDocument
                var documents = xmlFilePaths.Select(path => new XPathDocument(path));
                //用于取Controller的说明
                options.DocumentFilter<SwaggerDocumentFilter>(documents);
                foreach (var document in documents)
                {
                    options.IncludeXmlComments(() => document);
                }
            });

    /******Your Self Services Register.........******/
}

只支持IE浏览器吗?

使用360极速浏览器时,样式打乱,只支持IE浏览器吗?
Swashbuckle.AspNetCore.Swagger的版本是1.2

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.