Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 8 additions & 4 deletions aws_lambda_powertools/utilities/kafka/consumer_records.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,11 @@ def key(self) -> Any:

@cached_property
def value(self) -> Any:
value = self["value"]
value = self.get("value")

# Return None for a tombstone record, which Lambda delivers without a value or with a null one
if value is None:
return None

# Determine schema type and schema string
schema_type = None
Expand Down Expand Up @@ -98,9 +102,9 @@ def value(self) -> Any:
return deserialized_value

@property
def original_value(self) -> str:
"""The original (base64 encoded) Kafka record value."""
return self["value"]
def original_value(self) -> str | None:
"""The original (base64 encoded) Kafka record value, or None for a tombstone record."""
return self.get("value")

@property
def original_key(self) -> str | None:
Expand Down
4 changes: 2 additions & 2 deletions docs/utilities/kafka.md
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,8 @@ Each Kafka record contains important metadata that you can access alongside the
| `timestamp_type` | Timestamp type (CREATE_TIME or LOG_APPEND_TIME) | Data lineage verification |
| `headers` | Key-value pairs attached to the message | Cross-cutting concerns like correlation IDs |
| `key` | Deserialized message key | Customer ID or entity identifier |
| `value` | Deserialized message content | The actual business data |
| `original_value` | Base64-encoded original message value | Debugging or custom deserialization |
| `value` | Deserialized message content, or `None` for a tombstone record | The actual business data |
| `original_value` | Base64-encoded original message value, or `None` for a tombstone record | Debugging or custom deserialization |
| `original_key` | Base64-encoded original message key | Debugging or custom deserialization |
| `value_schema_metadata` | Metadata about the value schema like `schemaId` and `dataFormat` | Data format and schemaId propagated when integrating with Schema Registry |
| `key_schema_metadata` | Metadata about the key schema like `schemaId` and `dataFormat` | Data format and schemaId propagated when integrating with Schema Registry |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -422,3 +422,22 @@ def handler(event: ConsumerRecords, context):
assert result.partition == kafka_event["records"]["my-topic-1"][0]["partition"]
assert result.offset == kafka_event["records"]["my-topic-1"][0]["offset"]
assert result.timestamp == kafka_event["records"]["my-topic-1"][0]["timestamp"]


@pytest.mark.parametrize("tombstone", [{}, {"value": None}], ids=["value_missing", "value_null"])
def test_kafka_consumer_tombstone_record_has_no_value(kafka_event_with_json_data, lambda_context, tombstone):
# GIVEN a tombstone record, which Lambda delivers without a value or with a null one
event = deepcopy(kafka_event_with_json_data)
record = event["records"]["my-topic-1"][0]
del record["value"]
record.update(tombstone)

@kafka_consumer(schema_config=SchemaConfig(value_schema_type="JSON", key_schema_type="JSON"))
def handler(event: ConsumerRecords, context):
return [(record.key, record.value, record.original_value) for record in event.records]

# WHEN the handler reads the record
result = handler(event, lambda_context)

# THEN the key is still deserialized and the value is None instead of raising
assert result == [({"user_id": "123"}, None, None)]