Skip to content

Commit 363529b

Browse files
committed
feat(generator): handle Any fields with Value or Struct
1 parent cefdd50 commit 363529b

4 files changed

Lines changed: 18 additions & 9 deletions

File tree

generator/internal/discovery_to_proto.cc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,9 @@ StatusOr<std::string> GetImportForProtobufType(
108108
std::string const& protobuf_type) {
109109
static auto const* const kProtobufTypeImports =
110110
new std::unordered_map<std::string, std::string>{
111-
{"google.protobuf.Any", "google/protobuf/any.proto"}};
111+
{"google.protobuf.Any", "google/protobuf/any.proto"},
112+
{"google.protobuf.Struct", "google/protobuf/struct.proto"},
113+
{"google.protobuf.Value", "google/protobuf/struct.proto"}};
112114

113115
auto iter = kProtobufTypeImports->find(protobuf_type);
114116
if (iter == kProtobufTypeImports->end()) {
@@ -354,7 +356,7 @@ std::set<std::string> FindAllTypesToImport(nlohmann::json const& json) {
354356
worklist.pop_back();
355357

356358
if (current->contains("type") && (*current)["type"] == "any") {
357-
types_to_import.insert("google.protobuf.Any");
359+
types_to_import.insert("google.protobuf.Value");
358360
}
359361
if (current->contains("$ref")) {
360362
types_to_import.insert((*current)["$ref"]);

generator/internal/discovery_to_proto_test.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1276,7 +1276,7 @@ TEST(FindAllTypesToImportTest, SimpleAnyField) {
12761276
auto const parsed_json = nlohmann::json::parse(kTypeJson, nullptr, false);
12771277
ASSERT_TRUE(parsed_json.is_object());
12781278
auto result = FindAllTypesToImport(parsed_json);
1279-
EXPECT_THAT(result, UnorderedElementsAre("google.protobuf.Any"));
1279+
EXPECT_THAT(result, UnorderedElementsAre("google.protobuf.Value"));
12801280
}
12811281

12821282
TEST(FindAllTypesToImportTest, MultipleSimpleRefFields) {
@@ -1345,7 +1345,7 @@ TEST(FindAllTypesToImportTest, ArrayRefAnyFields) {
13451345
auto const parsed_json = nlohmann::json::parse(kTypeJson, nullptr, false);
13461346
ASSERT_TRUE(parsed_json.is_object());
13471347
auto result = FindAllTypesToImport(parsed_json);
1348-
EXPECT_THAT(result, UnorderedElementsAre("google.protobuf.Any", "Bar"));
1348+
EXPECT_THAT(result, UnorderedElementsAre("google.protobuf.Value", "Bar"));
13491349
}
13501350

13511351
TEST(FindAllTypesToImportTest, MapRefFields) {
@@ -1387,7 +1387,7 @@ TEST(FindAllTypesToImportTest, MapAnyFields) {
13871387
auto const parsed_json = nlohmann::json::parse(kTypeJson, nullptr, false);
13881388
ASSERT_TRUE(parsed_json.is_object());
13891389
auto result = FindAllTypesToImport(parsed_json);
1390-
EXPECT_THAT(result, UnorderedElementsAre("google.protobuf.Any"));
1390+
EXPECT_THAT(result, UnorderedElementsAre("google.protobuf.Value"));
13911391
}
13921392

13931393
TEST(FindAllTypesToImportTest, SingleNestedRefField) {

generator/internal/discovery_type_vertex.cc

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ DiscoveryTypeVertex::DetermineTypeAndSynthesis(nlohmann::json const& v,
110110
}
111111

112112
if (type == "any") {
113-
return TypeInfo{"google.protobuf.Any", compare_package_name,
113+
return TypeInfo{"google.protobuf.Value", compare_package_name,
114114
properties_for_synthesis, false, false};
115115
}
116116

@@ -181,13 +181,16 @@ DiscoveryTypeVertex::DetermineTypeAndSynthesis(nlohmann::json const& v,
181181
scalar_type = CheckForScalarType(items);
182182
if (scalar_type) {
183183
type = *scalar_type;
184+
} else if (type == "any") {
185+
type = "google.protobuf.Value";
186+
return TypeInfo{type, compare_package_name, nullptr, false, false};
184187
} else if (type == "object" && items.contains("properties")) {
185188
// Synthesize a nested type for this array.
186189
type = CapitalizeFirstLetter(field_name + "Item");
187190
return TypeInfo{type, compare_package_name, &items, false, true};
188191
} else if (type == "object" && items.contains("additionalProperties") &&
189192
(items["additionalProperties"]).value("type", "") == "any") {
190-
type = "google.protobuf.Any";
193+
type = "google.protobuf.Struct";
191194
return TypeInfo{type, compare_package_name, nullptr, false, false};
192195
} else {
193196
return internal::InvalidArgumentError(

generator/internal/discovery_type_vertex_test.cc

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,8 @@ INSTANTIATE_TEST_SUITE_P(
220220
DetermineTypesSuccess{"string", R"""({"type":"string"})""", "string",
221221
true, false, false, false},
222222
DetermineTypesSuccess{"any", R"""({"type":"any"})""",
223-
"google.protobuf.Any", true, false, false, false},
223+
"google.protobuf.Value", true, false, false,
224+
false},
224225
DetermineTypesSuccess{"boolean", R"""({"type":"boolean"})""", "bool",
225226
true, false, false, false},
226227
DetermineTypesSuccess{"integer_no_format", R"""({"type":"integer"})""",
@@ -246,7 +247,10 @@ INSTANTIATE_TEST_SUITE_P(
246247
DetermineTypesSuccess{
247248
"array_any",
248249
R"""({"type":"array","items":{"type":"object","additionalProperties":{"type":"any"}}})""",
249-
"google.protobuf.Any", true, false, false, false},
250+
"google.protobuf.Struct", true, false, false, false},
251+
DetermineTypesSuccess{
252+
"array_items_any", R"""({"type":"array","items":{"type":"any"}})""",
253+
"google.protobuf.Value", true, false, false, false},
250254
DetermineTypesSuccess{
251255
"array_nested_message",
252256
R"""({"type":"array","items":{"type":"object", "properties":{}}})""",

0 commit comments

Comments
 (0)