Skip to content

Commit

Permalink
Update PropertiesModel's deserialization of tags to not use `Model.in…
Browse files Browse the repository at this point in the history
…itializeFromJson`.
  • Loading branch information
brismithers committed Oct 30, 2023
1 parent 43f36f4 commit 0ee4ef3
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ open class Model(
Float::class.java, java.lang.Float::class.java -> data[property] = jsonObject.getDouble(property).toFloat()
Int::class.java, java.lang.Integer::class.java -> data[property] = jsonObject.getInt(property)
Boolean::class.java, java.lang.Boolean::class.java -> data[property] = jsonObject.getBoolean(property)
String::class.java, java.lang.String::class.java -> data[property] = jsonObject.getString(property)
else -> data[property] = jsonObject.get(property)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,9 @@ class PropertiesModel : Model() {
override fun createModelForProperty(property: String, jsonObject: JSONObject): Model? {
if (property == ::tags.name) {
val model = MapModel<String>(this, ::tags.name)
model.initializeFromJson(jsonObject)
for (key in jsonObject.keys()) {
model.setStringProperty(key, jsonObject.getString(key))
}
return model
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.onesignal.user.internal.properties

import com.onesignal.common.putJSONObject
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.shouldBe
import io.kotest.runner.junit4.KotestTestRunner
import org.json.JSONObject
import org.junit.runner.RunWith

@RunWith(KotestTestRunner::class)
class PropertiesModelTests : FunSpec({

test("successfully initializes varying tag names") {
/* Given */
val varyingTags = JSONObject()
.putJSONObject(PropertiesModel::tags.name) {
it.put("value", "data1")
.put("isEmpty", "data2")
.put("object", "data3")
.put("1", "data4")
.put("false", "data5")
.put("15.7", "data6")
}
val propertiesModel = PropertiesModel()

/* When */
propertiesModel.initializeFromJson(varyingTags)
val tagsModel = propertiesModel.tags

/* Then */
tagsModel["value"] shouldBe "data1"
tagsModel["isEmpty"] shouldBe "data2"
tagsModel["object"] shouldBe "data3"
tagsModel["1"] shouldBe "data4"
tagsModel["false"] shouldBe "data5"
tagsModel["15.7"] shouldBe "data6"
}
})

0 comments on commit 0ee4ef3

Please sign in to comment.