Created
March 8, 2019 09:35
-
-
Save Tewr/356e89cba320ae99b6e1da2ef4ac1273 to your computer and use it in GitHub Desktop.
Adds Enums to swashbuckle for use with NSwag
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Source: https://github.com/domaindrivendev/Swashbuckle/issues/1287 */ | |
(...) | |
config.AddSwaggerGen(options => { | |
options.DocumentFilter<SwaggerAddEnumDescriptions>(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Source: https://github.com/domaindrivendev/Swashbuckle/issues/1287 */ | |
public class SwaggerAddEnumDescriptions : IDocumentFilter | |
{ | |
public void Apply(SwaggerDocument swaggerDoc, DocumentFilterContext context) | |
{ | |
// add enum descriptions to result models | |
foreach (var schemaDictionaryItem in swaggerDoc.Definitions) | |
{ | |
var schema = schemaDictionaryItem.Value; | |
foreach (var propertyDictionaryItem in schema.Properties) | |
{ | |
var property = propertyDictionaryItem.Value; | |
var propertyEnums = property.Enum; | |
if (propertyEnums != null && propertyEnums.Count > 0) | |
{ | |
property.Description += DescribeEnum(propertyEnums); | |
property.Extensions.Add("x-enumNames", GetStringMapping(propertyEnums)); | |
} | |
} | |
} | |
if (swaggerDoc.Paths.Count <= 0) return; | |
// add enum descriptions to input parameters | |
foreach (var pathItem in swaggerDoc.Paths.Values) | |
{ | |
DescribeEnumParameters(pathItem.Parameters); | |
// head, patch, options, delete left out | |
var possibleParameterisedOperations = new List<Operation> {pathItem.Get, pathItem.Post, pathItem.Put}; | |
possibleParameterisedOperations.FindAll(x => x != null) | |
.ForEach(x => DescribeEnumParameters(x.Parameters)); | |
} | |
} | |
private string[] GetStringMapping(IList<object> enums) | |
{ | |
var enumDescriptions = new List<string>(); | |
Type type = null; | |
foreach (var enumOption in enums) | |
{ | |
if (type == null) type = enumOption.GetType(); | |
enumDescriptions.Add(Enum.GetName(type, enumOption)); | |
} | |
return enumDescriptions.ToArray(); | |
} | |
private static void DescribeEnumParameters(IList<IParameter> parameters) | |
{ | |
if (parameters == null) return; | |
foreach (var param in parameters) | |
{ | |
if (param.Extensions.ContainsKey("enum") && param.Extensions["enum"] is IList<object> paramEnums && | |
paramEnums.Count > 0) | |
{ | |
param.Description += DescribeEnum(paramEnums); | |
} | |
} | |
} | |
private static string DescribeEnum(IEnumerable<object> enums) | |
{ | |
var enumDescriptions = new List<string>(); | |
Type type = null; | |
foreach (var enumOption in enums) | |
{ | |
if (type == null) type = enumOption.GetType(); | |
enumDescriptions.Add( | |
$"{Convert.ChangeType(enumOption, type.GetEnumUnderlyingType())} = {Enum.GetName(type, enumOption)}"); | |
} | |
return $"{Environment.NewLine}{string.Join(Environment.NewLine, enumDescriptions)}"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment