-
Notifications
You must be signed in to change notification settings - Fork 26.6k
[Bug] Fix RpcContext compatibility between Object and String attachments #15934
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 3.3
Are you sure you want to change the base?
Conversation
…acy getAttachment API
There was a problem hiding this 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 attempts to fix a compatibility issue in RpcContext where non-String objects stored via setObjectAttachment could not be retrieved via the legacy getAttachment(String) method. The fix addresses a TODO comment about maintaining backward compatibility with the legacy API.
Key changes:
- Modified
RpcContext#setObjectAttachmentto store a String representation alongside non-String objects - Added unit test
RpcContextCompatibilityTestto verify the compatibility behavior
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/RpcContext.java | Added logic to store String representation of non-String objects for legacy API compatibility |
| dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/RpcContextCompatibilityTest.java | New test file to verify attachment compatibility between object and string APIs |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| Object legacyValue = context.getAttachment(key); | ||
| System.out.println("Legacy getAttachment result: " + legacyValue); |
Copilot
AI
Dec 28, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The test uses System.out.println for debugging instead of proper assertions. The test should verify that the legacy getAttachment method returns the expected String value "12345" using an assertion like Assertions.assertEquals("12345", legacyValue) to ensure the compatibility fix is working correctly.
| try { | ||
| for (Map.Entry<String, String> entry : allAttachments.entrySet()) { | ||
| String k = entry.getKey(); | ||
| String v = entry.getValue(); // 如果 entry.getValue() 实际是 Long,这里可能会崩 | ||
| System.out.println(k + " = " + v); | ||
| } | ||
| } catch (ClassCastException e) { | ||
| System.err.println("发现不兼容 Bug!由于底层存了非 String 对象,导致老接口遍历崩溃: " + e.getMessage()); | ||
| } |
Copilot
AI
Dec 28, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The test relies on System.out.println instead of proper assertions. The try-catch block should assert that no ClassCastException occurs when iterating over attachments, and verify that the String representation exists in the map. Consider using Assertions.assertDoesNotThrow and validating the map contents.
| if (value != null && !(value instanceof String)) { | ||
| CLIENT_ATTACHMENT.get().setAttachment(key, String.valueOf(value)); | ||
| } |
Copilot
AI
Dec 28, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This fix has a critical bug. When a non-String object is stored, the code first calls setObjectAttachment(key, value) which stores the object in the attachments map. Then it calls setAttachment(key, String.valueOf(value)), which delegates to setObjectAttachment and overwrites the original object with its String representation. This means getObjectAttachment(key) will return the String "12345" instead of the original Long object, breaking the intended behavior. The fix should store both representations without overwriting, or use a different storage mechanism.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## 3.3 #15934 +/- ##
============================================
+ Coverage 60.74% 60.76% +0.02%
+ Complexity 11713 11697 -16
============================================
Files 1938 1938
Lines 88698 88700 +2
Branches 13387 13388 +1
============================================
+ Hits 53876 53900 +24
+ Misses 29290 29272 -18
+ Partials 5532 5528 -4
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
wangchengming666
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
|
I’m a bit concerned that this PR will alter the existing behavior. Additionally, it’s odd that when I set an Integer value as an attachment and retrieve it using getAttachment (instead of getStringAttachment), I end up getting a String. |
Hi @RainYuY, thank you for your feedback! Regarding your concern, this PR is specifically intended to address the Currently, the legacy By performing a "read-time conversion" in
Do you think we should maintain the current behavior (returning null for non-String objects in the legacy API), or is this dynamic conversion a more robust way to fulfill the "compatible with previous" TODO? |
What is the purpose of the change?
This PR fixes the compatibility issue in
RpcContextwhere non-String objects stored viasetObjectAttachmentcould not be retrieved via the legacygetAttachment(String)method.Brief changelog
RpcContext#setObjectAttachmentto automatically store a String representation of the object for legacy API compatibility.Verifying this change
I have added a unit test
RpcContextCompatibilityTestto verify that:getObjectAttachmentreturns the original object (e.g., Long).getAttachmentreturns the String representation (e.g., "12345") instead of null.I have already sent my ICLA to [email protected]
Checklist