mirror of
https://github.com/corda/corda.git
synced 2025-06-22 17:09:00 +00:00
Fixed issues with incorrect serialisation in test utilities.
This commit is contained in:
@ -32,12 +32,6 @@ object JsonSupport {
|
|||||||
mapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY)
|
mapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY)
|
||||||
mapper.enable(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS)
|
mapper.enable(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS)
|
||||||
|
|
||||||
val timeModule = SimpleModule("java.time")
|
|
||||||
timeModule.addSerializer(LocalDate::class.java, ToStringSerializer)
|
|
||||||
timeModule.addDeserializer(LocalDate::class.java, LocalDateDeserializer)
|
|
||||||
timeModule.addKeyDeserializer(LocalDate::class.java, LocalDateKeyDeserializer)
|
|
||||||
timeModule.addSerializer(LocalDateTime::class.java, ToStringSerializer)
|
|
||||||
|
|
||||||
val cordaModule = SimpleModule("core")
|
val cordaModule = SimpleModule("core")
|
||||||
cordaModule.addSerializer(Party::class.java, PartySerializer)
|
cordaModule.addSerializer(Party::class.java, PartySerializer)
|
||||||
cordaModule.addDeserializer(Party::class.java, PartyDeserializer)
|
cordaModule.addDeserializer(Party::class.java, PartyDeserializer)
|
||||||
@ -62,12 +56,23 @@ object JsonSupport {
|
|||||||
cordaModule.addSerializer(NodeInfo::class.java, NodeInfoSerializer)
|
cordaModule.addSerializer(NodeInfo::class.java, NodeInfoSerializer)
|
||||||
cordaModule.addDeserializer(NodeInfo::class.java, NodeInfoDeserializer)
|
cordaModule.addDeserializer(NodeInfo::class.java, NodeInfoDeserializer)
|
||||||
|
|
||||||
mapper.registerModule(timeModule)
|
mapper.registerModule(createJavaTimeModule())
|
||||||
mapper.registerModule(cordaModule)
|
mapper.registerModule(cordaModule)
|
||||||
mapper.registerModule(KotlinModule())
|
mapper.registerModule(KotlinModule())
|
||||||
return mapper
|
return mapper
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun createJavaTimeModule(): Module {
|
||||||
|
val timeModule = SimpleModule("java.time")
|
||||||
|
timeModule.apply {
|
||||||
|
addSerializer(LocalDate::class.java, ToStringSerializer)
|
||||||
|
addDeserializer(LocalDate::class.java, LocalDateDeserializer)
|
||||||
|
addKeyDeserializer(LocalDate::class.java, LocalDateKeyDeserializer)
|
||||||
|
addSerializer(LocalDateTime::class.java, ToStringSerializer)
|
||||||
|
}
|
||||||
|
return timeModule
|
||||||
|
}
|
||||||
|
|
||||||
class ServiceHubObjectMapper(val identities: IdentityService) : ObjectMapper()
|
class ServiceHubObjectMapper(val identities: IdentityService) : ObjectMapper()
|
||||||
|
|
||||||
object ToStringSerializer : JsonSerializer<Any>() {
|
object ToStringSerializer : JsonSerializer<Any>() {
|
||||||
|
@ -23,7 +23,7 @@ class HttpApi(val root: URL) {
|
|||||||
*/
|
*/
|
||||||
inline fun<reified T: Any> getJson(path: String, params: Map<String, String> = mapOf()) = HttpUtils.getJson<T>(URL(root, path), params)
|
inline fun<reified T: Any> getJson(path: String, params: Map<String, String> = mapOf()) = HttpUtils.getJson<T>(URL(root, path), params)
|
||||||
|
|
||||||
private fun toJson(any: Any) = any as? String ?: ObjectMapper().writeValueAsString(any)
|
private fun toJson(any: Any) = any as? String ?: HttpUtils.createDefaultMapper().writeValueAsString(any)
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
fun fromHostAndPort(hostAndPort: HostAndPort, base: String, protocol: String = "http"): HttpApi
|
fun fromHostAndPort(hostAndPort: HostAndPort, base: String, protocol: String = "http"): HttpApi
|
||||||
|
@ -3,6 +3,7 @@ package net.corda.testing.http
|
|||||||
import com.fasterxml.jackson.databind.ObjectMapper
|
import com.fasterxml.jackson.databind.ObjectMapper
|
||||||
import com.fasterxml.jackson.module.kotlin.KotlinModule
|
import com.fasterxml.jackson.module.kotlin.KotlinModule
|
||||||
import net.corda.core.utilities.loggerFor
|
import net.corda.core.utilities.loggerFor
|
||||||
|
import net.corda.node.utilities.JsonSupport
|
||||||
import okhttp3.*
|
import okhttp3.*
|
||||||
import java.net.URL
|
import java.net.URL
|
||||||
import java.util.concurrent.TimeUnit
|
import java.util.concurrent.TimeUnit
|
||||||
@ -31,7 +32,7 @@ object HttpUtils {
|
|||||||
inline fun<reified T: Any> getJson(url: URL, params: Map<String, String> = mapOf()) : T {
|
inline fun<reified T: Any> getJson(url: URL, params: Map<String, String> = mapOf()) : T {
|
||||||
val paramString = if(params.isEmpty()) "" else "?" + params.map { "${it.key}=${it.value}" }.joinToString("&")
|
val paramString = if(params.isEmpty()) "" else "?" + params.map { "${it.key}=${it.value}" }.joinToString("&")
|
||||||
val parameterisedUrl = URL(url.toExternalForm() + paramString)
|
val parameterisedUrl = URL(url.toExternalForm() + paramString)
|
||||||
return ObjectMapper().registerModule(KotlinModule()).readValue(parameterisedUrl, T::class.java)
|
return createDefaultMapper().readValue(parameterisedUrl, T::class.java)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun makeRequest(request: Request): Boolean {
|
private fun makeRequest(request: Request): Boolean {
|
||||||
@ -44,14 +45,7 @@ object HttpUtils {
|
|||||||
return response.isSuccessful
|
return response.isSuccessful
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun getRequest(request: Request): Pair<Boolean, String> {
|
fun createDefaultMapper(): ObjectMapper {
|
||||||
val response = client.newCall(request).execute()
|
return ObjectMapper().registerModule(JsonSupport.createJavaTimeModule()).registerModule(KotlinModule())
|
||||||
|
|
||||||
if (!response.isSuccessful) {
|
|
||||||
logger.error("Could not fulfill HTTP request of type ${request.method()} to ${request.url()}. Status Code: ${response.code()}. Message: ${response.body().string()}")
|
|
||||||
return Pair(false, "")
|
|
||||||
}
|
|
||||||
|
|
||||||
return Pair(true, response.body().string())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user