confluentinc / confluentinc/confluent-kafka-python

AvroSerializer with record_subject_name_strategy raises TypeError: SerializationContext cannot be None since 2.13

Open
#2,183 1 comment 0 reactions 0 assignees View on GitHub
component:schema-registry
Dominant language
Python
Stars
509
Forks
964
Avg merge
2d 2h
Merged PRs (30d)
14

Description

Hi,

We're using `AvroSerializer` with `record_subject_name_strategy`, and never needed a `SerializationContext`. Starting from 2.13, we get `TypeError: SerializationContext cannot be None`.

Looks like the change was introduced in https://github.com/confluentinc/confluent-kafka-python/commit/3886a30c070359f2d54a7c659715758823550054.

Maybe I'm missing some context on this, but I don't understand how raising here would make sense. I'm supplying the serializer function bound to `self._to_dict`.

```py
if self._to_dict is not None:
if ctx is None:
raise TypeError("SerializationContext cannot be None")
value = self._to_dict(obj, ctx)
```
https://github.com/confluentinc/confluent-kafka-python/blob/master/src/confluent_kafka/schema_registry/_sync/avro.py#L396-L399

Here's a minimal reproducer that hopefully gives an idea how we're using it (works on 2.12.2):

```py
import json

from confluent_kafka.schema_registry import (
SchemaRegistryClient,
record_subject_name_strategy,
)
from confluent_kafka.schema_registry.avro import AvroSerializer

schema = {
"type": "record",
"name": "MockSchema",
"namespace": "test",
"fields": [
{"name": "id", "type": "string"},
{"name": "size", "type": ["null", "string"], "default": None},
],
}

c = SchemaRegistryClient({"url": "http://schema-registry.kafka"})
c.get_subjects()

s = AvroSerializer(
schema_registry_client=c,
schema_str=json.dumps(schema),
to_dict=lambda msg, ctx: msg,
conf={
"auto.register.schemas": True,
"subject.name.strategy": record_subject_name_strategy,
},
)

print(s({"id": "hello"}))
```

```
$ python -m minimal_ctx
Traceback (most recent call last):
File "", line 198, in _run_module_as_main
File "", line 88, in _run_code
File "/minimal_ctx.py", line 32, in
print(s({"id": "hello"}))
^^^^^^^^^^^^^^^^^^
File "/lib/python3.11/site-packages/confluent_kafka/schema_registry/_sync/avro.py", line 347, in __call__
return self.__serialize(obj, ctx)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/lib/python3.11/site-packages/confluent_kafka/schema_registry/_sync/avro.py", line 398, in __serialize
raise TypeError("SerializationContext cannot be None")
TypeError: SerializationContext cannot be None

# on 2.12.2:

$ python -m minimal_ctx
b'\x00\x00\x00\x00\x15\nhello\x00'
```

Thanks for looking into this!

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.