From 7c0bb4928a56ea03a2f73c39d3473ec477b463fc Mon Sep 17 00:00:00 2001 From: Olivier Cavadenti Date: Thu, 12 May 2022 19:45:10 +0200 Subject: [PATCH 1/5] Support RawJson in Body and fixes - Support RawJson body type in requests. - Avoid NPE when empty response. - Avoid requestId missing error when empty response by taking the requestId of the event. --- src/main/kotlin/io/kuzzle/sdk/Kuzzle.kt | 9 ++++++- .../io/kuzzle/sdk/coreClasses/http/Route.kt | 27 +++++++++++++++---- .../sdk/coreClasses/json/JsonSerializer.kt | 7 ++++- 3 files changed, 36 insertions(+), 7 deletions(-) diff --git a/src/main/kotlin/io/kuzzle/sdk/Kuzzle.kt b/src/main/kotlin/io/kuzzle/sdk/Kuzzle.kt index 6b0524d0..38d7566f 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/coreClasses/http/Route.kt b/src/main/kotlin/io/kuzzle/sdk/coreClasses/http/Route.kt index 404f203d..efd663cd 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,8 @@ 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.json.RawJson import io.kuzzle.sdk.coreClasses.maps.KuzzleMap import io.kuzzle.sdk.coreClasses.serializer.StringSerializer import java.net.URLEncoder @@ -63,7 +65,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 +79,15 @@ class Route { "headers" -> headers.putAll(request.optMap("headers", KuzzleMap())) "body" -> { if (verb == "GET") { - queryArgs.putAll(request.optMap("body", KuzzleMap())) + var bodyMap = KuzzleMap() + var body = request["body"] + if(body != null) { + if (body is RawJson) { + body = JsonSerializer.deserialize(body.rawJson) + } + bodyMap = KuzzleMap.from(body as Map) + } + queryArgs.putAll(bodyMap) } } else -> { @@ -88,7 +97,6 @@ class Route { } } } - /** * Build the query string */ @@ -113,7 +121,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 +146,20 @@ 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 { + val body = request["body"] + return if (body == null) { + KuzzleMap() + } 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..a51bbd0d 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 { From 50ee46f6e41085bc6feee9ef64da4c6167ce922d Mon Sep 17 00:00:00 2001 From: Olivier Cavadenti Date: Thu, 12 May 2022 19:57:04 +0200 Subject: [PATCH 2/5] lint --- src/main/kotlin/io/kuzzle/sdk/Kuzzle.kt | 2 +- src/main/kotlin/io/kuzzle/sdk/coreClasses/http/Route.kt | 4 ++-- .../kotlin/io/kuzzle/sdk/coreClasses/json/JsonSerializer.kt | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/kotlin/io/kuzzle/sdk/Kuzzle.kt b/src/main/kotlin/io/kuzzle/sdk/Kuzzle.kt index 38d7566f..66279c50 100644 --- a/src/main/kotlin/io/kuzzle/sdk/Kuzzle.kt +++ b/src/main/kotlin/io/kuzzle/sdk/Kuzzle.kt @@ -80,7 +80,7 @@ open class Kuzzle { // 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) { + if (! jsonObject.containsKey("requestId") && event.requestId != null) { jsonObject = jsonObject.plus("requestId" to event.requestId) } 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 efd663cd..48f066da 100644 --- a/src/main/kotlin/io/kuzzle/sdk/coreClasses/http/Route.kt +++ b/src/main/kotlin/io/kuzzle/sdk/coreClasses/http/Route.kt @@ -81,11 +81,11 @@ class Route { if (verb == "GET") { var bodyMap = KuzzleMap() var body = request["body"] - if(body != null) { + if (body != null) { if (body is RawJson) { body = JsonSerializer.deserialize(body.rawJson) } - bodyMap = KuzzleMap.from(body as Map) + bodyMap = KuzzleMap.from(body as Map) } queryArgs.putAll(bodyMap) } 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 a51bbd0d..a52c52c6 100644 --- a/src/main/kotlin/io/kuzzle/sdk/coreClasses/json/JsonSerializer.kt +++ b/src/main/kotlin/io/kuzzle/sdk/coreClasses/json/JsonSerializer.kt @@ -8,7 +8,7 @@ import io.kuzzle.sdk.coreClasses.maps.KuzzleMap object JsonSerializer { private var gson: Gson? = null fun deserialize(rawJson: String?): Map<*, *> { - return if(rawJson == null || rawJson.isBlank()) { + return if (rawJson == null || rawJson.isBlank()) { KuzzleMap() } else { gson!!.fromJson(rawJson, Map::class.java) From a0d258b9072bb3fbac53f1692f821edc9dc78662 Mon Sep 17 00:00:00 2001 From: Olivier Cavadenti Date: Fri, 13 May 2022 10:32:42 +0200 Subject: [PATCH 3/5] pr returns --- .../io/kuzzle/sdk/coreClasses/http/Route.kt | 24 +++++++++++-------- 1 file changed, 14 insertions(+), 10 deletions(-) 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 48f066da..cc7205d3 100644 --- a/src/main/kotlin/io/kuzzle/sdk/coreClasses/http/Route.kt +++ b/src/main/kotlin/io/kuzzle/sdk/coreClasses/http/Route.kt @@ -79,15 +79,13 @@ class Route { "headers" -> headers.putAll(request.optMap("headers", KuzzleMap())) "body" -> { if (verb == "GET") { - var bodyMap = KuzzleMap() var body = request["body"] if (body != null) { - if (body is RawJson) { - body = JsonSerializer.deserialize(body.rawJson) + if (body !is Map<*, *>) { + body = JsonSerializer.deserialize(JsonSerializer.serialize(body)) } - bodyMap = KuzzleMap.from(body as Map) + queryArgs.putAll(body as Map) } - queryArgs.putAll(bodyMap) } } else -> { @@ -152,11 +150,17 @@ class Route { } private fun getBody(request: KuzzleMap): KuzzleMap { - val body = request["body"] - return if (body == null) { - KuzzleMap() - } else { - KuzzleMap.from(body as Map) + 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) + } } } From 72adda495560fd7ee5a3be2cea6b0d4da38c541a Mon Sep 17 00:00:00 2001 From: Olivier Cavadenti Date: Fri, 13 May 2022 10:57:57 +0200 Subject: [PATCH 4/5] unused import --- src/main/kotlin/io/kuzzle/sdk/coreClasses/http/Route.kt | 1 - 1 file changed, 1 deletion(-) 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 cc7205d3..11c82879 100644 --- a/src/main/kotlin/io/kuzzle/sdk/coreClasses/http/Route.kt +++ b/src/main/kotlin/io/kuzzle/sdk/coreClasses/http/Route.kt @@ -2,7 +2,6 @@ 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.json.RawJson import io.kuzzle.sdk.coreClasses.maps.KuzzleMap import io.kuzzle.sdk.coreClasses.serializer.StringSerializer import java.net.URLEncoder From c402bf84f7ac1044e354a1307b354d6795dccf9d Mon Sep 17 00:00:00 2001 From: Olivier Cavadenti Date: Fri, 13 May 2022 11:01:39 +0200 Subject: [PATCH 5/5] remove some useless things --- .../kotlin/io/kuzzle/sdk/controllers/RealtimeController.kt | 4 ++-- src/main/kotlin/io/kuzzle/sdk/coreClasses/maps/KuzzleMap.kt | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) 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/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,