@@ -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