Skip to content

Commit cecb8c7

Browse files
committed
Update
1 parent 4c659c0 commit cecb8c7

File tree

1 file changed

+8
-4
lines changed
  • src/Grpc/JsonTranscoding/src/Microsoft.AspNetCore.Grpc.JsonTranscoding/Internal/Json

1 file changed

+8
-4
lines changed

src/Grpc/JsonTranscoding/src/Microsoft.AspNetCore.Grpc.JsonTranscoding/Internal/Json/EnumNameHelpers.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ internal static class JsonNamingHelpers
3030
resolvedName = value;
3131
}
3232

33-
var valueDescriptor = enumDescriptor.FindValueByName(resolvedName);
34-
return valueDescriptor;
33+
return enumDescriptor.FindValueByName(resolvedName);
3534
}
3635

3736
internal static string? GetEnumFieldWriteName(EnumDescriptor enumDescriptor, object value, GrpcJsonSettings settings)
@@ -91,9 +90,10 @@ private static EnumMapping GetEnumMapping(string enumName, Type enumType)
9190
return new EnumMapping { WriteMapping = writeMapping, StripEnumPrefixMapping = stripEnumPrefixMapping };
9291
}
9392

93+
// Remove the prefix from the specified value. Ignore case and underscores in the comparison.
9494
private static string TryRemovePrefix(string prefix, string value)
9595
{
96-
var normalizedPrefix = new string(prefix.Where(c => c != '_').Select(char.ToLowerInvariant).ToArray());
96+
var normalizedPrefix = prefix.Replace("_", string.Empty, StringComparison.Ordinal).ToLowerInvariant();
9797

9898
var prefixIndex = 0;
9999
var valueIndex = 0;
@@ -131,7 +131,11 @@ private static string TryRemovePrefix(string prefix, string value)
131131
private static string GetEnumValueName(string enumName, string valueName)
132132
{
133133
var result = TryRemovePrefix(enumName, valueName);
134-
return char.IsDigit(result[0]) ? $"_{result}" : result;
134+
135+
// Prefix name starting with a digit with an underscore to ensure it is a valid identifier.
136+
return result.Length > 0 && char.IsDigit(result[0])
137+
? $"_{result}"
138+
: result;
135139
}
136140

137141
private sealed class EnumMapping

0 commit comments

Comments
 (0)