You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by mm...@apache.org on 2021/08/04 19:15:59 UTC

[pulsar] branch branch-2.8 updated: Check if the record is not None (#11559)

This is an automated email from the ASF dual-hosted git repository.

mmerli pushed a commit to branch branch-2.8
in repository https://gitbox.apache.org/repos/asf/pulsar.git


The following commit(s) were added to refs/heads/branch-2.8 by this push:
     new 49ea7e9  Check if the record is not None (#11559)
49ea7e9 is described below

commit 49ea7e9f18dc6390d18c84cf60baacb21a386a53
Author: Hugo Pelletier <hp...@gmail.com>
AuthorDate: Wed Aug 4 15:11:06 2021 -0400

    Check if the record is not None (#11559)
---
 .../python/pulsar/schema/definition.py             |  3 +++
 pulsar-client-cpp/python/schema_test.py            | 26 ++++++++++++++++++----
 2 files changed, 25 insertions(+), 4 deletions(-)

diff --git a/pulsar-client-cpp/python/pulsar/schema/definition.py b/pulsar-client-cpp/python/pulsar/schema/definition.py
index d7fcd1e..dcb2d83 100644
--- a/pulsar-client-cpp/python/pulsar/schema/definition.py
+++ b/pulsar-client-cpp/python/pulsar/schema/definition.py
@@ -149,6 +149,9 @@ class Record(with_metaclass(RecordMeta, object)):
         return self.__class__
 
     def validate_type(self, name, val):
+        if not val and not self._required:
+            return self.default()
+
         if not isinstance(val, self.__class__):
             raise TypeError("Invalid type '%s' for sub-record field '%s'. Expected: %s" % (
                 type(val), name, self.__class__))
diff --git a/pulsar-client-cpp/python/schema_test.py b/pulsar-client-cpp/python/schema_test.py
index c2cc745..7ec0c9a 100755
--- a/pulsar-client-cpp/python/schema_test.py
+++ b/pulsar-client-cpp/python/schema_test.py
@@ -452,7 +452,6 @@ class SchemaTest(TestCase):
                         'my-json-python-topic',
                         schema=JsonSchema(Example))
 
-
         # Validate that incompatible schema is rejected
         try:
             client.subscribe('my-json-python-topic', 'sub-1',
@@ -519,14 +518,12 @@ class SchemaTest(TestCase):
         self.assertEqual(b"Hello", msg.data())
         client.close()
 
-
     def test_bytes_schema(self):
         client = pulsar.Client(self.serviceUrl)
         producer = client.create_producer(
                         'my-bytes-python-topic',
                         schema=BytesSchema())
 
-
         # Validate that incompatible schema is rejected
         try:
             class Example(Record):
@@ -853,7 +850,6 @@ class SchemaTest(TestCase):
 
         client.close()
 
-
     def test_default_value(self):
         class MyRecord(Record):
             A = Integer()
@@ -974,6 +970,28 @@ class SchemaTest(TestCase):
         encode_and_decode('avro')
         encode_and_decode('json')
 
+    def test_sub_record_set_to_none(self):
+        class NestedObj1(Record):
+            na1 = String()
+            nb1 = Double()
+
+        class NestedObj2(Record):
+            na2 = Integer()
+            nb2 = Boolean()
+            nc2 = NestedObj1()
+
+        data_schema = AvroSchema(NestedObj2)
+        r = NestedObj2(na2=1, nb2=True)
+
+        data_encode = data_schema.encode(r)
+        data_decode = data_schema.decode(data_encode)
+
+        self.assertEqual(data_decode.__class__.__name__, 'NestedObj2')
+        self.assertEqual(data_decode, r)
+        self.assertEqual(data_decode.na2, 1)
+        self.assertTrue(data_decode.nb2)
+
+
     def test_produce_and_consume_complex_schema_data(self):
         class NestedObj1(Record):
             na1 = String()