Copilot commented on code in PR #15934: URL: https://github.com/apache/dubbo/pull/15934#discussion_r2649578102
########## dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/RpcContextCompatibilityTest.java: ########## @@ -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); Review Comment: 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. ########## dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/RpcContextCompatibilityTest.java: ########## @@ -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()); + } Review Comment: 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. ########## dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/RpcContext.java: ########## @@ -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)); + } Review Comment: 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. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
