Coverage for faststream/kafka/subscriber/specification.py: 96%
22 statements
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-17 21:20 +0000
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-17 21:20 +0000
1from faststream._internal.endpoint.subscriber import SubscriberSpecification
2from faststream._internal.utils.path import Address
3from faststream.kafka.configs import KafkaBrokerConfig
4from faststream.kafka.subscriber.usecase import KAFKA_ADDRESS_SYNTAX
5from faststream.specification.asyncapi.utils import resolve_payloads
6from faststream.specification.schema import Message, Operation, SubscriberSpec
7from faststream.specification.schema.bindings import ChannelBinding, kafka
9from .config import KafkaSubscriberSpecificationConfig
12class KafkaSubscriberSpecification(
13 SubscriberSpecification[KafkaBrokerConfig, KafkaSubscriberSpecificationConfig],
14):
15 __slots__ = ()
17 @property
18 def topics(self) -> list[str]:
19 """The topics this endpoint reads, in the order they were declared.
21 Deduped through a dict rather than a set: set order varies per process and
22 would reach the document as the order of its channels.
23 """
24 prefix = self._outer_config.prefix
26 topics = [t.add_prefix(prefix).name for t in self.config.topics]
27 topics.extend(f"{prefix}{p.topic}" for p in self.config.partitions)
29 if self.config.pattern:
30 # A topic is a literal and takes the prefix as one; a pattern is the
31 # one Kafka argument that is compiled, so its prefix is compiled too.
32 topics.append(
33 Address(self.config.pattern, KAFKA_ADDRESS_SYNTAX)
34 .add_prefix(prefix)
35 .template,
36 )
38 return list(dict.fromkeys(topics))
40 @property
41 def channel_labels(self) -> list[str]:
42 return self.topics
44 def get_schema(self) -> dict[str, SubscriberSpec]:
45 payloads = self.get_payloads()
47 topics = self.topics
48 split = len(topics) > 1
50 channels = {}
51 for t in topics:
52 handler_name = self._channel_key(t, split=split)
54 channels[handler_name] = SubscriberSpec(
55 address=t,
56 description=self.description,
57 operation=Operation(
58 message=Message(
59 title=f"{handler_name}:Message",
60 payload=resolve_payloads(payloads),
61 ),
62 bindings=None,
63 ),
64 bindings=ChannelBinding(
65 kafka=kafka.ChannelBinding(
66 topic=t,
67 partitions=None,
68 replicas=None,
69 ),
70 ),
71 )
73 return channels