This is an automated email from the ASF dual-hosted git repository.
liubao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/servicecomb-java-chassis.git
The following commit(s) were added to refs/heads/master by this push:
new 7faf4bccf [SCB-2465]Fix InvocationContext merge logic encapsulation
violation (#2802)
7faf4bccf is described below
commit 7faf4bccf372a283980bf8a8965c1e844f2e8125
Author: liubao68 <[email protected]>
AuthorDate: Tue Apr 26 11:56:44 2022 +0800
[SCB-2465]Fix InvocationContext merge logic encapsulation violation (#2802)
---
.../swagger/invocation/context/InvocationContext.java | 18 ++++++------------
.../invocation/context/TestInvocationContext.java | 4 ++--
2 files changed, 8 insertions(+), 14 deletions(-)
diff --git
a/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/context/InvocationContext.java
b/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/context/InvocationContext.java
index 8a13c4967..9789752bd 100644
---
a/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/context/InvocationContext.java
+++
b/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/context/InvocationContext.java
@@ -19,7 +19,6 @@ package org.apache.servicecomb.swagger.invocation.context;
import java.util.HashMap;
import java.util.Map;
-import java.util.Map.Entry;
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.core.Response.StatusType;
@@ -32,9 +31,9 @@ public class InvocationContext {
protected StatusType httpStatus;
- protected Map<String, String> context = new HashMap<>();
+ protected final Map<String, String> context = new HashMap<>();
- protected Map<String, Object> localContext = new HashMap<>();
+ protected final Map<String, Object> localContext = new HashMap<>();
protected TransportContext transportContext;
@@ -47,7 +46,8 @@ public class InvocationContext {
}
public void setContext(Map<String, String> context) {
- this.context = context;
+ this.context.clear();
+ this.addContext(context);
}
public void addContext(String key, String value) {
@@ -78,13 +78,6 @@ public class InvocationContext {
if (otherContext == null) {
return;
}
- if (otherContext.size() > context.size()) {
- for (Entry<String, String> entry : context.entrySet()) {
- otherContext.putIfAbsent(entry.getKey(), entry.getValue());
- }
- this.context = otherContext;
- return;
- }
context.putAll(otherContext);
}
@@ -93,7 +86,8 @@ public class InvocationContext {
}
public void setLocalContext(Map<String, Object> localContext) {
- this.localContext = localContext;
+ this.localContext.clear();
+ this.addLocalContext(localContext);
}
public void addLocalContext(String key, Object value) {
diff --git
a/swagger/swagger-invocation/invocation-core/src/test/java/org/apache/servicecomb/swagger/invocation/context/TestInvocationContext.java
b/swagger/swagger-invocation/invocation-core/src/test/java/org/apache/servicecomb/swagger/invocation/context/TestInvocationContext.java
index 189b1af4f..166c2ad0f 100644
---
a/swagger/swagger-invocation/invocation-core/src/test/java/org/apache/servicecomb/swagger/invocation/context/TestInvocationContext.java
+++
b/swagger/swagger-invocation/invocation-core/src/test/java/org/apache/servicecomb/swagger/invocation/context/TestInvocationContext.java
@@ -41,7 +41,7 @@ public class TestInvocationContext {
InvocationContext invocationContext2 = new InvocationContext();
Map<String, String> otherContext2 = new HashMap<>();
otherContext2.put("key3", "value3");
- invocationContext2.context = otherContext2;
+ invocationContext2.setContext(otherContext2);
invocationContext.addContext(invocationContext2);
Assert.assertEquals(3, invocationContext.getContext().size());
}
@@ -69,7 +69,7 @@ public class TestInvocationContext {
Map<String, String> otherContext = new HashMap<>();
otherContext.put("key1", "value1");
InvocationContext context2 = new InvocationContext();
- context2.context = otherContext;
+ context2.setContext(otherContext);
invocationContext.mergeContext(context2);
Assert.assertEquals(1, invocationContext.getContext().size());
Assert.assertEquals("value1", invocationContext.getContext("key1"));