Skip to content

fix(json): avoid Gson 2.11+ Strictness to keep runtime compatibility - #228

Open
sls-dev-agent wants to merge 1 commit into
aliyun:non-fastjsonfrom
sls-dev-agent:fix/gson-strictness-compat
Open

fix(json): avoid Gson 2.11+ Strictness to keep runtime compatibility#228
sls-dev-agent wants to merge 1 commit into
aliyun:non-fastjsonfrom
sls-dev-agent:fix/gson-strictness-compat

Conversation

@sls-dev-agent

Copy link
Copy Markdown
Contributor

Problem

Querying logs on the non-fastjson line throws when the host application's
classpath resolves a Gson older than 2.11:

java.lang.NoClassDefFoundError: com/google/gson/Strictness
    at com.aliyun.openservices.log.internal.json.JsonTree.write(JsonTree.java:57)
    at com.aliyun.openservices.log.internal.json.JSONObject.toString(JSONObject.java:241)
    at com.aliyun.openservices.log.request.GetLogsRequest.getRequestBody(GetLogsRequest.java:422)
    at com.aliyun.openservices.log.Client.getLogsInternal(Client.java:1366)
    at com.aliyun.openservices.log.Client.GetLogs(Client.java:1370)

com.google.gson.Strictness and JsonReader/JsonWriter.setStrictness(...) were
introduced in Gson 2.11. The SDK declares Gson 2.13.2, but the plain
(non-shaded) artifact runs against whatever Gson the consuming app provides.
When dependency mediation picks an older Gson (< 2.11), loading Strictness
fails and every GetLogs call breaks.

Fix

Replace setStrictness(Strictness.STRICT) with the equivalent
setLenient(false) in JsonTree.parse / JsonTree.write. setLenient exists
in all Gson versions and maps to Strictness.STRICT in 2.11+, so behavior is
unchanged while the runtime floor drops back to the Gson versions the SDK
otherwise already relies on (setHtmlSafe, setSerializeNulls,
ToNumberPolicy). Added @SuppressWarnings("deprecation") since setLenient
is deprecated in the 2.13.2 the SDK compiles against.

Verification

  • mvn -B -Dtest='**/*Test,!**/functiontest/**,!ClientTest' test -> 160 tests, 0 failures.
  • mvn -B package -DskipTests -> BUILD SUCCESS (shaded artifact still attaches).
  • Reproduced the customer path (GetLogsRequest.getRequestBody()) with Gson
    2.8.9 forced on the classpath: succeeds after the fix (previously
    NoClassDefFoundError).

Version

Bumped to 0.6.161-non-fastjson.2 and updated the version references in
README/README_EN and the non-fastjson migration docs.

GetLogs threw NoClassDefFoundError: com/google/gson/Strictness when the
application classpath resolved an older Gson (< 2.11). Strictness and
setStrictness() only exist since Gson 2.11, but the SDK must run against
the Gson version the host app provides. Replace setStrictness(STRICT) with
the equivalent setLenient(false), which exists in all Gson versions, so the
plain (non-shaded) artifact no longer requires Gson 2.11+ at runtime.

Bump to 0.6.161-non-fastjson.2.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants