Skip to content

Commit

Permalink
Fix default value of enum(int) in json_util with proto2 (#8835)
Browse files Browse the repository at this point in the history
  • Loading branch information
springhack authored Aug 3, 2021
1 parent d13afed commit 343f778
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 3 deletions.
23 changes: 20 additions & 3 deletions src/google/protobuf/util/internal/default_value_objectwriter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -411,16 +411,33 @@ void DefaultValueObjectWriter::MaybePopulateChildrenOfAny(Node* node) {
DataPiece DefaultValueObjectWriter::FindEnumDefault(
const google::protobuf::Field& field, const TypeInfo* typeinfo,
bool use_ints_for_enums) {
if (!field.default_value().empty())
return DataPiece(field.default_value(), true);

const google::protobuf::Enum* enum_type =
typeinfo->GetEnumByTypeUrl(field.type_url());
if (!enum_type) {
GOOGLE_LOG(WARNING) << "Could not find enum with type '" << field.type_url()
<< "'";
return DataPiece::NullData();
}
if (!field.default_value().empty()) {
if (!use_ints_for_enums) {
return DataPiece(field.default_value(), true);
} else {
const std::string& enum_default_value_name = field.default_value();
for (int enum_index = 0;
enum_index < enum_type->enumvalue_size();
++enum_index) {
auto& enum_value = enum_type->enumvalue(enum_index);
if (enum_value.name() == enum_default_value_name)
return DataPiece(enum_value.number());
}
GOOGLE_LOG(WARNING) << "Could not find enum value '"
<< enum_default_value_name
<< "' with type '"
<< field.type_url()
<< "'";
return DataPiece::NullData();
}
}
// We treat the first value as the default if none is specified.
return enum_type->enumvalue_size() > 0
? (use_ints_for_enums
Expand Down
10 changes: 10 additions & 0 deletions src/google/protobuf/util/json_format.proto
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,13 @@ message TestExtension {
}
optional string value = 1;
}

enum EnumValue {
PROTOCOL = 0;
BUFFER = 1;
DEFAULT = 2;
}

message TestDefaultEnumValue {
optional EnumValue enum_value = 1 [default = DEFAULT];
}
19 changes: 19 additions & 0 deletions src/google/protobuf/util/json_util_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,25 @@ TEST_F(JsonUtilTest, TestPrintEnumsAsIntsWithDefaultValue) {
EXPECT_EQ(proto3::BAR, parsed.enum_value3());
}

TEST_F(JsonUtilTest, TestPrintProto2EnumAsIntWithDefaultValue) {
protobuf_unittest::TestDefaultEnumValue orig;

JsonPrintOptions print_options;
// use enum as int
print_options.always_print_enums_as_ints = true;
print_options.always_print_primitive_fields = true;

// result should be int rather than string
std::string expected_json = "{\"enumValue\":2}";
EXPECT_EQ(expected_json, ToJson(orig, print_options));

protobuf_unittest::TestDefaultEnumValue parsed;
JsonParseOptions parse_options;
ASSERT_TRUE(FromJson(expected_json, &parsed, parse_options));

EXPECT_EQ(protobuf_unittest::DEFAULT, parsed.enum_value());
}

TEST_F(JsonUtilTest, ParseMessage) {
// Some random message but good enough to verify that the parsing wrapper
// functions are working properly.
Expand Down

0 comments on commit 343f778

Please sign in to comment.