Last active
October 27, 2022 06:32
-
-
Save JamesIgoe/70691dd456ed46f2b89eed041d868bf7 to your computer and use it in GitHub Desktop.
When creating Swagger definitions using Swashbuckle, this enables detailing of ENUM properties as text
This file contains 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
using CitrixSessionManagement; | |
using Microsoft.OpenApi.Any; | |
using Microsoft.OpenApi.Models; | |
using Swashbuckle.AspNetCore.SwaggerGen; | |
using System; | |
using System.ComponentModel.DataAnnotations; | |
using System.IO; | |
using System.Linq; | |
using System.Reflection; | |
using System.Reflection.Metadata; | |
using System.Xml.Linq; | |
namespace OfficeServices.Helpers | |
{ | |
//services.AddControllers().AddNewtonsoftJson(o => | |
//{ | |
// o.SerializerSettings.Converters.Add(new StringEnumConverter | |
// { | |
// NamingStrategy = new DefaultNamingStrategy() | |
// }); | |
//}); | |
////generates swagger file | |
//services.AddSwaggerGen(); | |
//services.AddSwaggerGen(options => | |
//{ | |
// options.SwaggerDoc("v1", new OpenApiInfo | |
// { | |
// Version = "v1", | |
// Title = "TBD API", | |
// Description = "Collection of processes used across TBD", | |
// Contact = new OpenApiContact | |
// { | |
// Name = "TBD", | |
// Url = new Uri("https://TBD.org/Contact") | |
// }, | |
// }); | |
// var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml"; | |
// var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile); | |
// options.IncludeXmlComments(xmlPath); | |
// options.SchemaFilter<EnumTypesSchemaFilter>(xmlPath); | |
//}); | |
public static class EnumExtensions | |
{ | |
public static string GetDisplayName(this Enum enumValue) | |
{ | |
return enumValue.GetType() | |
.GetMember(enumValue.ToString()) | |
.First() | |
.GetCustomAttribute<DisplayAttribute>() | |
?.GetName(); | |
} | |
public static Type GetEnumType(string enumName) | |
{ | |
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies()) | |
{ | |
var type = assembly.GetType(enumName); | |
if (type == null) | |
continue; | |
if (type.IsEnum) | |
return type; | |
} | |
return null; | |
} | |
} | |
public class EnumTypesSchemaFilter : ISchemaFilter | |
{ | |
private readonly XDocument _xmlComments; | |
public EnumTypesSchemaFilter(string xmlPath) | |
{ | |
if (File.Exists(xmlPath)) | |
{ | |
_xmlComments = XDocument.Load(xmlPath); | |
} | |
} | |
public void Apply(OpenApiSchema schema, SchemaFilterContext context) | |
{ | |
if (_xmlComments == null) return; | |
if (schema.Enum != null | |
&& schema.Enum.Count > 0 | |
&& context.Type != null | |
&& context.Type.IsEnum) | |
{ | |
schema.Description += "<p>Members:</p><ul>"; | |
string enumName = context.Type.FullName; | |
Type classType = EnumExtensions.GetEnumType(enumName); | |
foreach (Enum apps in Enum.GetValues(classType)) | |
{ | |
schema.Description += "<li>"; | |
string name = apps.ToString(); | |
int value = (int)Enum.Parse(classType, name, true); | |
//possibly use | |
//string display = apps.GetDisplayName(); | |
schema.Description += name + " = " + value; | |
schema.Description += "</li>"; | |
} | |
schema.Description += "</ul>"; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment