Skip to content

Commit

Permalink
fix(json-utils) convert null string value to null
Browse files Browse the repository at this point in the history
SUITEDEV-21557

Co-authored-by: kovacszsoltizsolt <[email protected]>
Co-authored-by: davidSchuppa <[email protected]>
  • Loading branch information
3 people committed Mar 19, 2020
1 parent 936ad8a commit 83af5cf
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 13 deletions.
12 changes: 9 additions & 3 deletions core/src/main/java/com/emarsys/core/util/JsonUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
}
}
Expand All @@ -94,4 +96,8 @@ object JsonUtils {
}
require(nullCount != jsonObjects.size) { "Argument must contain at least one not null element!" }
}
}
}

private fun String.convertNullStringValueToNull(): String? {
return if (this == "null") null else this
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class PredictResponseMapperTest {
mockResponseModel = Mockito.mock(ResponseModel::class.java)
}

private fun getExpectedResult(feature: String, msrp: Float?, available: Boolean, price: Float?): List<Product> {
private fun getExpectedResult(feature: String, msrp: Float?, available: Boolean?, price: Float?): List<Product> {
val productBuilder = Product.Builder(
"2119",
"LSL Men Polo Shirt SE16",
Expand All @@ -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")
Expand Down Expand Up @@ -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]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,10 @@ private Product buildProductFromFields(String feature, String cohort, Map<String
productBuilder.available(Boolean.valueOf(available));
}
if (msrp != null) {
try {
productBuilder.msrp(Float.parseFloat(msrp));
} catch (Exception ignored) {
}
productBuilder.msrp(Float.parseFloat(msrp));
}
if (price != null) {
try {
productBuilder.price(Float.parseFloat(price));
} catch (Exception ignored) {
}
productBuilder.price(Float.parseFloat(price));
}
productBuilder.imageUrl(productFields.remove("image"));
productBuilder.zoomImageUrl(productFields.remove("zoom_image"));
Expand Down

0 comments on commit 83af5cf

Please sign in to comment.