Bug: ConsumerRecordRecords.key returns None when record key is an empty string

オープン
#8,696 コメント 3 件 リアクション 0 件 担当者 1 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

@ASHMEET555 がすでに取り組んでいます。

2026年10月29日 から。

  • #8698 @ASHMEET555 による — オープン

評価

難易度
1/5
見積もり時間
1〜3時間
初心者へのやさしさ
25/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
停滞
技術スタック
python
領域
backend

調査の方向性

Start in aws_lambda_powertools/utilities/kafka/consumer_records.py at the ConsumerRecordRecords.key logic described in the issue, then inspect the existing KafkaEventRecord.decoded_key behavior for comparison. Add regression coverage for empty, missing, and null keys; done means an empty key decodes to an empty value while missing or null keys return None. A linked pull request is already open.

索引モデルが issue の本文から書いたものです。

説明

bug event_sources

Expected Behaviour

When a Kafka event contains an empty key ("key": ""), ConsumerRecordRecords.key should deserialize the empty string and return an empty key (e.g. "" or b""), matching Kafka semantics and KafkaEventRecord.decoded_key behavior. Only missing or null keys (None) should evaluate to None.

Current Behaviour

In aws_lambda_powertools/utilities/kafka/consumer_records.py (line 35):

key = self.get("key")

# Return None if key doesn't exist
if not key:
    return None

Because if not key: performs a truthiness check rather than an identity check against None, any empty base64 string ("key": "") is treated as nonexistent and returns None.

In contrast, KafkaEventRecord.decoded_key in aws_lambda_powertools.utilities.data_classes.kafka_event correctly distinguishes between missing and empty keys:

return None if self.key is None else base64.b64decode(self.key)

Code snippet

from aws_lambda_powertools.utilities.kafka.consumer_records import ConsumerRecordRecords

data = {
    "topic": "test-topic",
    "partition": 0,
    "offset": 100,
    "timestamp": 1690000000000,
    "timestampType": "CREATE_TIME",
    "key": "",  # Empty key representation in Kafka event
    "value": "dGVzdC12YWx1ZQ==",
}

record = ConsumerRecordRecords(data=data)

# Fails: returns None instead of empty decoded key
assert record.key is not None

Possible Solution

Update line 35 of aws_lambda_powertools/utilities/kafka/consumer_records.py:

-        if not key:
+        if key is None:
             return None

And add unit regression tests verifying empty string key handling.

Steps to Reproduce

  1. Instantiate ConsumerRecordRecords with a dict containing "key": "".
  2. Access record.key.
  3. Observe it returns None instead of deserializing the empty key.

Powertools for AWS Lambda (Python) version

latest

AWS Lambda function runtime

3.12

Packaging format used

PyPi

主要言語
Python
スター
3.3k
フォーク
511
平均マージ
3日 4時間
マージ済み PR(30日)
98

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

aws-powertools/powertools-lambda-python のほかの issue

aws-powertools/powertools-lambda-python の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。