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 2022/06/07 14:58:30 UTC

[pulsar] branch master updated: [Python Client] Fixed reserved keys is not removed when encode (#15844) (#15947)

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

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


The following commit(s) were added to refs/heads/master by this push:
     new f22f29fd2cf [Python Client] Fixed reserved keys is not removed when encode (#15844) (#15947)
f22f29fd2cf is described below

commit f22f29fd2cfb11aab371ba735d8c4493494b6b99
Author: boatrainlsz <yi...@gmail.com>
AuthorDate: Tue Jun 7 22:58:22 2022 +0800

    [Python Client] Fixed reserved keys is not removed when encode (#15844) (#15947)
---
 pulsar-client-cpp/python/pulsar/schema/schema.py | 20 ++++++++++++--------
 pulsar-client-cpp/python/schema_test.py          | 13 +++++++++++++
 2 files changed, 25 insertions(+), 8 deletions(-)

diff --git a/pulsar-client-cpp/python/pulsar/schema/schema.py b/pulsar-client-cpp/python/pulsar/schema/schema.py
index faf2c89ae25..f062c2e5e5e 100644
--- a/pulsar-client-cpp/python/pulsar/schema/schema.py
+++ b/pulsar-client-cpp/python/pulsar/schema/schema.py
@@ -77,6 +77,14 @@ class StringSchema(Schema):
         return 'StringSchema'
 
 
+def remove_reserved_key(data):
+    if '_default' in data:
+        del data['_default']
+    if '_required' in data:
+        del data['_required']
+    if '_required_default' in data:
+        del data['_required_default']
+
 
 class JsonSchema(Schema):
 
@@ -88,19 +96,15 @@ class JsonSchema(Schema):
         if isinstance(o, enum.Enum):
             return o.value
         else:
-            return o.__dict__
+            data = o.__dict__.copy()
+            remove_reserved_key(data)
+            return data
 
     def encode(self, obj):
         self._validate_object_type(obj)
         # Copy the dict of the object as to not modify the provided object via the reference provided
         data = obj.__dict__.copy()
-        if '_default' in data:
-            del data['_default']
-        if '_required' in data:
-            del data['_required']
-        if '_required_default' in data:
-            del data['_required_default']
-
+        remove_reserved_key(data)
         return json.dumps(data, default=self._get_serialized_value, indent=True).encode('utf-8')
 
     def decode(self, data):
diff --git a/pulsar-client-cpp/python/schema_test.py b/pulsar-client-cpp/python/schema_test.py
index c36a55eb0eb..78491ba7754 100755
--- a/pulsar-client-cpp/python/schema_test.py
+++ b/pulsar-client-cpp/python/schema_test.py
@@ -1266,5 +1266,18 @@ class SchemaTest(TestCase):
 
         client.close()
 
+    def test_json_schema_encode_remove_reserved_key(self):
+        class SchemaB(Record):
+            field = String(required=True)
+
+        class SchemaA(Record):
+            field = SchemaB()
+
+        a = SchemaA(field=SchemaB(field="something"))
+        b = JsonSchema(SchemaA).encode(a)
+        # reserved field should not be in the encoded json
+        self.assertTrue(b'_default' not in b)
+        self.assertTrue(b'_required' not in b)
+        self.assertTrue(b'_required_default' not in b)
 if __name__ == '__main__':
     main()