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.

Unexpected 0-th value enum JSON serialization

See original GitHub issue

The testing message is defined as follows:

syntax = "proto2";

message MessageWithEnum {
    enum EnumType {
      FOO = 0;
      BAR = 1;
    }
    optional EnumType value = 1;
}

We observe the following serialization with pbandk:

Source message current JSON serialization expected JSON serialization
MessageWithEnum() {"value":null} {}
MessageWithEnum(value=FOO) {} {"value":"FOO"}
MessageWithEnum(value=BAR) {"value":"BAR"} OK!

The bug is reproduced in a unit test here: https://github.com/streem/pbandk/pull/234

I know that proto2 doesn’t really have a JSON spec defined, but other protobuf implementations (java, python, swift) would all behave as expected in this example.

In proto3, we observed the following serialization behavior:

Source message current JSON serialization expected JSON serialization
TestAllTypesProto3() {} OK!
TestAllTypesProto3(optionalNestedEnum = FOO) {} {"value":"FOO"}
TestAllTypesProto3(optionalNestedEnum = BAR) {"value":"BAR"} OK!
  1. Do you agree that the current behavior is unexpected?
  2. Would you be open for us contributing a fix for it?

Thank you for your time 😃

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:9 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
garypcommented, Nov 18, 2022

@antongrbin It’s released now!

1reaction
garypcommented, Oct 25, 2022

@antongrbin Thank you for the thorough fix and tests!

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - How can I ignore unknown enum values during json ...
You can solve this problem with a custom JsonConverter . Here is one I put together using a few pieces from the StringEnumConverter...
Read more >
Enum with a value of zero deserializes into default value
First (which has a value of 0) and then serialize and deserialize, I get ModeEnum.Third out of it, as obviously it considers a...
Read more >
How to correctly serialize/deserialize an enum with custom ...
I'm hoping someone can give me some insight into what I'm doing wrong here. I have an enum type, and I want to...
Read more >
JSON Schema Serializer and Deserializer
json.schema.spec.version Indicates the specification version to use for JSON schemas derived from objects. Valid values are one of the following strings: ...
Read more >
Enumeration class and JSON Serialization - Hi, I'm Ankit
In this post, I will explain how we can serialize an Enumeration class using a custom JSON Converter for both System.Text.
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