• swagger+enum+注释如何实现?


    1、添加DocumentFilter

    
    GlobalConfiguration.Configuration
    	.EnableSwagger(c =>
    	{
    	c.DocumentFilter<SwaggerAddEnumDescriptions>();
    	})
    

    2、 {project-webapi}/swagger下新增类SwaggerAddEnumDescriptions

    using System;
    using System.Web.Http.Description;
    using Swashbuckle.Swagger;
    using System.Collections.Generic;
    
    namespace WebWeizhanWeixinGateway.Swagger
    {
        /// <summary>
        ///  swagger enum 支持
        /// </summary>
        public class SwaggerAddEnumDescriptions : IDocumentFilter
        {
            /// <summary>
            /// Apply
            /// </summary>
            /// <param name="swaggerDoc">swaggerDoc</param>
            /// <param name="schemaRegistry">schemaRegistry</param>
            /// <param name="apiExplorer">apiExplorer</param>
            public void Apply(SwaggerDocument swaggerDoc, SchemaRegistry schemaRegistry, IApiExplorer apiExplorer)
            {
                // add enum descriptions to result models
                foreach (KeyValuePair<string, Schema> schemaDictionaryItem in swaggerDoc.definitions)
                {
                    Schema schema = schemaDictionaryItem.Value;
                    foreach (KeyValuePair<string, Schema> propertyDictionaryItem in schema.properties)
                    {
                        Schema property = propertyDictionaryItem.Value;
                        IList<object> propertyEnums = property.@enum;
                        if (propertyEnums != null && propertyEnums.Count > 0)
                        {
                            property.description += DescribeEnum(propertyEnums);
                        }
                    }
                }
    
                // add enum descriptions to input parameters
                if (swaggerDoc.paths.Count > 0)
                {
                    foreach (PathItem pathItem in swaggerDoc.paths.Values)
                    {
                        DescribeEnumParameters(pathItem.parameters);
    
                        // head, patch, options, delete left out
                        List<Operation> possibleParameterisedOperations = new List<Operation> { pathItem.get, pathItem.post, pathItem.put };
                        possibleParameterisedOperations.FindAll(x => x != null).ForEach(x => DescribeEnumParameters(x.parameters));
                    }
                }
            }
    
            private void DescribeEnumParameters(IList<Parameter> parameters)
            {
                if (parameters != null)
                {
                    foreach (Parameter param in parameters)
                    {
                        IList<object> paramEnums = param.@enum;
                        if (paramEnums != null && paramEnums.Count > 0)
                        {
                            param.description += DescribeEnum(paramEnums);
                        }
                    }
                }
            }
    
            private string DescribeEnum(IList<object> enums)
            {
                List<string> enumDescriptions = new List<string>();
                foreach (object enumOption in enums)
                {
                    enumDescriptions.Add(string.Format("{0} = {1}", (int)enumOption, Enum.GetName(enumOption.GetType(), enumOption)));
                }
                return string.Join(", ", enumDescriptions.ToArray());
            }
    
        }
    }
    

  • 相关阅读:
    软件工程(2019)第一次作业
    软件工程-第二次结对编程
    软件工程-第一次结对编程
    java-最大连续子数组和(最大字段和)
    软件工程-第二次作业
    软件工程-第一次作业
    windows下安装oracle11g测试是否成功与监听器问题和网页控制台登录
    java jdk13的安装与环境变量的配置(jre手动生成)
    结对编程第二次作业
    结对作业
  • 原文地址:https://www.cnblogs.com/microsoft-zyl/p/14125068.html
Copyright © 2020-2023  润新知