question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

gRPC JSON transcoding — Unable to resolve descriptor for Enum (deserialize enum from string value)

See original GitHub issue

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

It’s impossible to deserialize a string value of protobuf enum using Microsoft.AspNetCore.Grpc.JsonTranscoding 7.0.0. Received rror:

{
  "code": 3,
  "message": "Unable to resolve descriptor for Example.Country.Alpha3CountryCode.",
  "details": []
}

I think the problem with the method ResolveEnumDescriptor in Microsoft.AspNetCore.Grpc.JsonTranscoding.Internal.Json.EnumConverter<TEnum>. It expects that the enum type is defined as a nested type in a message

private static EnumDescriptor? ResolveEnumDescriptor(Type typeToConvert)
{
    var containingType = typeToConvert?.DeclaringType?.DeclaringType;

    if (containingType != null)
    {
        var messageDescriptor = JsonConverterHelper.GetMessageDescriptor(containingType);
        if (messageDescriptor != null)
        {
            for (var i = 0; i < messageDescriptor.EnumTypes.Count; i++)
            {
                if (messageDescriptor.EnumTypes[i].ClrType == typeToConvert)
                {
                    return messageDescriptor.EnumTypes[i];
                }
            }
        }
    }

    return null;
}

Setting TypeRegistry in the transcoding options doesn’t solve this problem.

Expected Behavior

Both int value and string value can be deserialized from JSON.

Steps To Reproduce

I wrote a small web app to reproduce this issue. Link.

Exceptions (if any)

      System.InvalidOperationException: Unable to resolve descriptor for Example.Country.Alpha3CountryCode.
         at Microsoft.AspNetCore.Grpc.JsonTranscoding.Internal.Json.EnumConverter`1.Read(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options)
         at System.Text.Json.Serialization.JsonConverter`1.TryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value)
         at System.Text.Json.Serialization.Metadata.JsonPropertyInfo`1.ReadJsonAndSetMember(Object obj, ReadStack& state, Utf8JsonReader& reader)
         at System.Text.Json.Serialization.Converters.ObjectDefaultConverter`1.OnTryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value)
         at System.Text.Json.Serialization.JsonConverter`1.TryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value)
         at System.Text.Json.Serialization.JsonConverter`1.ReadCore(Utf8JsonReader& reader, JsonSerializerOptions options, ReadStack& state)
         at System.Text.Json.Serialization.JsonConverter`1.ReadCoreAsObject(Utf8JsonReader& reader, JsonSerializerOptions options, ReadStack& state)
         at System.Text.Json.JsonSerializer.ReadCore[TValue](Utf8JsonReader& reader, JsonTypeInfo jsonTypeInfo, ReadStack& state)
         at System.Text.Json.JsonSerializer.ContinueDeserialize[TValue](ReadBufferState& bufferState, JsonReaderState& jsonReaderState, ReadStack& readStack, JsonTypeInfo jsonTypeInfo)
         at System.Text.Json.JsonSerializer.ReadFromStreamAsync[TValue](Stream utf8Json, JsonTypeInfo jsonTypeInfo, CancellationToken cancellationToken)
         at Microsoft.AspNetCore.Grpc.JsonTranscoding.Internal.JsonRequestHelpers.ReadMessage[TRequest](JsonTranscodingServerCallContext serverCallContext, JsonSerializerOptions serializerOptions)
         at Microsoft.AspNetCore.Grpc.JsonTranscoding.Internal.JsonRequestHelpers.ReadMessage[TRequest](JsonTranscodingServerCallContext serverCallContext, JsonSerializerOptions serializerOptions)
info: Grpc.AspNetCore.Grpc.JsonTranscoding.ServerCallHandler[3]
      Error status code 'InvalidArgument' with detail 'Unable to resolve descriptor for Example.Country.Alpha3CountryCode.' raised.
      System.InvalidOperationException: Unable to resolve descriptor for Example.Country.Alpha3CountryCode.
         at Microsoft.AspNetCore.Grpc.JsonTranscoding.Internal.Json.EnumConverter`1.Read(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options)
         at System.Text.Json.Serialization.JsonConverter`1.TryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value)
         at System.Text.Json.Serialization.Metadata.JsonPropertyInfo`1.ReadJsonAndSetMember(Object obj, ReadStack& state, Utf8JsonReader& reader)
         at System.Text.Json.Serialization.Converters.ObjectDefaultConverter`1.OnTryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value)
         at System.Text.Json.Serialization.JsonConverter`1.TryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value)
         at System.Text.Json.Serialization.JsonConverter`1.ReadCore(Utf8JsonReader& reader, JsonSerializerOptions options, ReadStack& state)
         at System.Text.Json.Serialization.JsonConverter`1.ReadCoreAsObject(Utf8JsonReader& reader, JsonSerializerOptions options, ReadStack& state)
         at System.Text.Json.JsonSerializer.ReadCore[TValue](Utf8JsonReader& reader, JsonTypeInfo jsonTypeInfo, ReadStack& state)
         at System.Text.Json.JsonSerializer.ContinueDeserialize[TValue](ReadBufferState& bufferState, JsonReaderState& jsonReaderState, ReadStack& readStack, JsonTypeInfo jsonTypeInfo)
         at System.Text.Json.JsonSerializer.ReadFromStreamAsync[TValue](Stream utf8Json, JsonTypeInfo jsonTypeInfo, CancellationToken cancellationToken)
         at Microsoft.AspNetCore.Grpc.JsonTranscoding.Internal.JsonRequestHelpers.ReadMessage[TRequest](JsonTranscodingServerCallContext serverCallContext, JsonSerializerOptions serializerOptions)
         at Microsoft.AspNetCore.Grpc.JsonTranscoding.Internal.JsonRequestHelpers.ReadMessage[TRequest](JsonTranscodingServerCallContext serverCallContext, JsonSerializerOptions serializerOptions)

.NET Version

7.0.100

Anything else?

Packages:

<PackageReference Include="Grpc.AspNetCore" Version="2.50.0" />
<PackageReference Include="Grpc.AspNetCore.Server.Reflection" Version="2.50.0" />
<PackageReference Include="Microsoft.AspNetCore.Grpc.JsonTranscoding" Version="7.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Grpc.Swagger" Version="0.3.0" />

Issue Analytics

  • State:closed
  • Created 10 months ago
  • Reactions:1
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
JamesNKcommented, Dec 2, 2022

@AndrewP-GH Attached is an unofficial package from the fix PR. It’s one option if you don’t want to wait.

Microsoft.AspNetCore.Grpc.JsonTranscoding.7.0.2.zip

0reactions
AndrewP-GHcommented, Dec 2, 2022

Thank you!

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to get string values from enums in a protobuf grpc ...
When importing the .proto file into Postman, the JSON payload is suggested to be: "bookStatus": "IDEA", and it type-hints all the ...
Read more >
Enum naming convention is broken after version 1.35.0 ...
However, the HTTP/JSON to GRPC transcoder has changed the way it works. When issuing an HTTP/JSON request against endpoints, it used to accept...
Read more >
Protocol Buffers Well-Known Types
Kind (enum); FieldMask (message); FloatValue (message) ... The JSON representation of an Any value uses the regular representation of the deserialized, ...
Read more >
Announcing gRPC JSON transcoding for .NET - .NET Blog
gRPC uses HTTP/2, streaming, binary serialization, and message contracts to create high-performance, real-time services. .NET has excellent ...
Read more >
Tobu — Rust data encoding library ...
Note: All protobuf messages have a JSON encoding. rpc ... use std::ptr::NonNull; pub enum Value { Bool(NonNull<bool>), I32(NonNull<i32>), } ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found