Browse Source

Fix issue with empty fields serialization

- Invalidate field if variant value isNull(was never set)
- Add and update tests
Alexey Edelev 5 years ago
parent
commit
a2d99a453d
2 changed files with 18 additions and 0 deletions
  1. 1 0
      src/protobuf/qprotobufserializer_p.h
  2. 17 0
      tests/test_protobuf/serializationtest.cpp

+ 1 - 0
src/protobuf/qprotobufserializer_p.h

@@ -406,6 +406,7 @@ public:
                QByteArray(*s)(const T &, int &)>
     static QByteArray serializeWrapper(const QVariant &variantValue, int &fieldIndex) {
         if (variantValue.isNull()) {
+            fieldIndex = QtProtobufPrivate::NotUsedFieldIndex;
             return QByteArray();
         }
         const T& value = *(static_cast<const T *>(variantValue.data()));

+ 17 - 0
tests/test_protobuf/serializationtest.cpp

@@ -76,6 +76,7 @@
 #include "fieldindextest3message.h"
 #include "fieldindextest4message.h"
 #include "simpleenumlistmessage.h"
+#include "simplebytesmessage.h"
 
 using namespace qtprotobufnamespace::tests;
 using namespace QtProtobuf::tests;
@@ -1721,6 +1722,22 @@ TEST_F(SerializationTest, SimpleEnumListMessageTest)
                  "");
 }
 
+TEST_F(SerializationTest, EmptyBytesMessageTest)
+{
+    SimpleBytesMessage msg;
+
+    QByteArray result = msg.serialize(serializer.get());
+    ASSERT_TRUE(result.isEmpty());
+}
+
+TEST_F(SerializationTest, EmptyStringMessageTest)
+{
+    SimpleStringMessage msg;
+
+    QByteArray result = msg.serialize(serializer.get());
+    ASSERT_TRUE(result.isEmpty());
+}
+
 TEST_F(SerializationTest, DISABLED_BenchmarkTest)
 {
     SimpleIntMessage msg;