diff --git a/src/KubeOps.Transpiler/Crds.cs b/src/KubeOps.Transpiler/Crds.cs index e41d9f0d..42116320 100644 --- a/src/KubeOps.Transpiler/Crds.cs +++ b/src/KubeOps.Transpiler/Crds.cs @@ -1,6 +1,7 @@ using System.Collections; using System.Collections.ObjectModel; using System.Reflection; +using System.Runtime.Serialization; using System.Text.Json.Serialization; using k8s; @@ -336,12 +337,46 @@ private static V1JSONSchemaProps Map(this MetadataLoadContext context, Type type "System.Enum" => new V1JSONSchemaProps { Type = String, - EnumProperty = Enum.GetNames(type).Cast().ToList(), + EnumProperty = GetEnumNames(context, type), }, _ => throw InvalidType(type), }; } + private static IList GetEnumNames(this MetadataLoadContext context, Type type) + { + var attributeNameByFieldName = new Dictionary(); + foreach (var field in type.GetFields(BindingFlags.Public | BindingFlags.Static)) + { + if (field.GetCustomAttribute() is { Value: not null } enumMemberAtribute) + { + attributeNameByFieldName.Add(field.Name, enumMemberAtribute.Value); + } + +#if NET9_0_OR_GREATER + if (field.GetCustomAttribute() is { Name: not null } stringEnumMemberAttribute) + { + attributeNameByFieldName.Add(field.Name, stringEnumMemberAttribute.Name); + } +#endif + } + + var enumName = new List(); + foreach (var value in Enum.GetNames(type)) + { + if (attributeNameByFieldName.TryGetValue(value, out var name)) + { + enumName.Add(name); + } + else + { + enumName.Add(value); + } + } + + return enumName; + } + private static V1JSONSchemaProps MapObjectType(this MetadataLoadContext context, Type type) { switch (type.FullName) diff --git a/src/KubeOps.Transpiler/KubeOps.Transpiler.csproj b/src/KubeOps.Transpiler/KubeOps.Transpiler.csproj index a71a5557..25af1db5 100644 --- a/src/KubeOps.Transpiler/KubeOps.Transpiler.csproj +++ b/src/KubeOps.Transpiler/KubeOps.Transpiler.csproj @@ -1,7 +1,7 @@ - net6.0;net7.0;net8.0 + net6.0;net7.0;net8.0;net9.0