Skip to content

Conversation

@fangnx
Copy link
Member

@fangnx fangnx commented Aug 26, 2025

What

Problem: Deserializers with ctx=None threw 'NoneType' object has no attribute 'headers'

Fix:

  • dual_schema_id_deserializer: Handle None context gracefully
  • header_schema_id_serializer: Throw clear SerializationError for required context

Checklist

  • Contains customer facing changes? Including API/behavior changes
  • Did you add sufficient unit test and/or integration test coverage for this PR?
    • If not, please explain why it is not required

References

JIRA: https://confluentinc.atlassian.net/browse/DGS-22029
Issue: #2034

Test & Review

Open questions / Follow-ups

Copilot AI review requested due to automatic review settings August 26, 2025 17:47
@fangnx fangnx requested review from a team and MSeal as code owners August 26, 2025 17:47
@confluent-cla-assistant
Copy link

🎉 All Contributor License Agreements have been signed. Ready to merge.
Please push an empty commit if you would like to re-run the checks to verify CLA status for all contributors.

This comment was marked as outdated.

@fangnx fangnx requested a review from rayokota August 26, 2025 17:50
@sonarqube-confluent

This comment has been minimized.

1 similar comment
@sonarqube-confluent

This comment has been minimized.

except AttributeError as e:
if "'NoneType' object has no attribute 'headers'" in str(e):
pytest.fail("Should not raise AttributeError for None context")
except Exception:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't put generic Exception: pass into code. It'll mask ugly errors and failure modes.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use https://docs.pytest.org/en/stable/reference/reference.html#pytest.raises instead for this test pattern and assert to correct exception positively rather than wrong exception negatively

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the review! I've refactored the tests

except AttributeError as e:
if "'NoneType' object has no attribute 'headers'" in str(e):
pytest.fail("Should not raise AttributeError for None context")
except Exception:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See above comment

Returns:
bytes: The payload
"""
if ctx is None:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should instead do a header assignment conditional on ctx so that the serialization choice is made once and keep the logic DRY.

@sonarqube-confluent

This comment has been minimized.

query = {}
if fmt is not None:
query = {'format': fmt}
query['format'] = fmt
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Found this bug when I was going through the code paths

@fangnx fangnx requested review from MSeal and Copilot August 28, 2025 15:11
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR addresses a bug where Avro deserializers with ctx=None would throw 'NoneType' object has no attribute 'headers' errors by implementing proper null context handling.

  • Added null context validation in header_schema_id_serializer to throw clear error messages
  • Modified dual_schema_id_deserializer to gracefully handle None context by skipping header checks
  • Fixed an unrelated bug in schema registry clients where undefined query variable could cause errors

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
tests/schema_registry/test_schema_id.py Added unit tests to verify null context handling in both serializer and deserializer functions
src/confluent_kafka/schema_registry/init.py Updated type hints and implemented null context handling logic for serializer/deserializer functions
src/confluent_kafka/schema_registry/_sync/schema_registry_client.py Fixed undefined variable bug by initializing query dictionary before conditional assignment
src/confluent_kafka/schema_registry/_async/schema_registry_client.py Fixed undefined variable bug by initializing query dictionary before conditional assignment

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

ServerConfig
)
from ..serialization import SerializationError, MessageField
from ..serialization import SerializationError, MessageField, SerializationContext
Copy link

Copilot AI Aug 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The import of SerializationContext is added but Optional is used in type hints without importing it from typing module. Consider adding 'from typing import Optional' to ensure proper type annotation support.

Copilot uses AI. Check for mistakes.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

from typing import Optional already exists in line 19

@sonarqube-confluent

This comment has been minimized.

1 similar comment
@sonarqube-confluent

This comment has been minimized.

Copy link
Member

@rayokota rayokota left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @fangnx , LGTM

@sonarqube-confluent
Copy link

Passed

Analysis Details

1 Issue

  • Bug 0 Bugs
  • Vulnerability 0 Vulnerabilities
  • Code Smell 1 Code Smell

Coverage and Duplications

  • Coverage 81.00% Coverage (66.40% Estimated after merge)
  • Duplications No duplication information (5.60% Estimated after merge)

Project ID: confluent-kafka-python

View in SonarQube

@fangnx fangnx merged commit 97bded0 into master Aug 29, 2025
3 checks passed
@fangnx fangnx deleted the fix-serde-ctx-none branch August 29, 2025 17:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants