From 83af5cf8fea476212e49496bf3f9a53b5ee15eb5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hunyady=20Mih=C3=A1ly?= Date: Thu, 19 Mar 2020 10:50:30 +0100 Subject: [PATCH] fix(json-utils) convert null string value to null SUITEDEV-21557 Co-authored-by: kovacszsoltizsolt <22084766+kovacszsoltizsolt@users.noreply.github.com> Co-authored-by: davidSchuppa <32750715+davidSchuppa@users.noreply.github.com> --- .../src/main/java/com/emarsys/core/util/JsonUtils.kt | 12 +++++++++--- .../com/emarsys/predict/PredictResponseMapperTest.kt | 7 +++++-- .../com/emarsys/predict/PredictResponseMapper.java | 10 ++-------- 3 files changed, 16 insertions(+), 13 deletions(-) diff --git a/core/src/main/java/com/emarsys/core/util/JsonUtils.kt b/core/src/main/java/com/emarsys/core/util/JsonUtils.kt index 1bb962f27..8e2658e10 100644 --- a/core/src/main/java/com/emarsys/core/util/JsonUtils.kt +++ b/core/src/main/java/com/emarsys/core/util/JsonUtils.kt @@ -76,8 +76,10 @@ object JsonUtils { while (iterator.hasNext()) { try { val key = iterator.next() - val value = jsonObject.getString(key) - result[key] = value + val value = jsonObject.getString(key).convertNullStringValueToNull() + if (value != null) { + result[key] = value + } } catch (ignore: JSONException) { } } @@ -94,4 +96,8 @@ object JsonUtils { } require(nullCount != jsonObjects.size) { "Argument must contain at least one not null element!" } } -} \ No newline at end of file +} + +private fun String.convertNullStringValueToNull(): String? { + return if (this == "null") null else this +} diff --git a/predict/src/androidTest/java/com/emarsys/predict/PredictResponseMapperTest.kt b/predict/src/androidTest/java/com/emarsys/predict/PredictResponseMapperTest.kt index 05c066f66..abb5a66b5 100644 --- a/predict/src/androidTest/java/com/emarsys/predict/PredictResponseMapperTest.kt +++ b/predict/src/androidTest/java/com/emarsys/predict/PredictResponseMapperTest.kt @@ -27,7 +27,7 @@ class PredictResponseMapperTest { mockResponseModel = Mockito.mock(ResponseModel::class.java) } - private fun getExpectedResult(feature: String, msrp: Float?, available: Boolean, price: Float?): List { + private fun getExpectedResult(feature: String, msrp: Float?, available: Boolean?, price: Float?): List { val productBuilder = Product.Builder( "2119", "LSL Men Polo Shirt SE16", @@ -42,6 +42,9 @@ class PredictResponseMapperTest { if (price != null) { productBuilder.price(price) } + if (available != null) { + productBuilder.available(available) + } productBuilder .imageUrl("http://lifestylelabels.com/pub/media/catalog/product/m/p/mp001.jpg") .zoomImageUrl("http://lifestylelabels.com/pub/media/catalog/product/m/p/mp001.jpg") @@ -100,7 +103,7 @@ class PredictResponseMapperTest { fun testMap_shouldNotCrash_whenParsedValuesAreNull() { whenever(mockResponseModel.body).thenReturn(getBodyFor("SEARCH", "null", "null", "null")) val predictResponseMapper = PredictResponseMapper() - val expectedResult = getExpectedResult("SEARCH", null, false, null)[0] + val expectedResult = getExpectedResult("SEARCH", null, null, null)[0] val result = predictResponseMapper.map(mockResponseModel)[0] diff --git a/predict/src/main/java/com/emarsys/predict/PredictResponseMapper.java b/predict/src/main/java/com/emarsys/predict/PredictResponseMapper.java index c0bd52d6d..e63663f05 100644 --- a/predict/src/main/java/com/emarsys/predict/PredictResponseMapper.java +++ b/predict/src/main/java/com/emarsys/predict/PredictResponseMapper.java @@ -65,16 +65,10 @@ private Product buildProductFromFields(String feature, String cohort, Map