Skip to content

Commit

Permalink
[Dubbo-7375]fix url add repeatedly (#7376)
Browse files Browse the repository at this point in the history
* fix url add repeatedly

* use iterator to traverse parameters map

* remove unused import

* import static CommonConstants.TIMESTAMP_KEY

Co-authored-by: changye <[email protected]>
  • Loading branch information
kevinw66 and changye authored Mar 18, 2021
1 parent 12aad84 commit c7cee8d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
20 changes: 17 additions & 3 deletions dubbo-common/src/main/java/org/apache/dubbo/common/URL.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import org.apache.dubbo.common.config.Configuration;
import org.apache.dubbo.common.config.InmemoryConfiguration;
import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.common.constants.RemotingConstants;
import org.apache.dubbo.common.utils.ArrayUtils;
import org.apache.dubbo.common.utils.CollectionUtils;
Expand Down Expand Up @@ -56,6 +55,7 @@
import static org.apache.dubbo.common.constants.CommonConstants.PATH_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.PORT_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.PROTOCOL_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.TIMESTAMP_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.USERNAME_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.VERSION_KEY;
import static org.apache.dubbo.common.convert.Converter.convertIfPossible;
Expand Down Expand Up @@ -1659,8 +1659,9 @@ public Configuration toConfiguration() {
public int hashCode() {
final int prime = 31;
int result = 1;

result = prime * result + ((host == null) ? 0 : host.hashCode());
result = prime * result + ((parameters == null) ? 0 : parameters.hashCode());
result = prime * result + ((parameters == null) ? 0 : parametersHashCode());
result = prime * result + ((password == null) ? 0 : password.hashCode());
result = prime * result + ((path == null) ? 0 : path.hashCode());
result = prime * result + port;
Expand Down Expand Up @@ -1692,7 +1693,7 @@ public boolean equals(Object obj) {
return false;
} else {
for (String key : parameters.keySet()) {
if (key.equals(CommonConstants.TIMESTAMP_KEY)) {
if (key.equals(TIMESTAMP_KEY)) {
continue;
}
if (!parameters.get(key).equals(other.parameters.get(key))) {
Expand All @@ -1719,6 +1720,19 @@ public boolean equals(Object obj) {
return true;
}

private int parametersHashCode() {
int h = 0;
for (Map.Entry<String, String> next : parameters.entrySet()) {
if (TIMESTAMP_KEY.equals(next.getKey())) {
continue;
}

h += next.hashCode();
}

return h;
}

public static void putMethodParameter(String method, String key, String value, Map<String, Map<String, String>> methodParameters) {
Map<String, String> subParameter = methodParameters.computeIfAbsent(method, k -> new HashMap<>());
subParameter.put(key, value);
Expand Down
21 changes: 21 additions & 0 deletions dubbo-common/src/test/java/org/apache/dubbo/common/URLTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -921,6 +921,27 @@ public void testEquals() {
assertNotEquals(url3, url4);
}

@Test
public void testHashcode() {
URL url1 = URL.valueOf("consumer://30.225.20.150/org.apache.dubbo.rpc.service.GenericService?application=" +
"dubbo-demo-api-consumer&category=consumers&check=false&dubbo=2.0.2&generic=true&interface=" +
"org.apache.dubbo.demo.DemoService&pid=7375&side=consumer&sticky=false&timestamp=1599556506417");
URL url2 = URL.valueOf("consumer://30.225.20.150/org.apache.dubbo.rpc.service.GenericService?application=" +
"dubbo-demo-api-consumer&category=consumers&check=false&dubbo=2.0.2&generic=true&interface=" +
"org.apache.dubbo.demo.DemoService&pid=7375&side=consumer&sticky=false&timestamp=2299556506417");
assertEquals(url1.hashCode(), url2.hashCode());

URL url3 = URL.valueOf("consumer://30.225.20.150/org.apache.dubbo.rpc.service.GenericService?application=" +
"dubbo-demo-api-consumer&category=consumers&check=false&dubbo=2.0.2&interface=" +
"org.apache.dubbo.demo.DemoService&pid=7375&side=consumer&sticky=false&timestamp=2299556506417");
assertNotEquals(url2.hashCode(), url3.hashCode());

URL url4 = URL.valueOf("consumer://30.225.20.150/org.apache.dubbo.rpc.service.GenericService?application=" +
"dubbo-demo-api-consumer&category=consumers&check=true&dubbo=2.0.2&interface=" +
"org.apache.dubbo.demo.DemoService&pid=7375&side=consumer&sticky=false&timestamp=2299556506417");
assertNotEquals(url3.hashCode(), url4.hashCode());
}

@Test
public void testEqualsWithPassword() {
URL url1 = URL.valueOf("ad@min:hello@[email protected]:20880/context/path?version=1.0.0&application=morgan");
Expand Down

0 comments on commit c7cee8d

Please sign in to comment.