Skip to content

Add support for ByteString#62

Merged
Havret merged 6 commits into
Havret:mainfrom
Lerbaek:main
Aug 25, 2023
Merged

Add support for ByteString#62
Havret merged 6 commits into
Havret:mainfrom
Lerbaek:main

Conversation

@Lerbaek

@Lerbaek Lerbaek commented Aug 24, 2023

Copy link
Copy Markdown

This aims to fix issue #5 by supporting the Google.Protobuf.ByteString type in FieldTypeResolver.

Furthermore, the error message for unsupported field types has been improved. It now prints the name of the unsupported field type, rather than the name of the FieldDescriptor type.

Keep in mind that I'm not all too experienced with Protobuf, so I don't know if this fixes the entire issue or if there's more to it, but it has been verified to fix my use case.

Note also that I'm well aware of the lack of testing, but unfortunately, I'm very restricted on my work PC, and so am unable to generate anything new.

@Lerbaek Lerbaek changed the title Add support for Protobuf Add support for ByteString Aug 24, 2023

@Havret Havret left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, and thank you for your contribution. I appreciate your efforts.

However, I've encountered an issue with the changes you've made. When I uncomment the following lines:


// BytesProperty = ByteString.CopyFromUtf8("abc")

I'm getting different results than expected:

The output I get differs from what's expected. Specifically, I see:
"bytesProperty":[97,98,99]
when I anticipate:
"bytesProperty": "YWJj"

Furthermore, during the deserialization test, I'm getting the exception:

System.NotSupportedException: The collection type 'Google.Protobuf.ByteString' is abstract, an interface, or is read only, and could not be instantiated and populated. Path: $ | LineNumber: 0 | BytePositionInLine: 299.
 ---> System.NotSupportedException: The collection type 'Google.Protobuf.ByteString' is abstract, an interface, or is read only, and could not be instantiated and populated.
   --- End of inner exception stack trace ---
   at System.Text.Json.ThrowHelper.ThrowNotSupportedException(ReadStack& state, Utf8JsonReader& reader, NotSupportedException ex)
   at System.Text.Json.ThrowHelper.ThrowNotSupportedException_CannotPopulateCollection(Type type, Utf8JsonReader& reader, ReadStack& state)
   at System.Text.Json.Serialization.Converters.IEnumerableOfTConverter`2.CreateCollection(Utf8JsonReader& reader, ReadStack& state, JsonSerializerOptions options)
   at System.Text.Json.Serialization.JsonCollectionConverter`2.OnTryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, TCollection& value)
   at System.Text.Json.Serialization.JsonConverter`1.TryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value)
   at System.Text.Json.Serialization.JsonResumableConverter`1.Read(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options)
   at Protobuf.System.Text.Json.InternalConverters.FieldConverter`1.Read(Utf8JsonReader& reader, IMessage obj, Type typeToConvert, JsonSerializerOptions options, IFieldAccessor fieldAccessor) in /Users/krzysztofporebski/git/Protobuf.System.Text.Json/src/Protobuf.System.Text.Json/InternalConverters/FieldConverter.cs:line 22
   at Protobuf.System.Text.Json.ProtobufConverter`1.Read(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options) in /Users/krzysztofporebski/git/Protobuf.System.Text.Json/src/Protobuf.System.Text.Json/ProtobufConverter.cs:line 110
   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)

@Lerbaek

Lerbaek commented Aug 25, 2023

Copy link
Copy Markdown
Author

Thanks for your feedback. I was aware that it was a naive approach, and the only reason it worked in my use case was probably because the ByteString was only in the contract but not in the instances I worked with.

Great to see that tests were already waiting. I've added a ByteString converter and added use of it, and now the ByteString tests pass.

@Lerbaek
Lerbaek requested a review from Havret August 25, 2023 10:33
Comment thread src/Protobuf.System.Text.Json/FieldTypeResolver.cs
Havret
Havret previously approved these changes Aug 25, 2023

@Havret Havret left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It works for the base case. While further work is needed to handle all cases (see link), I believe we can merge this now and address additional concerns in separate PRs.

@Havret
Havret merged commit c72dd78 into Havret:main Aug 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants