Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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 @@ -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;
Expand Down Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, String> allAttachments = context.getAttachments();

Assertions.assertDoesNotThrow(
() -> {
for (Map.Entry<String, String> entry : allAttachments.entrySet()) {
String k = entry.getKey();

String v = entry.getValue();
Assertions.assertNotNull(v);
}
},
"Iterating legacy attachments should not throw ClassCastException");
}
}
Loading