diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/RpcContext.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/RpcContext.java index 12d00716cf25..5cd4a8b5644f 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/RpcContext.java +++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/RpcContext.java @@ -19,7 +19,6 @@ import org.apache.dubbo.common.Experimental; import org.apache.dubbo.common.URL; import org.apache.dubbo.common.threadlocal.InternalThreadLocal; -import org.apache.dubbo.common.utils.StringUtils; import java.net.InetSocketAddress; import java.util.HashMap; @@ -533,11 +532,16 @@ public int getRemotePort() { * @return attachment */ public String getAttachment(String key) { - String client = CLIENT_ATTACHMENT.get().getAttachment(key); - if (StringUtils.isEmpty(client)) { - return SERVER_ATTACHMENT.get().getAttachment(key); + Object value = CLIENT_ATTACHMENT.get().getObjectAttachment(key); + if (value == null) { + value = SERVER_ATTACHMENT.get().getObjectAttachment(key); } - return client; + + if (value != null && !(value instanceof String)) { + return String.valueOf(value); + } + + return (String) value; } /** @@ -574,6 +578,7 @@ public RpcContext setAttachment(String key, Object value) { public RpcContext setObjectAttachment(String key, Object value) { // TODO compatible with previous CLIENT_ATTACHMENT.get().setObjectAttachment(key, value); + return this; } diff --git a/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/RpcContextCompatibilityTest.java b/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/RpcContextCompatibilityTest.java new file mode 100644 index 000000000000..e17325516348 --- /dev/null +++ b/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/RpcContextCompatibilityTest.java @@ -0,0 +1,52 @@ +/* + * 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); + Assertions.assertEquals("12345", legacyValue, "Legacy API should return String representation"); + + Map allAttachments = context.getAttachments(); + + Assertions.assertDoesNotThrow( + () -> { + for (Map.Entry entry : allAttachments.entrySet()) { + String k = entry.getKey(); + + String v = entry.getValue(); + Assertions.assertNotNull(v); + } + }, + "Iterating legacy attachments should not throw ClassCastException"); + } +}