Helper function to check if the maximum depth of a message is exceeded.
| 41 | |
| 42 | // Helper function to check if the maximum depth of a message is exceeded. |
| 43 | bool ExceedMaxDepth(const google::protobuf::Message& message, int current_depth) { |
| 44 | if (current_depth > FLAGS_json2pb_max_recursion_depth) { |
| 45 | return true; |
| 46 | } |
| 47 | |
| 48 | const google::protobuf::Descriptor* descriptor = message.GetDescriptor(); |
| 49 | const google::protobuf::Reflection* reflection = message.GetReflection(); |
| 50 | |
| 51 | std::vector<const google::protobuf::FieldDescriptor*> fields; |
| 52 | // Collect declared fields. |
| 53 | for (int i = 0; i < descriptor->field_count(); ++i) { |
| 54 | fields.push_back(descriptor->field(i)); |
| 55 | } |
| 56 | // Collect extension fields (if any). |
| 57 | { |
| 58 | std::vector<const google::protobuf::FieldDescriptor*> ext_fields; |
| 59 | descriptor->file()->pool()->FindAllExtensions(descriptor, &ext_fields); |
| 60 | fields.insert(fields.end(), ext_fields.begin(), ext_fields.end()); |
| 61 | } |
| 62 | |
| 63 | for (const auto* field : fields) { |
| 64 | if (field->cpp_type() != google::protobuf::FieldDescriptor::CPPTYPE_MESSAGE) { |
| 65 | continue; |
| 66 | } |
| 67 | |
| 68 | if (field->is_repeated()) { |
| 69 | const int count = reflection->FieldSize(message, field); |
| 70 | for (int j = 0; j < count; ++j) { |
| 71 | const google::protobuf::Message& sub_message = |
| 72 | reflection->GetRepeatedMessage(message, field, j); |
| 73 | if (ExceedMaxDepth(sub_message, current_depth + 1)) { |
| 74 | return true; |
| 75 | } |
| 76 | } |
| 77 | } else { |
| 78 | if (reflection->HasField(message, field)) { |
| 79 | const google::protobuf::Message& sub_message = |
| 80 | reflection->GetMessage(message, field); |
| 81 | if (ExceedMaxDepth(sub_message, current_depth + 1)) { |
| 82 | return true; |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | return false; |
| 88 | } |
| 89 | |
| 90 | Pb2JsonOptions::Pb2JsonOptions() |
| 91 | : enum_option(OUTPUT_ENUM_BY_NAME) |
no test coverage detected