Skip to content
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

fix getAttchments return copy map problem. #9571

Merged
merged 1 commit into from Jan 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -16,17 +16,19 @@
*/
package org.apache.dubbo.rpc.cluster.directory;

import org.apache.dubbo.rpc.AttachmentsAdapter;
import org.apache.dubbo.rpc.Invocation;
import org.apache.dubbo.rpc.Invoker;

import java.util.HashMap;
import java.util.Map;

import static org.apache.dubbo.common.constants.CommonConstants.DUBBO_VERSION_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.GROUP_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.PATH_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.TIMEOUT_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.VERSION_KEY;
import org.apache.dubbo.common.utils.MapUtils;
import static org.apache.dubbo.rpc.Constants.TOKEN_KEY;
import org.apache.dubbo.rpc.Invocation;
import org.apache.dubbo.rpc.Invoker;

/**
* MockInvocation.java
Expand Down Expand Up @@ -73,7 +75,7 @@ public Object[] getArguments() {
}

public Map<String, String> getAttachments() {
return MapUtils.objectToStringMap(attachments);
return new AttachmentsAdapter.ObjectToStringMap(attachments);
}

@Override
Expand All @@ -88,7 +90,7 @@ public void setAttachment(String key, String value) {

@Override
public void setAttachment(String key, Object value) {
setObjectAttachment(key, value);
setObjectAttachment(key, value);
}

@Override
Expand Down

This file was deleted.

Expand Up @@ -16,7 +16,7 @@
*/
package org.apache.dubbo.service;

import org.apache.dubbo.common.utils.MapUtils;
import org.apache.dubbo.rpc.AttachmentsAdapter;
import org.apache.dubbo.rpc.Invocation;
import org.apache.dubbo.rpc.Invoker;

Expand Down Expand Up @@ -79,7 +79,7 @@ public Object[] getArguments() {
}

public Map<String, String> getAttachments() {
return MapUtils.objectToStringMap(attachments);
return new AttachmentsAdapter.ObjectToStringMap(attachments);
}

@Override
Expand Down
Expand Up @@ -17,7 +17,6 @@
package org.apache.dubbo.rpc;


import org.apache.dubbo.common.utils.MapUtils;
import org.apache.dubbo.rpc.proxy.InvokerInvocationHandler;

import java.util.HashMap;
Expand Down Expand Up @@ -122,7 +121,7 @@ public boolean hasException() {
@Override
@Deprecated
public Map<String, String> getAttachments() {
return MapUtils.objectToStringMap(attachments);
return new AttachmentsAdapter.ObjectToStringMap(attachments);
}

@Override
Expand Down
Expand Up @@ -22,18 +22,12 @@
/**
* This class provides map adapters to support attachments in RpcContext, Invocation and Result switch from
* <String, String> to <String, Object>
*
* please use {@link org.apache.dubbo.common.utils.MapUtils}
*
*/
@Deprecated
public class AttachmentsAdapter {

@Deprecated
public static class ObjectToStringMap extends HashMap<String, String> {
private Map<String, Object> attachments;

@Deprecated
public ObjectToStringMap(Map<String, Object> attachments) {
for (Entry<String, Object> entry : attachments.entrySet()) {
String convertResult = convert(entry.getValue());
Expand All @@ -57,11 +51,10 @@ public String remove(Object key) {
}

private String convert(Object obj) {
if (obj == null) {
return null;
} else {
return obj.toString();
if (obj instanceof String) {
return (String) obj;
}
return null; // or JSON.toString(obj);
}

@Override
Expand Down
Expand Up @@ -20,7 +20,6 @@
import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.threadlocal.InternalThreadLocal;
import org.apache.dubbo.common.utils.CollectionUtils;
import org.apache.dubbo.common.utils.MapUtils;
import org.apache.dubbo.common.utils.NetUtils;
import org.apache.dubbo.common.utils.StringUtils;

Expand Down Expand Up @@ -547,15 +546,13 @@ public RpcContext removeAttachment(String key) {
}

/**
* get String type attachments.
*
* Best to use {{@link #getObjectAttachments()}}
* get attachments.
*
* @return attachments
*/
@Deprecated
public Map<String, String> getAttachments() {
return MapUtils.objectToStringMap(this.getObjectAttachments());
return new AttachmentsAdapter.ObjectToStringMap(this.getObjectAttachments());
}

/**
Expand Down
Expand Up @@ -17,7 +17,6 @@
package org.apache.dubbo.rpc;

import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.utils.MapUtils;
import org.apache.dubbo.common.utils.ReflectUtils;
import org.apache.dubbo.common.utils.StringUtils;
import org.apache.dubbo.rpc.model.ApplicationModel;
Expand Down Expand Up @@ -278,7 +277,7 @@ public void setAttachment(String key, String value) {
@Deprecated
@Override
public Map<String, String> getAttachments() {
return MapUtils.objectToStringMap(attachments);
return new AttachmentsAdapter.ObjectToStringMap(attachments);
}

@Deprecated
Expand Down
Expand Up @@ -117,6 +117,9 @@ public void testObjectAttachment() {
Assertions.assertEquals(1, response.getObjectAttachment("objectKey3"));
Assertions.assertEquals(3, response.getObjectAttachments().size());

response.getAttachments().put("objectKey4", "value4");
Assertions.assertEquals(response.getAttachment("objectKey4"), "value4");

HashMap<String, Object> map = new HashMap<>();
map.put("mapKey1", 1);
map.put("mapKey2", "mapValue2");
Expand Down
Expand Up @@ -108,8 +108,13 @@ public void testAttachments() {
Assertions.assertNull(context.getAttachment("_33"));
Assertions.assertEquals("3333", context.getAttachment(".33"));

context.getAttachments().put("44", "4444");
Assertions.assertEquals(context.getAttachment("44"), "4444");

context.clearAttachments();
Assertions.assertNull(context.getAttachment("_11"));


}

@Test
Expand Down
Expand Up @@ -37,6 +37,10 @@ public void testAttachment() {
Assertions.assertEquals(1, invocation.getObjectAttachment("objectKey3"));
Assertions.assertEquals(3, invocation.getObjectAttachments().size());

invocation.getAttachments().put("object4", "value4");
Assertions.assertEquals(invocation.getAttachment("object4"), "value4");


HashMap<String, Object> map = new HashMap<>();
map.put("mapKey1", 1);
map.put("mapKey2", "mapValue2");
Expand Down
Expand Up @@ -16,7 +16,7 @@
*/
package org.apache.dubbo.rpc.support;

import org.apache.dubbo.common.utils.MapUtils;
import org.apache.dubbo.rpc.AttachmentsAdapter;
import org.apache.dubbo.rpc.Invocation;
import org.apache.dubbo.rpc.Invoker;

Expand Down Expand Up @@ -75,7 +75,7 @@ public Object[] getArguments() {
}

public Map<String, String> getAttachments() {
return MapUtils.objectToStringMap(attachments);
return new AttachmentsAdapter.ObjectToStringMap(attachments);
}

@Override
Expand Down