Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,9 @@ public RpcContext setAttachment(String key, Object value) {
public RpcContext setObjectAttachment(String key, Object value) {
// TODO compatible with previous
CLIENT_ATTACHMENT.get().setObjectAttachment(key, value);
if (value != null && !(value instanceof String)) {
CLIENT_ATTACHMENT.get().setAttachment(key, String.valueOf(value));
}
Copy link

Copilot AI Dec 28, 2025

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.

Copilot uses AI. Check for mistakes.
return this;
}

Expand Down
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);
Copy link

Copilot AI Dec 28, 2025

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.

Copilot uses AI. Check for mistakes.

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());
}
Copy link

Copilot AI Dec 28, 2025

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.

Copilot uses AI. Check for mistakes.
}
}
Loading