Repository: dotnet/aspnetcore
File: src/Grpc/JsonTranscoding/src/Microsoft.AspNetCore.Grpc.JsonTranscoding/Internal/Json/EnumConverter.cs (C#)

Code before:
                var value = reader.GetString()!;
                var valueDescriptor = JsonNamingHelpers.GetEnumFieldReadValue(enumDescriptor, value, Context.Settings);
                if (valueDescriptor == null)
                {
                    throw new InvalidOperationException(@$"Error converting value ""{value}"" to enum type {typeToConvert}.");
                }

                return ConvertFromInteger(valueDescriptor.Number);
            case JsonTokenType.Number:
                return ConvertFromInteger(reader.GetInt32());
            case JsonTokenType.Null:
                return default;
            default:
                throw new InvalidOperationException($"Unexpected JSON token: {reader.TokenType}.");
        }
    }

    public override void Write(Utf8JsonWriter writer, TEnum value, JsonSerializerOptions options)
    {
        if (Context.Settings.WriteEnumsAsIntegers)
        {
            writer.WriteNumberValue(ConvertToInteger(value));
        }
        else
        {
            var enumDescriptor = (EnumDescriptor?)Context.DescriptorRegistry.FindDescriptorByType(value.GetType());
            if (enumDescriptor == null)
            {
                throw new InvalidOperationException($"Unable to resolve descriptor for {value.GetType()}.");
            }

            var name = JsonNamingHelpers.GetEnumFieldWriteName(enumDescriptor, value, Context.Settings);
            if (name != null)
            {
                writer.WriteStringValue(name);
            }
            else
            {
                writer.WriteNumberValue(ConvertToInteger(value));
            }
        }
    }

    private static TEnum ConvertFromInteger(int integer)
    {
        if (!TryConvertToEnum(integer, out var value))
        {
            throw new InvalidOperationException($"Integer can't be converted to enum {typeof(TEnum).FullName}.");
        }

        return value;

Review comment (suggestion):
```suggestion
            var enumDescriptor = (EnumDescriptor?)Context.DescriptorRegistry.FindDescriptorByType(value.GetType()) ?? throw new Ithrow new InvalidOperationException($"Unable to resolve descriptor for {value.GetType()}.");
```

Code after:

                return ConvertFromInteger(valueDescriptor.Number);
            case JsonTokenType.Number:
                return ConvertFromInteger(reader.GetInt32());
            case JsonTokenType.Null:
                return default;
            default:
                throw new InvalidOperationException($"Unexpected JSON token: {reader.TokenType}.");
        }
    }

    public override void Write(Utf8JsonWriter writer, TEnum value, JsonSerializerOptions options)
    {
        if (Context.Settings.WriteEnumsAsIntegers)
        {
            writer.WriteNumberValue(ConvertToInteger(value));
        }
        else
        {
            var enumDescriptor = (EnumDescriptor?)Context.DescriptorRegistry.FindDescriptorByType(value.GetType())
                ?? throw new InvalidOperationException($"Unable to resolve descriptor for {value.GetType()}.");

            var name = JsonNamingHelpers.GetEnumFieldWriteName(enumDescriptor, value, Context.Settings);
            if (name != null)
            {
                writer.WriteStringValue(name);
            }
            else
            {
                writer.WriteNumberValue(ConvertToInteger(value));
            }
        }
    }

    private static TEnum ConvertFromInteger(int integer)
    {
        if (!TryConvertToEnum(integer, out var value))
        {
            throw new InvalidOperationException($"Integer can't be converted to enum {typeof(TEnum).FullName}.");
        }

        return value;
    }

    private static int ConvertToInteger(TEnum value)
    {
        if (!TryConvertToInteger(value, out var integer))
        {
            throw new InvalidOperationException($"Enum {typeof(TEnum).FullName} can't be converted to integer.");
        }