You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by pe...@apache.org on 2021/08/05 11:26:54 UTC

[pulsar] 01/03: [Issue 11473] [Python] Fix fields that are ignoring the required key argument (#11508)

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

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

commit a68cf5cf94748c450b18dc7fdb0f01a6981b0daf
Author: Hugo Pelletier <hp...@gmail.com>
AuthorDate: Wed Aug 4 03:08:05 2021 -0400

     [Issue 11473] [Python] Fix fields that are ignoring the required key argument (#11508)
    
    Fixes #11473
    
    
    ### Motivation
    
    By default, classes that inherit from Fields have a `required` key argument which is False.
    
    If an attribute is not declared with a value, an exception is raised.
    
    ```python
    Invalid type '<class' NoneType '>' for field 'name'. Expected a string
    ```
    
    ### Modifications
    
    The modifications made affect 2 classes: Field and String. In both cases, the `validate_type` method has been modified to take into account arguments inherited from the Field class, including` required` and `default`.
    
    Validation looks to see if the field is required. If it is not required (`required=False`) and the value does not exist, the default value is returned.
    
    This modification removes the error and allows to integrate the different types of fields based on the definition of the key arguments of the Field class
    
    (cherry picked from commit b80b0be4237ea11ed03378d1c9fb50f16b11b6a5)
---
 .../python/pulsar/schema/definition.py             |  9 ++++++--
 pulsar-client-cpp/python/schema_test.py            | 26 +++++++++++++++++++++-
 2 files changed, 32 insertions(+), 3 deletions(-)

diff --git a/pulsar-client-cpp/python/pulsar/schema/definition.py b/pulsar-client-cpp/python/pulsar/schema/definition.py
index d46cf3c..ad81a42 100644
--- a/pulsar-client-cpp/python/pulsar/schema/definition.py
+++ b/pulsar-client-cpp/python/pulsar/schema/definition.py
@@ -155,6 +155,9 @@ class Field(object):
         pass
 
     def validate_type(self, name, val):
+        if not val and not self._required:
+            return self.default()
+
         if type(val) != self.python_type():
             raise TypeError("Invalid type '%s' for field '%s'. Expected: %s" % (type(val), name, self.python_type()))
         return val
@@ -279,6 +282,10 @@ class String(Field):
 
     def validate_type(self, name, val):
         t = type(val)
+
+        if not val and not self._required:
+            return self.default()
+
         if not (t is str or t.__name__ == 'unicode'):
             raise TypeError("Invalid type '%s' for field '%s'. Expected a string" % (t, name))
         return val
@@ -289,10 +296,8 @@ class String(Field):
         else:
             return None
 
-
 # Complex types
 
-
 class _Enum(Field):
     def __init__(self, enum_type):
         if not issubclass(enum_type, Enum):
diff --git a/pulsar-client-cpp/python/schema_test.py b/pulsar-client-cpp/python/schema_test.py
index a0d60c0..24cd2e8 100755
--- a/pulsar-client-cpp/python/schema_test.py
+++ b/pulsar-client-cpp/python/schema_test.py
@@ -305,7 +305,6 @@ class SchemaTest(TestCase):
         except TypeError:
             pass # Expected
 
-
     def test_serialize_json(self):
         class Example(Record):
             a = Integer()
@@ -410,6 +409,31 @@ class SchemaTest(TestCase):
         self.assertEqual(r.b, None)
         self.assertEqual(r.c, 'hello')
 
+    def test_none_value(self):
+        """
+        The objective of the test is to check that if no value is assigned to the attribute, the validation is returning
+        the expect default value as defined in the Field class
+        """
+        class Example(Record):
+            a = Null()
+            b = Boolean()
+            c = Integer()
+            d = Long()
+            e = Float()
+            f = Double()
+            g = Bytes()
+            h = String()
+
+        r = Example()
+
+        self.assertIsNone(r.a)
+        self.assertFalse(r.b)
+        self.assertIsNone(r.c)
+        self.assertIsNone(r.d)
+        self.assertIsNone(r.e)
+        self.assertIsNone(r.f)
+        self.assertIsNone(r.g)
+        self.assertIsNone(r.h)
     ####
 
     def test_json_schema(self):