Skip to content

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

@l1nd3r0th

Description

@l1nd3r0th

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 3886a30.

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.

        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):

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 "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "<project_dir>/minimal_ctx.py", line 32, in <module>
    print(s({"id": "hello"}))
          ^^^^^^^^^^^^^^^^^^
  File "<venv>/lib/python3.11/site-packages/confluent_kafka/schema_registry/_sync/avro.py", line 347, in __call__
    return self.__serialize(obj, ctx)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<venv>/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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    component:schema-registryAny schema registry related isues rather than kafka isolated ones

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions