Parcourir la source

Fix generation of nested types

- Fix generation of nested types if no fields of this type defined
  in wrapper class
Alexey Edelev il y a 4 ans
Parent
commit
9e73b89b8d

+ 7 - 0
src/generator/generatorcommon.cpp

@@ -423,6 +423,9 @@ void common::iterateNestedMessages(const ::google::protobuf::Descriptor *message
 {
     for (int i = 0; i < message->nested_type_count(); i++) {
         auto nestedMessage = message->nested_type(i);
+        if (message->field_count() <= 0) {
+            callback(nestedMessage);
+        }
         for (int j = 0; j < message->field_count(); j++) {
             auto field = message->field(j);
             if (!field->is_map() && field->message_type() == nestedMessage) { //Probably there is more correct way to detect map in nested messages.                                                                              //TODO: Have idea to make maps nested classes instead of typedefs.
@@ -434,6 +437,10 @@ void common::iterateNestedMessages(const ::google::protobuf::Descriptor *message
 
 bool common::hasNestedMessages(const ::google::protobuf::Descriptor *message)
 {
+    if (message->nested_type_count() > 0 && message->field_count() <= 0) {
+        return true;
+    }
+
     for (int i = 0; i < message->nested_type_count(); i++) {
         auto nestedMessage = message->nested_type(i);
         for (int j = 0; j < message->field_count(); j++) {

+ 14 - 0
tests/test_protobuf/nestedtest.cpp.inc

@@ -185,5 +185,19 @@ TEST_F(NestedTest, ExternalTest)
     ASSERT_TRUE(test.externalNested() == qtprotobufnamespace1::externaltests::NestedFieldMessage::NestedMessage{55});
 }
 
+TEST_F(NestedTest, NestedNoFieldsTest)
+{
+    assertMessagePropertyRegistered<NestedNoFields::Nested, QtProtobuf::sint32>(1, "QtProtobuf::sint32", "testFieldInt");
+
+    NestedNoFields::Nested test{15};
+    EXPECT_EQ(test.testFieldInt(), 15);
+
+    const char *propertyName = "testFieldInt";
+
+    ASSERT_TRUE(test.setProperty(propertyName, 55));
+    ASSERT_TRUE(test.property(propertyName).value<QtProtobuf::sint32>() == 55);
+    ASSERT_TRUE(test.testFieldInt() == 55);
+}
+
 }
 }

+ 7 - 0
tests/test_protobuf/proto/nestedmessages.proto

@@ -30,3 +30,10 @@ message NeighborNested {
 message NestedExternal {
     qtprotobufnamespace1.externaltests.NestedFieldMessage.NestedMessage externalNested = 1;
 }
+
+message NestedNoFields {
+  message Nested {
+    sint32 testFieldInt = 1;
+  }
+}
+

+ 1 - 0
tests/test_protobuf_multifile/nestedtest.cpp

@@ -27,5 +27,6 @@
 #include "qtprotobufnamespace/tests/nested/nestedfieldmessage.h"
 #include "qtprotobufnamespace/tests/nested/nestedfieldmessage2.h"
 #include "qtprotobufnamespace/tests/nested/nestedexternal.h"
+#include "qtprotobufnamespace/tests/nested/nestednofields.h"
 
 #include "../test_protobuf/nestedtest.cpp.inc"