diff --git a/src/main/kotlin/io/kuzzle/sdk/Kuzzle.kt b/src/main/kotlin/io/kuzzle/sdk/Kuzzle.kt index 6b0524d0..66279c50 100644 --- a/src/main/kotlin/io/kuzzle/sdk/Kuzzle.kt +++ b/src/main/kotlin/io/kuzzle/sdk/Kuzzle.kt @@ -65,7 +65,7 @@ open class Kuzzle { private fun onMessageReceived(event: MessageReceivedEvent) { val message = event.message - val jsonObject: Map + var jsonObject: Map try { jsonObject = JsonSerializer.deserialize(message) as Map } catch (e: Exception) { @@ -77,6 +77,13 @@ open class Kuzzle { } return } + + // If the message is empty, we take the requestId of the event, + // to avoid error in fromMap function. + if (! jsonObject.containsKey("requestId") && event.requestId != null) { + jsonObject = jsonObject.plus("requestId" to event.requestId) + } + val response = Response().apply { fromMap(jsonObject) } diff --git a/src/main/kotlin/io/kuzzle/sdk/controllers/RealtimeController.kt b/src/main/kotlin/io/kuzzle/sdk/controllers/RealtimeController.kt index 2db8f9f5..fc45925e 100644 --- a/src/main/kotlin/io/kuzzle/sdk/controllers/RealtimeController.kt +++ b/src/main/kotlin/io/kuzzle/sdk/controllers/RealtimeController.kt @@ -25,7 +25,7 @@ class RealtimeController(kuzzle: Kuzzle) : BaseController(kuzzle) { ) init { - kuzzle.protocol.addListener() { + kuzzle.protocol.addListener { val response = Response().apply { fromMap(JsonSerializer.deserialize(it.message) as Map) } @@ -50,7 +50,7 @@ class RealtimeController(kuzzle: Kuzzle) : BaseController(kuzzle) { } } - kuzzle.protocol.addListener() { + kuzzle.protocol.addListener { if (it.state == ProtocolState.CLOSE) { currentSubscriptions.clear() } diff --git a/src/main/kotlin/io/kuzzle/sdk/coreClasses/http/Route.kt b/src/main/kotlin/io/kuzzle/sdk/coreClasses/http/Route.kt index 404f203d..11c82879 100644 --- a/src/main/kotlin/io/kuzzle/sdk/coreClasses/http/Route.kt +++ b/src/main/kotlin/io/kuzzle/sdk/coreClasses/http/Route.kt @@ -1,6 +1,7 @@ package io.kuzzle.sdk.coreClasses.http import io.kuzzle.sdk.coreClasses.exceptions.MissingURLParamException +import io.kuzzle.sdk.coreClasses.json.JsonSerializer import io.kuzzle.sdk.coreClasses.maps.KuzzleMap import io.kuzzle.sdk.coreClasses.serializer.StringSerializer import java.net.URLEncoder @@ -63,7 +64,6 @@ class Route { */ fun buildRequest(request: KuzzleMap): HttpRequest { val headers = request.optMap("headers", KuzzleMap()) - val queryArgs = KuzzleMap() for (key: String? in request.keys) { // Skip if a key or value is null @@ -78,7 +78,13 @@ class Route { "headers" -> headers.putAll(request.optMap("headers", KuzzleMap())) "body" -> { if (verb == "GET") { - queryArgs.putAll(request.optMap("body", KuzzleMap())) + var body = request["body"] + if (body != null) { + if (body !is Map<*, *>) { + body = JsonSerializer.deserialize(JsonSerializer.serialize(body)) + } + queryArgs.putAll(body as Map) + } } } else -> { @@ -88,7 +94,6 @@ class Route { } } } - /** * Build the query string */ @@ -113,7 +118,7 @@ class Route { return HttpRequest( verb, if (queryArgs.isEmpty()) staticURL else "$staticURL?$queryString", - if (verb != "GET") request.optMap("body", KuzzleMap()) else null, + if (verb != "GET") getBody(request) else null, headers ) } @@ -138,11 +143,26 @@ class Route { return HttpRequest( verb, if (queryArgs.isEmpty()) urlBuilder.toString() else "$urlBuilder?$queryString", - if (verb != "GET") request.optMap("body", KuzzleMap()) else null, + if (verb != "GET") getBody(request) else null, headers ) } + private fun getBody(request: KuzzleMap): KuzzleMap { + return when (val body = request["body"]) { + null -> { + KuzzleMap() + } + // If it's not a map, it can be a RawJson or Serializable object. + !is Map<*, *> -> { + KuzzleMap.from(JsonSerializer.deserialize(JsonSerializer.serialize(body)) as Map) + } + else -> { + KuzzleMap.from(body as Map) + } + } + } + companion object { /** * Parse Kuzzle url with the format /:index/:collection diff --git a/src/main/kotlin/io/kuzzle/sdk/coreClasses/json/JsonSerializer.kt b/src/main/kotlin/io/kuzzle/sdk/coreClasses/json/JsonSerializer.kt index 80f66889..a52c52c6 100644 --- a/src/main/kotlin/io/kuzzle/sdk/coreClasses/json/JsonSerializer.kt +++ b/src/main/kotlin/io/kuzzle/sdk/coreClasses/json/JsonSerializer.kt @@ -3,11 +3,16 @@ package io.kuzzle.sdk.coreClasses.json import com.google.gson.Gson import com.google.gson.GsonBuilder import io.kuzzle.sdk.coreClasses.RequestPayload +import io.kuzzle.sdk.coreClasses.maps.KuzzleMap object JsonSerializer { private var gson: Gson? = null fun deserialize(rawJson: String?): Map<*, *> { - return gson!!.fromJson(rawJson, Map::class.java) + return if (rawJson == null || rawJson.isBlank()) { + KuzzleMap() + } else { + gson!!.fromJson(rawJson, Map::class.java) + } } fun serialize(obj: Any): String { diff --git a/src/main/kotlin/io/kuzzle/sdk/coreClasses/maps/KuzzleMap.kt b/src/main/kotlin/io/kuzzle/sdk/coreClasses/maps/KuzzleMap.kt index 57a4f0b0..3bd7339d 100644 --- a/src/main/kotlin/io/kuzzle/sdk/coreClasses/maps/KuzzleMap.kt +++ b/src/main/kotlin/io/kuzzle/sdk/coreClasses/maps/KuzzleMap.kt @@ -9,7 +9,7 @@ class KuzzleMap : HashMap { /** * Create a new instance of CustomMap */ - constructor() : super() {} + constructor() : super() /** * Create a new instance of CustomMap from a Map,