-
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?
Changes from 1 commit
2404f50
d46d433
ec53537
c88c0fa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.apache.dubbo.rpc; | ||
|
|
||
| import java.util.Map; | ||
|
|
||
| import org.junit.jupiter.api.Assertions; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| public class RpcContextCompatibilityTest { | ||
|
|
||
| @Test | ||
| void testAttachmentCompatibility() { | ||
| RpcContext context = RpcContext.getServiceContext(); | ||
|
|
||
| String key = "test_long_key"; | ||
| Long value = 12345L; | ||
| context.setObjectAttachment(key, value); | ||
|
|
||
| Assertions.assertEquals(value, context.getObjectAttachment(key)); | ||
|
|
||
| Object legacyValue = context.getAttachment(key); | ||
| System.out.println("Legacy getAttachment result: " + legacyValue); | ||
|
||
|
|
||
| Map<String, String> allAttachments = context.getAttachments(); | ||
|
|
||
| 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()); | ||
| } | ||
|
||
| } | ||
| } | ||
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.