This is an automated email from the ASF dual-hosted git repository.
Alanxtl pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/dubbo-go.git
The following commit(s) were added to refs/heads/develop by this push:
new 38520f3e3 fix(common): omit trailing ? in URL.String() without query
params (#3328)
38520f3e3 is described below
commit 38520f3e3a07c60895c685d0dbb1ec6b1d748cc0
Author: 吴杨帆 <[email protected]>
AuthorDate: Mon Jun 8 15:18:50 2026 +0800
fix(common): omit trailing ? in URL.String() without query params (#3328)
* fix(common): omit trailing ? in URL.String() without params
Closes #3327. Only append a query string when URL parameters exist.
* test: update consistent hash url expectation
---------
Co-authored-by: wuyangfan <[email protected]>
Co-authored-by: Xuetao Li <[email protected]>
---
cluster/loadbalance/consistenthashing/loadbalance_test.go | 2 +-
common/url.go | 9 ++++++---
common/url_test.go | 6 ++++++
3 files changed, 13 insertions(+), 4 deletions(-)
diff --git a/cluster/loadbalance/consistenthashing/loadbalance_test.go
b/cluster/loadbalance/consistenthashing/loadbalance_test.go
index f0c48d0ad..95b4a71a5 100644
--- a/cluster/loadbalance/consistenthashing/loadbalance_test.go
+++ b/cluster/loadbalance/consistenthashing/loadbalance_test.go
@@ -76,7 +76,7 @@ func (s *consistentHashSelectorSuite) TestSelectForKey() {
s.selector.virtualInvokers[9999945] = base.NewBaseInvoker(url2)
s.selector.keys = []uint32{99874, 9999945}
result := s.selector.selectForKey(9999944)
- s.Equal(url8081Short+"?", result.GetURL().String())
+ s.Equal(url8081Short, result.GetURL().String())
}
func TestConsistentHashLoadBalanceSuite(t *testing.T) {
diff --git a/common/url.go b/common/url.go
index fbcb4c860..4ae557f92 100644
--- a/common/url.go
+++ b/common/url.go
@@ -399,11 +399,14 @@ func (c *URL) String() string {
defer c.paramsLock.Unlock()
var buf strings.Builder
if len(c.Username) == 0 && len(c.Password) == 0 {
- buf.WriteString(fmt.Sprintf("%s://%s:%s%s?", c.Protocol, c.Ip,
c.Port, c.Path))
+ buf.WriteString(fmt.Sprintf("%s://%s:%s%s", c.Protocol, c.Ip,
c.Port, c.Path))
} else {
- buf.WriteString(fmt.Sprintf("%s://%s:%s@%s:%s%s?", c.Protocol,
c.Username, c.Password, c.Ip, c.Port, c.Path))
+ buf.WriteString(fmt.Sprintf("%s://%s:%s@%s:%s%s", c.Protocol,
c.Username, c.Password, c.Ip, c.Port, c.Path))
+ }
+ if encoded := c.params.Encode(); encoded != "" {
+ buf.WriteByte('?')
+ buf.WriteString(encoded)
}
- buf.WriteString(c.params.Encode())
return buf.String()
}
diff --git a/common/url_test.go b/common/url_test.go
index 929de014a..f77333225 100644
--- a/common/url_test.go
+++ b/common/url_test.go
@@ -1295,6 +1295,12 @@ func TestURLStringWithoutAuth(t *testing.T) {
assert.NotContains(t, str, "@")
}
+func TestURLStringWithoutQuery(t *testing.T) {
+ u, err := NewURL("dubbo://127.0.0.1:20000/com.test.Service")
+ require.NoError(t, err)
+ assert.Equal(t, "dubbo://127.0.0.1:20000/com.test.Service", u.String())
+}
+
func TestGetParamAndDecodedError(t *testing.T) {
u := &URL{}
params := url.Values{}