From b5f9140244e5b5842834defcedb07d778cdc5e9b Mon Sep 17 00:00:00 2001 From: shanzhenlong <1951661954@qq.com> Date: Sun, 5 Apr 2026 21:45:32 +0800 Subject: [PATCH 1/2] fix: log response body and status code on unmarshal failure When UnmarshalRespUtil.unmarshalResp() fails to deserialize the HTTP response, the original status code and response body are silently lost, making it impossible to diagnose the root cause (e.g. network gateway returning HTML instead of JSON). Changes: - Log a warning when HTTP status code is not 2xx - Log an error with status code and truncated body (max 500 chars) when JSON deserialization fails - Add unit tests covering success, invalid JSON, non-2xx with valid JSON, HTML response, and body truncation scenarios The response body is truncated to 500 characters to avoid leaking sensitive data and to prevent log flooding. Closes #163 Relates to #142, #133 --- .../oapi/core/utils/UnmarshalRespUtil.java | 27 +++++- .../core/utils/TestUnmarshalRespUtil.java | 97 +++++++++++++++++++ 2 files changed, 122 insertions(+), 2 deletions(-) create mode 100644 larksuite-oapi/src/test/java/com/lark/oapi/core/utils/TestUnmarshalRespUtil.java diff --git a/larksuite-oapi/src/main/java/com/lark/oapi/core/utils/UnmarshalRespUtil.java b/larksuite-oapi/src/main/java/com/lark/oapi/core/utils/UnmarshalRespUtil.java index b9c6d0fc4..d9191c824 100644 --- a/larksuite-oapi/src/main/java/com/lark/oapi/core/utils/UnmarshalRespUtil.java +++ b/larksuite-oapi/src/main/java/com/lark/oapi/core/utils/UnmarshalRespUtil.java @@ -13,13 +13,36 @@ package com.lark.oapi.core.utils; import com.lark.oapi.core.response.RawResponse; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.nio.charset.StandardCharsets; public class UnmarshalRespUtil { + private static final Logger log = LoggerFactory.getLogger(UnmarshalRespUtil.class); + private static final int MAX_LOG_BODY_LENGTH = 500; + public static T unmarshalResp(RawResponse resp, Class respClass) { - return Jsons.DEFAULT.fromJson(new String(resp.getBody(), StandardCharsets.UTF_8), - respClass); + String bodyStr = new String(resp.getBody(), StandardCharsets.UTF_8); + int statusCode = resp.getStatusCode(); + if (statusCode < 200 || statusCode >= 300) { + String truncatedBody = truncate(bodyStr); + log.warn("unexpected response status, statusCode={}, body={}", statusCode, truncatedBody); + } + try { + return Jsons.DEFAULT.fromJson(bodyStr, respClass); + } catch (Exception e) { + String truncatedBody = truncate(bodyStr); + log.error("unmarshal response error, statusCode={}, body={}", statusCode, truncatedBody); + throw e; + } + } + + private static String truncate(String body) { + if (body.length() > MAX_LOG_BODY_LENGTH) { + return body.substring(0, MAX_LOG_BODY_LENGTH) + "...(truncated)"; + } + return body; } } diff --git a/larksuite-oapi/src/test/java/com/lark/oapi/core/utils/TestUnmarshalRespUtil.java b/larksuite-oapi/src/test/java/com/lark/oapi/core/utils/TestUnmarshalRespUtil.java new file mode 100644 index 000000000..35ebd2142 --- /dev/null +++ b/larksuite-oapi/src/test/java/com/lark/oapi/core/utils/TestUnmarshalRespUtil.java @@ -0,0 +1,97 @@ +/* + * MIT License + * + * Copyright (c) 2022 Lark Technologies Pte. Ltd. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice, shall be included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +package com.lark.oapi.core.utils; + +import com.google.gson.JsonSyntaxException; +import com.google.gson.annotations.SerializedName; +import com.lark.oapi.core.response.RawResponse; +import org.junit.Test; + +import java.nio.charset.StandardCharsets; + +public class TestUnmarshalRespUtil { + + public static class SimpleResp { + @SerializedName("code") + private int code; + @SerializedName("msg") + private String msg; + + public int getCode() { return code; } + public String getMsg() { return msg; } + } + + @Test + public void testUnmarshalSuccess() { + RawResponse raw = new RawResponse(); + raw.setStatusCode(200); + raw.setBody("{\"code\":0,\"msg\":\"ok\"}".getBytes(StandardCharsets.UTF_8)); + + SimpleResp resp = UnmarshalRespUtil.unmarshalResp(raw, SimpleResp.class); + assert resp != null; + assert resp.getCode() == 0; + assert "ok".equals(resp.getMsg()); + } + + @Test(expected = JsonSyntaxException.class) + public void testUnmarshalInvalidJson() { + RawResponse raw = new RawResponse(); + raw.setStatusCode(502); + raw.setBody("Bad Gateway".getBytes(StandardCharsets.UTF_8)); + + UnmarshalRespUtil.unmarshalResp(raw, SimpleResp.class); + } + + @Test(expected = JsonSyntaxException.class) + public void testUnmarshalStringInsteadOfObject() { + RawResponse raw = new RawResponse(); + raw.setStatusCode(200); + raw.setBody("\"some error message\"".getBytes(StandardCharsets.UTF_8)); + + UnmarshalRespUtil.unmarshalResp(raw, SimpleResp.class); + } + + @Test(expected = JsonSyntaxException.class) + public void testUnmarshalHtmlResponse() { + RawResponse raw = new RawResponse(); + raw.setStatusCode(403); + raw.setBody("Forbidden".getBytes(StandardCharsets.UTF_8)); + + UnmarshalRespUtil.unmarshalResp(raw, SimpleResp.class); + } + + @Test(expected = JsonSyntaxException.class) + public void testUnmarshalLongBodyIsTruncatedInLog() { + RawResponse raw = new RawResponse(); + raw.setStatusCode(500); + StringBuilder sb = new StringBuilder("\""); + for (int i = 0; i < 600; i++) { + sb.append("x"); + } + sb.append("\""); + raw.setBody(sb.toString().getBytes(StandardCharsets.UTF_8)); + + UnmarshalRespUtil.unmarshalResp(raw, SimpleResp.class); + } + + @Test + public void testUnmarshalNon2xxWithValidJson() { + RawResponse raw = new RawResponse(); + raw.setStatusCode(400); + raw.setBody("{\"code\":99991400,\"msg\":\"param error\"}".getBytes(StandardCharsets.UTF_8)); + + SimpleResp resp = UnmarshalRespUtil.unmarshalResp(raw, SimpleResp.class); + assert resp != null; + assert resp.getCode() == 99991400; + } +} From d6724355c5642041d0a600ef7144a278e5d247e5 Mon Sep 17 00:00:00 2001 From: shanzhenlong <1951661954@qq.com> Date: Sun, 5 Apr 2026 21:47:10 +0800 Subject: [PATCH 2/2] fix: make skipTests configurable via Maven command line The surefire plugin had true hardcoded in its configuration block, which cannot be overridden by -DskipTests=false from the command line. Changed to use a Maven property reference ${skipTests} with a default value of true in , so that: - Default behavior is unchanged (tests are skipped) - Developers can run tests with: mvn test -DskipTests=false --- larksuite-oapi/pom.xml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/larksuite-oapi/pom.xml b/larksuite-oapi/pom.xml index 7c0d40662..9ac27116d 100644 --- a/larksuite-oapi/pom.xml +++ b/larksuite-oapi/pom.xml @@ -60,7 +60,7 @@ maven-surefire-plugin - true + ${skipTests} org.apache.maven.plugins ${maven-surefire-plugin.version} @@ -245,6 +245,7 @@ 2.6 3.0.1 2.12.4 + true 2.7 UTF-8 10-robolectric-5803371