Skip to content

Commit 140adac

Browse files
committed
ECWID-144513 safe deserializeOrNull implementation
1 parent 4ef36ef commit 140adac

5 files changed

Lines changed: 20 additions & 43 deletions

File tree

src/main/kotlin/com/ecwid/apiclient/v3/ApiClientHelper.kt

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ import com.ecwid.apiclient.v3.responsefields.responseFieldsOf
2323
import com.ecwid.apiclient.v3.util.buildEndpointPath
2424
import com.ecwid.apiclient.v3.util.createSecurePatterns
2525
import com.ecwid.apiclient.v3.util.maskLogString
26-
import com.google.gson.JsonParser
27-
import com.google.gson.JsonSyntaxException
2826
import java.net.URI
2927
import java.util.*
3028
import java.util.logging.Level
@@ -198,9 +196,8 @@ class ApiClientHelper private constructor(
198196
try {
199197
val responseBody = responseBytes.asString()
200198
logErrorResponseIfNeeded(requestId, requestTime, httpResponse.statusCode, responseBody)
201-
// Because of a html-based balancer error we should check responseBody string to be an actual json object
202-
val ecwidError = if (responseBody.isNotBlank() && isJsonObject(responseBody)) {
203-
jsonTransformer.deserialize(responseBody, EcwidApiError::class.java)
199+
val ecwidError = if (responseBody.isNotBlank()) {
200+
jsonTransformer.deserializeOrNull(responseBody, EcwidApiError::class.java)
204201
} else {
205202
null
206203
}
@@ -556,11 +553,3 @@ private fun createAdditionalDataPolymorphicType(): PolymorphicType<FetchedReport
556553
)
557554
)
558555
}
559-
560-
internal fun isJsonObject(input: String?): Boolean {
561-
return try {
562-
input?.let { JsonParser.parseString(it).isJsonObject } ?: false
563-
} catch (_: JsonSyntaxException) {
564-
false
565-
}
566-
}

src/main/kotlin/com/ecwid/apiclient/v3/jsontransformer/JsonTransformer.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ package com.ecwid.apiclient.v3.jsontransformer
22

33
interface JsonTransformer {
44
fun serialize(src: Any?, srcExt: Any? = null): String
5-
fun <V> deserialize(json: String, clazz: Class<V>): V?
5+
fun <V> deserialize(json: String, clazz: Class<V>): V
6+
fun <V> deserializeOrNull(json: String, clazz: Class<V>): V?
67
}

src/main/kotlin/com/ecwid/apiclient/v3/jsontransformer/gson/GsonTransformer.kt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,21 @@ class GsonTransformer(polymorphicTypes: List<PolymorphicType<*>>) : JsonTransfor
3636
}
3737
}
3838

39-
override fun <V> deserialize(json: String, clazz: Class<V>): V? {
39+
override fun <V> deserialize(json: String, clazz: Class<V>): V {
4040
try {
4141
return gson.fromJson(json, clazz)
4242
} catch (e: JsonParseException) {
4343
throw JsonDeserializationException(e.message, e)
4444
}
4545
}
46+
47+
override fun <V> deserializeOrNull(json: String, clazz: Class<V>): V? {
48+
return try {
49+
return gson.fromJson(json, clazz)
50+
} catch (e: JsonParseException) {
51+
null
52+
}
53+
}
4654
}
4755

4856
private fun JsonObject.mergeJsonObject(from: JsonObject) {

src/test/kotlin/com/ecwid/apiclient/v3/ApiClientHelperUnitTest.kt

Lines changed: 0 additions & 28 deletions
This file was deleted.

src/test/kotlin/com/ecwid/apiclient/v3/jsontransformer/gson/GsonTransformerTest.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,13 @@ internal class GsonTransformerTest {
230230
)
231231
}
232232

233+
@Test
234+
fun `deserializeOrNull of broken ParsedResponseWithExt`() {
235+
val json = "'testField': {'baseField': 'base', 'extField': 'ext'}}"
236+
val deserializedValue = transformer.deserializeOrNull(json, TestParsedResponseWithExt::class.java)
237+
assertEquals(null, deserializedValue)
238+
}
239+
233240
}
234241

235242
private fun assertJsonEquals(expected: String, actual: String) {

0 commit comments

Comments
 (0)