This is an automated email from the ASF dual-hosted git repository.
Aias00 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shenyu.git
The following commit(s) were added to refs/heads/master by this push:
new f55287416e fix(common): copy builder state in the CacheUpstream
constructor (#7325)
f55287416e is described below
commit f55287416ec04955a07839b71368485a04b5f737
Author: Sean-Walker0 <[email protected]>
AuthorDate: Wed Sep 30 11:03:46 2026 +0800
fix(common): copy builder state in the CacheUpstream constructor (#7325)
* fix(common): copy builder state in the CacheUpstream constructor
CacheUpstream(Builder) had an empty body, so CacheUpstream.builder()...
build() always returned an all-defaults instance and silently discarded
every configured value. The sibling DubboUpstream/SofaUpstream builder
constructors copy each field, confirming the intended pattern that this
class forgot. Current callers only reach CacheUpstream through Gson
deserialization (CachePluginDataHandler), so the defect is latent, but
any builder-based construction produces a default-protocol default-url
upstream with no error.
The new test fails on current master (expected host-1 but was null) and
passes with this change.
* fix(common): complete the CacheUpstream builder fluent API
Fold in the review nit: password, master, mode, maxIdle, minIdle,
maxActive and maxWait were copied from the builder by the previous
commit but had no fluent setters, leaving them unreachable. Add the
missing methods so every builder field is settable, and extend the test
to assert each one survives build().
---------
Co-authored-by: Sean-Walker0
<[email protected]>
---
.../common/dto/convert/selector/CacheUpstream.java | 96 ++++++++++++++++++++++
.../dto/convert/selector/CacheUpstreamTest.java | 65 +++++++++++++++
2 files changed, 161 insertions(+)
diff --git
a/shenyu-common/src/main/java/org/apache/shenyu/common/dto/convert/selector/CacheUpstream.java
b/shenyu-common/src/main/java/org/apache/shenyu/common/dto/convert/selector/CacheUpstream.java
index c97d3b1567..8d5869a11b 100644
---
a/shenyu-common/src/main/java/org/apache/shenyu/common/dto/convert/selector/CacheUpstream.java
+++
b/shenyu-common/src/main/java/org/apache/shenyu/common/dto/convert/selector/CacheUpstream.java
@@ -80,6 +80,25 @@ public class CacheUpstream extends CommonUpstream {
* @param builder builder
*/
public CacheUpstream(final Builder builder) {
+ boolean statusValue = builder.statusValue;
+ if (!builder.statusSet) {
+ statusValue = defaultStatus();
+ }
+ setUpstreamHost(builder.upstreamHost);
+ setProtocol(builder.protocol);
+ setUpstreamUrl(builder.upstreamUrl);
+ setStatus(statusValue);
+ setTimestamp(builder.timestamp);
+ this.cacheType = builder.cacheType;
+ this.url = builder.url;
+ this.password = builder.password;
+ this.database = builder.database;
+ this.master = builder.master;
+ this.mode = builder.mode;
+ this.maxIdle = builder.maxIdle;
+ this.minIdle = builder.minIdle;
+ this.maxActive = builder.maxActive;
+ this.maxWait = builder.maxWait;
}
/**
@@ -504,6 +523,83 @@ public class CacheUpstream extends CommonUpstream {
this.database = database;
return this;
}
+
+ /**
+ * build password.
+ *
+ * @param password password
+ * @return this
+ */
+ public Builder password(final String password) {
+ this.password = password;
+ return this;
+ }
+
+ /**
+ * build master.
+ *
+ * @param master master
+ * @return this
+ */
+ public Builder master(final String master) {
+ this.master = master;
+ return this;
+ }
+
+ /**
+ * build mode.
+ *
+ * @param mode mode
+ * @return this
+ */
+ public Builder mode(final String mode) {
+ this.mode = mode;
+ return this;
+ }
+
+ /**
+ * build maxIdle.
+ *
+ * @param maxIdle maxIdle
+ * @return this
+ */
+ public Builder maxIdle(final int maxIdle) {
+ this.maxIdle = maxIdle;
+ return this;
+ }
+
+ /**
+ * build minIdle.
+ *
+ * @param minIdle minIdle
+ * @return this
+ */
+ public Builder minIdle(final int minIdle) {
+ this.minIdle = minIdle;
+ return this;
+ }
+
+ /**
+ * build maxActive.
+ *
+ * @param maxActive maxActive
+ * @return this
+ */
+ public Builder maxActive(final int maxActive) {
+ this.maxActive = maxActive;
+ return this;
+ }
+
+ /**
+ * build maxWait.
+ *
+ * @param maxWait maxWait
+ * @return this
+ */
+ public Builder maxWait(final int maxWait) {
+ this.maxWait = maxWait;
+ return this;
+ }
}
}
diff --git
a/shenyu-common/src/test/java/org/apache/shenyu/common/dto/convert/selector/CacheUpstreamTest.java
b/shenyu-common/src/test/java/org/apache/shenyu/common/dto/convert/selector/CacheUpstreamTest.java
new file mode 100644
index 0000000000..0d5e968048
--- /dev/null
+++
b/shenyu-common/src/test/java/org/apache/shenyu/common/dto/convert/selector/CacheUpstreamTest.java
@@ -0,0 +1,65 @@
+/*
+ * 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.shenyu.common.dto.convert.selector;
+
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+
+/**
+ * Test case for CacheUpstream.
+ */
+public final class CacheUpstreamTest {
+
+ @Test
+ public void testBuilderCopiesAllStateIntoBuiltInstance() {
+ CacheUpstream upstream = CacheUpstream.builder()
+ .upstreamHost("host-1")
+ .protocol("http")
+ .upstreamUrl("localhost:6379")
+ .status(false)
+ .timestamp(1650549243L)
+ .cacheType("redis")
+ .url("localhost:6379")
+ .password("secret")
+ .database("0")
+ .master("mymaster")
+ .mode("cluster")
+ .maxIdle(8)
+ .minIdle(2)
+ .maxActive(16)
+ .maxWait(3000)
+ .build();
+ assertEquals("host-1", upstream.getUpstreamHost());
+ assertEquals("http", upstream.getProtocol());
+ assertEquals("localhost:6379", upstream.getUpstreamUrl());
+ assertFalse(upstream.isStatus());
+ assertEquals(1650549243L, upstream.getTimestamp());
+ assertEquals("redis", upstream.getCacheType());
+ assertEquals("localhost:6379", upstream.getUrl());
+ assertEquals("secret", upstream.getPassword());
+ assertEquals("0", upstream.getDatabase());
+ assertEquals("mymaster", upstream.getMaster());
+ assertEquals("cluster", upstream.getMode());
+ assertEquals(8, upstream.getMaxIdle());
+ assertEquals(2, upstream.getMinIdle());
+ assertEquals(16, upstream.getMaxActive());
+ assertEquals(3000, upstream.getMaxWait());
+ }
+}