Skip to content

Commit e988e9d

Browse files
fix(client): don't call validate() during deserialization if we don't have to (#516)
1 parent dffd823 commit e988e9d

File tree

3 files changed

+25
-16
lines changed

3 files changed

+25
-16
lines changed

finch-java-core/src/main/kotlin/com/tryfinch/api/core/BaseDeserializer.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import com.fasterxml.jackson.databind.JsonMappingException
1111
import com.fasterxml.jackson.databind.JsonNode
1212
import com.fasterxml.jackson.databind.deser.ContextualDeserializer
1313
import com.fasterxml.jackson.databind.deser.std.StdDeserializer
14+
import com.tryfinch.api.errors.FinchInvalidDataException
1415
import kotlin.reflect.KClass
1516

1617
abstract class BaseDeserializer<T : Any>(type: KClass<T>) :
@@ -29,6 +30,13 @@ abstract class BaseDeserializer<T : Any>(type: KClass<T>) :
2930

3031
protected abstract fun ObjectCodec.deserialize(node: JsonNode): T
3132

33+
protected fun <T> ObjectCodec.deserialize(node: JsonNode, type: TypeReference<T>): T =
34+
try {
35+
readValue(treeAsTokens(node), type)
36+
} catch (e: Exception) {
37+
throw FinchInvalidDataException("Error deserializing", e)
38+
}
39+
3240
protected fun <T> ObjectCodec.tryDeserialize(
3341
node: JsonNode,
3442
type: TypeReference<T>,

finch-java-core/src/main/kotlin/com/tryfinch/api/models/DocumentRetreiveResponse.kt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -165,16 +165,16 @@ private constructor(
165165

166166
when (type) {
167167
"w4_2020" -> {
168-
tryDeserialize(node, jacksonTypeRef<W42020>()) { it.validate() }
169-
?.let {
170-
return DocumentRetreiveResponse(w42020 = it, _json = json)
171-
}
168+
return DocumentRetreiveResponse(
169+
w42020 = deserialize(node, jacksonTypeRef<W42020>()),
170+
_json = json,
171+
)
172172
}
173173
"w4_2005" -> {
174-
tryDeserialize(node, jacksonTypeRef<W42005>()) { it.validate() }
175-
?.let {
176-
return DocumentRetreiveResponse(w42005 = it, _json = json)
177-
}
174+
return DocumentRetreiveResponse(
175+
w42005 = deserialize(node, jacksonTypeRef<W42005>()),
176+
_json = json,
177+
)
178178
}
179179
}
180180

finch-java-core/src/main/kotlin/com/tryfinch/api/models/JobAutomatedCreateParams.kt

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -337,16 +337,17 @@ private constructor(
337337

338338
when (type) {
339339
"data_sync_all" -> {
340-
tryDeserialize(node, jacksonTypeRef<DataSyncAll>()) { it.validate() }
341-
?.let {
342-
return Body(dataSyncAll = it, _json = json)
343-
}
340+
return Body(
341+
dataSyncAll = deserialize(node, jacksonTypeRef<DataSyncAll>()),
342+
_json = json,
343+
)
344344
}
345345
"w4_form_employee_sync" -> {
346-
tryDeserialize(node, jacksonTypeRef<W4FormEmployeeSync>()) { it.validate() }
347-
?.let {
348-
return Body(w4FormEmployeeSync = it, _json = json)
349-
}
346+
return Body(
347+
w4FormEmployeeSync =
348+
deserialize(node, jacksonTypeRef<W4FormEmployeeSync>()),
349+
_json = json,
350+
)
350351
}
351352
}
352353

0 commit comments

Comments
 (0)