tsturzl opened a new issue #12486:
URL: https://github.com/apache/pulsar/issues/12486


   **Describe the bug**
   The JsonSchema throws a KeyError exception during on `encode` method if you 
call it more than once. The keys that get deleted on encoding don't exist the 
second time and there is no check that happens to see if they still exist, [see 
here](https://github.com/apache/pulsar/blob/464a9cdc21609189771f1d95ac211b69eca1454c/pulsar-client-cpp/python/pulsar/schema/schema.py#L88).
 This seems problematic because you may want to send the message twice, or send 
the message into a different topic. Furthermore it's not generally good to 
modify an object by its `__dict__' representation, and it's probably best not 
to modify the object which is passed in by reference and instead copy it.
   
   **To Reproduce**
   Seems to happen even on the first use of the Record:
   
   ```
   class MyRecord(Record):
     a = Integer()
     b = String()
   
   producer = client.create_producer(
     topic="my-topic",
     schema=JsonSchema(MyRecord),
   )
   
   my_data = MyRecord(a=1, b="hello")
   producer.send(my_data)
   producer.send(my_data)
   ```
   
   This is can be shown in the REPL by simply invoking encode directly:
   
   ```
   >>> from pulsar.schema import *
   >>> class MyRecord(Record):
   ...     a=Integer()
   ...     b=String()
   ... 
   >>> record = MyRecord(a=1, b="hello")
   >>> schema = JsonSchema(MyRecord)
   >>> schema.encode(record)
   b'{\n "a": 1,\n "b": "hello"\n}'
   >>> schema.encode(record)
   Traceback (most recent call last):
     File "<stdin>", line 1, in <module>
     File 
"/home/tsturzl/.local/lib/python3.8/site-packages/pulsar/schema/schema.py", 
line 88, in encode
       del obj.__dict__['_default']
   KeyError: '_default'
   ```
   
   **Expected behavior**
   A message should be able to be used more than once. Encoding a Record should 
not modify that Record by reference.
   
   **Screenshots**
   N/A
   
   **Desktop (please complete the following information):**
   N/A
   
   **Additional context**
   Will provide PR to fix.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to