Skip to content

Instantly share code, notes, and snippets.

@JamesIgoe
Last active October 27, 2022 06:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JamesIgoe/70691dd456ed46f2b89eed041d868bf7 to your computer and use it in GitHub Desktop.
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
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