From 57ad3e4715f05e46c067cb5080cca6945aefdf32 Mon Sep 17 00:00:00 2001 From: boatrain Date: Sun, 5 Jun 2022 22:41:13 +0800 Subject: [PATCH] [Python Client] Fixed reserved keys is not removed when encode (#15844) --- .../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 faf2c89ae2587..f062c2e5e5e03 100644 --- a/pulsar-client-cpp/python/pulsar/schema/schema.py +++ b/pulsar-client-cpp/python/pulsar/schema/schema.py @@ -77,6 +77,14 @@ def __str__(self): 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 @@ def _get_serialized_value(self, o): 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 c36a55eb0eb11..78491ba7754b7 100755 --- a/pulsar-client-cpp/python/schema_test.py +++ b/pulsar-client-cpp/python/schema_test.py @@ -1266,5 +1266,18 @@ def produce_and_consume(topic, schema_definition): 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()