Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -211,22 +211,24 @@ internal static string AddEnumValuesDescription(
return null;
}

var enumElements = schema.Enum.Where(element => !(element is OpenApiNull)).ToArray();

if (!schema.Extensions.ContainsKey(xEnumNamesAlias) ||
((OpenApiArray)schema.Extensions[xEnumNamesAlias]).Count != schema.Enum.Count)
((OpenApiArray)schema.Extensions[xEnumNamesAlias]).Count != enumElements.Length)
{
return null;
}

var sb = new StringBuilder();
for (var i = 0; i < schema.Enum.Count; i++)
for (var i = 0; i < enumElements.Length; i++)
{
if (schema.Enum[i] is OpenApiInteger schemaEnumInt)
if (enumElements[i] is OpenApiInteger schemaEnumInt)
{
var value = schemaEnumInt.Value;
var name = ((OpenApiString)((OpenApiArray)schema.Extensions[xEnumNamesAlias])[i]).Value;
sb.Append($"{newLine}{newLine}{value} = {name}");
}
else if (schema.Enum[i] is OpenApiString schemaEnumString)
else if (enumElements[i] is OpenApiString schemaEnumString)
{
var value = schemaEnumString.Value;
sb.Append($"{newLine}{newLine}{value}");
Expand All @@ -241,7 +243,7 @@ internal static string AddEnumValuesDescription(
}

var xEnumDescriptions = (OpenApiArray)schema.Extensions[xEnumDescriptionsAlias];
if (xEnumDescriptions?.Count == schema.Enum.Count)
if (xEnumDescriptions?.Count == enumElements.Length)
{
var description = ((OpenApiString)((OpenApiArray)schema.Extensions[xEnumDescriptionsAlias])[i]).Value;
if (!string.IsNullOrWhiteSpace(description))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ public void Apply(
return;
}

var nullableUnderlyingType = Nullable.GetUnderlyingType(typeInfo);
typeInfo = nullableUnderlyingType?.IsEnum == true ? nullableUnderlyingType : typeInfo;

var enumsArray = new OpenApiArray();
var enumsDescriptionsArray = new OpenApiArray();
if (typeInfo.IsEnum)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,17 @@ public void Apply(
return;
}

var typeInfo = context.Type.GetTypeInfo();
var nullableUnderlyingType = Nullable.GetUnderlyingType(context.Type);
var enumType = nullableUnderlyingType?.IsEnum == true ? nullableUnderlyingType : context.Type;

var typeInfo = enumType.GetTypeInfo();
var enumsArray = new OpenApiArray();
var enumsDescriptionsArray = new OpenApiArray();
if (typeInfo.IsEnum)
{
var names = Enum
.GetNames(context.Type)
.Select(name => (Enum.Parse(context.Type, name), new OpenApiString(name)))
.GetNames(enumType)
.Select(name => (Enum.Parse(enumType, name), new OpenApiString(name)))
.GroupBy(x => x.Item1)
.Select(x => x.LastOrDefault().Item2)
.ToList();
Expand All @@ -98,7 +101,7 @@ public void Apply(
if (_includeXEnumDescriptions)
{
enumsDescriptionsArray.AddRange(EnumTypeExtensions
.GetEnumValuesDescription(context.Type, _descriptionSources, _xmlNavigators, _includeXEnumRemarks)
.GetEnumValuesDescription(enumType, _descriptionSources, _xmlNavigators, _includeXEnumRemarks)
.GroupBy(x => x.EnumValue)
.Select(x => x.LastOrDefault().EnumDescription));
if (!schema.Extensions.ContainsKey(_xEnumDescriptionsAlias) && enumsDescriptionsArray.Any())
Expand Down
Loading