Skip to content

Commit 3a1d807

Browse files
authored
🎨 Fix StackOverflowError in WxCpRedissonConfigImpl and WxCpRedisTemplateConfigImpl toString() methods
1 parent 2e952c7 commit 3a1d807

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/config/impl/AbstractWxCpInRedisConfigImpl.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,4 +180,17 @@ public boolean isAgentJsapiTicketExpired() {
180180
Long expire = redisOps.getExpire(this.agentJsapiTicketKey);
181181
return expire == null || expire < 2;
182182
}
183+
184+
@Override
185+
public String toString() {
186+
return "AbstractWxCpInRedisConfigImpl{" +
187+
"corpId='" + getCorpId() + '\'' +
188+
", agentId=" + getAgentId() +
189+
", keyPrefix='" + keyPrefix + '\'' +
190+
", accessTokenKey='" + accessTokenKey + '\'' +
191+
", jsapiTicketKey='" + jsapiTicketKey + '\'' +
192+
", agentJsapiTicketKey='" + agentJsapiTicketKey + '\'' +
193+
", lockKey='" + lockKey + '\'' +
194+
'}';
195+
}
183196
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package me.chanjar.weixin.cp.config.impl;
2+
3+
import me.chanjar.weixin.common.redis.WxRedisOps;
4+
5+
/**
6+
* Demonstration of the fix for toString() StackOverflowError issue
7+
*/
8+
public class DemoToStringFix {
9+
10+
public static void main(String[] args) {
11+
System.out.println("=== Demonstrating toString() Fix for WxCp Redis Config ===");
12+
13+
// Create a simple stub WxRedisOps implementation for testing
14+
WxRedisOps stubRedisOps = new WxRedisOps() {
15+
@Override
16+
public String getValue(String key) { return null; }
17+
@Override
18+
public void setValue(String key, String value, int expire, java.util.concurrent.TimeUnit timeUnit) {}
19+
@Override
20+
public Long getExpire(String key) { return null; }
21+
@Override
22+
public void expire(String key, int expire, java.util.concurrent.TimeUnit timeUnit) {}
23+
@Override
24+
public java.util.concurrent.locks.Lock getLock(String key) { return null; }
25+
};
26+
27+
// Test AbstractWxCpInRedisConfigImpl directly with our stub
28+
AbstractWxCpInRedisConfigImpl config = new AbstractWxCpInRedisConfigImpl(stubRedisOps, "demo:") {
29+
// Anonymous class to test the abstract parent
30+
};
31+
32+
config.setCorpId("demoCorpId");
33+
config.setAgentId(1001);
34+
35+
System.out.println("Testing toString() method:");
36+
try {
37+
String result = config.toString();
38+
System.out.println("✓ Success! toString() returned: " + result);
39+
System.out.println("✓ No StackOverflowError occurred");
40+
41+
// Verify the result contains expected information and excludes redisOps
42+
boolean containsCorpId = result.contains("demoCorpId");
43+
boolean containsAgentId = result.contains("1001");
44+
boolean containsKeyPrefix = result.contains("demo:");
45+
boolean excludesRedisOps = !result.contains("redisOps") && !result.contains("WxRedisOps");
46+
47+
System.out.println("✓ Contains corpId: " + containsCorpId);
48+
System.out.println("✓ Contains agentId: " + containsAgentId);
49+
System.out.println("✓ Contains keyPrefix: " + containsKeyPrefix);
50+
System.out.println("✓ Excludes redisOps: " + excludesRedisOps);
51+
52+
if (containsCorpId && containsAgentId && containsKeyPrefix && excludesRedisOps) {
53+
System.out.println("✓ All validations passed!");
54+
} else {
55+
System.out.println("✗ Some validations failed");
56+
}
57+
58+
} catch (StackOverflowError e) {
59+
System.out.println("✗ StackOverflowError still occurred - fix failed");
60+
} catch (Exception e) {
61+
System.out.println("✗ Unexpected error: " + e.getMessage());
62+
}
63+
64+
System.out.println("\n=== Demo completed ===");
65+
}
66+
}

0 commit comments

Comments
 (0)