Skip to content

fix(api): handle null context in Baggage.fromContext and fromContextOrNull - #8666

Closed
Solaris-star wants to merge 1 commit into
open-telemetry:mainfrom
Solaris-star:fix/8665-baggage-null-context
Closed

fix(api): handle null context in Baggage.fromContext and fromContextOrNull#8666
Solaris-star wants to merge 1 commit into
open-telemetry:mainfrom
Solaris-star:fix/8665-baggage-null-context

Conversation

@Solaris-star

Copy link
Copy Markdown

Summary

Fixes #8665

Baggage.fromContext(null) and Baggage.fromContextOrNull(null) threw NullPointerException because both dereferenced the supplied Context without a null guard. This is inconsistent with Span.fromContext and Span.fromContextOrNull, which already log via ApiUsageLogger and return a safe fallback.

Fix

Apply the same pattern used by Span:

  • fromContext(null) → logs via ApiUsageLogger.logNullParam and returns Baggage.empty()
  • fromContextOrNull(null) → logs via ApiUsageLogger.logNullParam and returns null

Tests

Added two tests in BaggageContextTest:

  • fromContext_null — asserts Baggage.empty() is returned
  • fromContextOrNull_null — asserts null is returned

Note: I could not run the Gradle build locally (no Java runtime on this machine), but the change is a direct copy of the established Span.fromContext / Span.fromContextOrNull null-handling pattern in the same codebase.

…rNull

Baggage.fromContext(null) and Baggage.fromContextOrNull(null) threw
NullPointerException because both dereferenced the supplied Context
without a null guard. This is inconsistent with Span.fromContext and
Span.fromContextOrNull, which already log via ApiUsageLogger and return
a safe fallback (getInvalid() / null).

Apply the same pattern:
- fromContext(null) logs and returns Baggage.empty()
- fromContextOrNull(null) logs and returns null

Adds two tests covering the null-context paths.

Fixes open-telemetry#8665
@Solaris-star
Solaris-star requested a review from a team as a code owner July 28, 2026 11:31
@linux-foundation-easycla

Copy link
Copy Markdown

CLA Not Signed

@opentelemetry-pr-dashboard

opentelemetry-pr-dashboard Bot commented Jul 28, 2026

Copy link
Copy Markdown

Pull request dashboard status

Closed · refreshed 2026-07-28 15:27 UTC

Status above doesn't look right?
  • Anything look wrong? Report it with what you expected; it helps us improve the dashboard.

@codecov

codecov Bot commented Jul 28, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.46%. Comparing base (ff91c02) to head (fff8942).

Additional details and impacted files
@@            Coverage Diff            @@
##               main    #8666   +/-   ##
=========================================
  Coverage     91.46%   91.46%           
- Complexity    10456    10459    +3     
=========================================
  Files          1021     1021           
  Lines         27647    27653    +6     
  Branches       3242     3242           
=========================================
+ Hits          25288    25294    +6     
  Misses         1616     1616           
  Partials        743      743           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@NithinU2802

Copy link
Copy Markdown
Contributor

Hey @Solaris-star, I found and raised this issue and did work on it in #8667. Could you please close this PR to avoid duplicate work? Thank you for understanding!

@jack-berg

Copy link
Copy Markdown
Member

Come on. I can see you know how to use an LLM. And its clear based on the fast turnaround that you're scanning for simple well scoped issues to point your tokens at. Believe it or not, racing to solve trivial issues nobody has actually encountered in the wild isn't actually helpful and doesn't earn you good will from maintainers. If you really want to help in OSS, the critical path - the thing that's needed most and that people want to do the least - is code review.

@Solaris-star

Copy link
Copy Markdown
Author

You're right — closing in favor of #8667 by the issue author (@NithinU2802). Apologies for the duplicate; I should have checked that the reporter was already working on a fix before opening this. Deferring to #8667.

@Solaris-star

Copy link
Copy Markdown
Author

Fair point, and thank you for saying it directly rather than just closing it. You're right on both counts: I should have checked that the issue reporter was already working on a fix before racing to open this, and grabbing well-scoped trivial issues isn't where the real value is. I've closed this in favor of #8667.

The code-review point is well taken — that is the unglamorous critical path, and I'll take it seriously rather than just optimizing for easy merges.

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.

Fix to add null handling to Baggage.fromContext() and Baggage.fromContextOrNull()

3 participants