Skip to content
This repository was archived by the owner on Dec 4, 2025. It is now read-only.

Commit 7891359

Browse files
committed
fix: Fix incorrect display of non-JSON enum and conversion configuration errors when pushing to Torna
1 parent 91cd0d5 commit 7891359

3 files changed

Lines changed: 71 additions & 20 deletions

File tree

src/main/java/com/ly/doc/model/torna/TornaRequestInfo.java

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import java.io.UnsupportedEncodingException;
2424
import java.net.URLDecoder;
25+
import java.nio.charset.StandardCharsets;
2526
import java.util.HashMap;
2627

2728
import com.ly.doc.constants.TornaConstants;
@@ -92,25 +93,47 @@ public TornaRequestInfo setResponseInfo(String responseInfo) {
9293
return this;
9394
}
9495

96+
/**
97+
* Builds a full log string including both request and response data.
98+
* @return Formatted log string with request and response details
99+
*/
95100
public String buildInfo() {
96-
StringBuilder sb = new StringBuilder();
97-
sb.append("---------------------------PUSH START---------------------------\n")
101+
return buildLogContent(true, "=============== REQUEST & RESPONSE LOG END ===============");
102+
}
103+
104+
/**
105+
* Builds a log string containing only request data (no response).
106+
* @return Formatted log string with request details only
107+
*/
108+
public String buildRequestInfo() {
109+
return buildLogContent(false, "==================== REQUEST LOG END ====================");
110+
}
111+
112+
/**
113+
* Shared method to construct log content dynamically.
114+
* @param includeResponse Whether to include response data
115+
* @param closingMarker Custom closing boundary marker
116+
*/
117+
private String buildLogContent(boolean includeResponse, String closingMarker) {
118+
StringBuilder sb = new StringBuilder().append("==================== PUSH LOG START ====================\n")
98119
.append("API: ")
99120
.append(category)
100121
.append("\n")
101122
.append("Request Param: \n")
102123
.append(TornaConstants.GSON.toJson(requestInfo))
103-
.append("\n")
104-
.append("Response: \n")
105-
.append(TornaConstants.GSON.fromJson(responseInfo, HashMap.class))
106-
.append("\n")
107-
.append("---------------------------PUSH END---------------------------\n");
124+
.append("\n");
125+
126+
if (includeResponse) {
127+
sb.append("Response: \n").append(TornaConstants.GSON.fromJson(responseInfo, HashMap.class)).append("\n");
128+
}
129+
130+
sb.append(closingMarker).append("\n"); // Custom closing marker
131+
108132
try {
109-
return URLDecoder.decode(sb.toString(), "utf-8");
133+
return URLDecoder.decode(sb.toString(), StandardCharsets.UTF_8.name());
110134
}
111135
catch (UnsupportedEncodingException e) {
112-
e.printStackTrace();
113-
return "";
136+
return ""; // In production, log this error (e.g., via SLF4J)
114137
}
115138
}
116139

src/main/java/com/ly/doc/utils/JavaClassUtil.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ public static Object getDefaultEnumValue(JavaClass javaClass, ProjectDocConfigBu
392392
}
393393

394394
// Default handling for enum values
395-
return processDefaultEnumFields(enumConstant);
395+
return processDefaultEnumFields(enumConstant, builder);
396396
}
397397

398398
/**
@@ -501,7 +501,12 @@ private static Optional<JavaField> findFieldWithJsonValue(JavaClass javaClass) {
501501
* @param javaField The JavaField object representing the enum constant
502502
* @return The value based on the enum field processing logic
503503
*/
504-
private static Object processDefaultEnumFields(JavaField javaField) {
504+
private static Object processDefaultEnumFields(JavaField javaField, ProjectDocConfigBuilder builder) {
505+
ApiConfig apiConfig = builder.getApiConfig();
506+
if (!apiConfig.isEnumConvertor()) {
507+
return javaField.getName();
508+
}
509+
505510
String initializationExpression = javaField.getInitializationExpression();
506511

507512
if (StringUtils.isBlank(initializationExpression)) {

src/main/java/com/ly/doc/utils/TornaUtil.java

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ public static void pushToTorna(TornaApi tornaApi, ApiConfig apiConfig, JavaProje
126126
private static void pushToTornaAll(TornaApi tornaApi, ApiConfig apiConfig, JavaProjectBuilder builder) {
127127
// Build push document information
128128
Map<String, String> requestJson = TornaConstants.buildParams(PUSH, new Gson().toJson(tornaApi), apiConfig);
129+
TornaUtil.printDebugInfo(apiConfig, null, requestJson, PUSH, true);
129130
// Push dictionary information
130131
Map<String, Object> dicMap = new HashMap<>(2);
131132
List<TornaDic> docDicts = TornaUtil.buildTornaDic(DocUtil.buildDictionary(apiConfig, builder));
@@ -190,23 +191,45 @@ public static boolean setDebugEnv(ApiConfig apiConfig, TornaApi tornaApi) {
190191
*/
191192
public static void printDebugInfo(ApiConfig apiConfig, String responseMsg, Map<String, String> requestJson,
192193
String category) {
194+
printDebugInfo(apiConfig, responseMsg, requestJson, category, false);
195+
}
196+
197+
/**
198+
* Prints debug information with mode support.
199+
* @param apiConfig The API configuration object containing OpenUrl, appToken, etc.
200+
* @param responseMsg The response message, null for pre-request mode.
201+
* @param requestJson The request JSON object in key-value pairs.
202+
* @param category The category of the request or response for classifying debug
203+
* information.
204+
* @param isPreRequest true for pre-request mode, false for post-request mode.
205+
*/
206+
public static void printDebugInfo(ApiConfig apiConfig, String responseMsg, Map<String, String> requestJson,
207+
String category, boolean isPreRequest) {
193208
if (apiConfig.isTornaDebug()) {
194209
String sb = "Configuration information : \n" + "OpenUrl: " + apiConfig.getOpenUrl() + "\n" + "appToken: "
195210
+ apiConfig.getAppToken() + "\n";
196211
System.out.println(sb);
197-
try {
198-
JsonElement element = JsonParser.parseString(responseMsg);
212+
if (isPreRequest) {
199213
TornaRequestInfo info = new TornaRequestInfo().of()
200214
.setCategory(category)
201-
.setCode(element.getAsJsonObject().get(TornaConstants.CODE).getAsString())
202-
.setMessage(element.getAsJsonObject().get(TornaConstants.MESSAGE).getAsString())
203215
.setRequestInfo(requestJson)
204216
.setResponseInfo(responseMsg);
205-
System.out.println(info.buildInfo());
217+
System.out.println(info.buildRequestInfo());
206218
}
207-
catch (Exception e) {
208-
// Ex : Nginx Error,Tomcat Error
209-
System.out.println("Response Error : \n" + responseMsg);
219+
else {
220+
try {
221+
JsonElement element = JsonParser.parseString(responseMsg);
222+
TornaRequestInfo info = new TornaRequestInfo().of()
223+
.setCategory(category)
224+
.setCode(element.getAsJsonObject().get(TornaConstants.CODE).getAsString())
225+
.setMessage(element.getAsJsonObject().get(TornaConstants.MESSAGE).getAsString())
226+
.setRequestInfo(requestJson)
227+
.setResponseInfo(responseMsg);
228+
System.out.println(info.buildInfo());
229+
}
230+
catch (Exception e) {
231+
System.out.println("Response Error : \n" + responseMsg);
232+
}
210233
}
211234
}
212235
}

0 commit comments

Comments
 (0)