swagger 怎么显示enum_swagger+enum+注释如何实现?

1、添加DocumentFilter

GlobalConfiguration.Configuration

.EnableSwagger(c =>

{

c.DocumentFilter();

})

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

using System;

using System.Web.Http.Description;

using Swashbuckle.Swagger;

using System.Collections.Generic;

namespace WebWeizhanWeixinGateway.Swagger

{

///

/// swagger enum 支持

///

public class SwaggerAddEnumDescriptions : IDocumentFilter

{

///

/// Apply

///

/// swaggerDoc

/// schemaRegistry

/// apiExplorer

public void Apply(SwaggerDocument swaggerDoc, SchemaRegistry schemaRegistry, IApiExplorer apiExplorer)

{

// add enum descriptions to result models

foreach (KeyValuePair schemaDictionaryItem in swaggerDoc.definitions)

{

Schema schema = schemaDictionaryItem.Value;

foreach (KeyValuePair propertyDictionaryItem in schema.properties)

{

Schema property = propertyDictionaryItem.Value;

IList propertyEnums = [email protected];

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 possibleParameterisedOperations = new List { pathItem.get, pathItem.post, pathItem.put };

possibleParameterisedOperations.FindAll(x => x != null).ForEach(x => DescribeEnumParameters(x.parameters));

}

}

}

private void DescribeEnumParameters(IList parameters)

{

if (parameters != null)

{

foreach (Parameter param in parameters)

{

IList paramEnums = [email protected];

if (paramEnums != null && paramEnums.Count > 0)

{

param.description += DescribeEnum(paramEnums);

}

}

}

}

private string DescribeEnum(IList enums)

{

List enumDescriptions = new List();

foreach (object enumOption in enums)

{

enumDescriptions.Add(string.Format("{0} = {1}", (int)enumOption, Enum.GetName(enumOption.GetType(), enumOption)));

}

return string.Join(", ", enumDescriptions.ToArray());

}

}

}

原文:https://www.cnblogs.com/microsoft-zyl/p/14125068.html

你可能感兴趣的:(swagger,怎么显示enum)