Skip to content

Commit

Permalink
Merge pull request #1884 from /issues/1874-deserialize-tag-with-ident…
Browse files Browse the repository at this point in the history
…ical-method-name

Update PropertiesModel's deserialization of tags to not use `Model.initializeFromJson`
  • Loading branch information
brismithers authored and jinliu9508 committed Feb 6, 2024
2 parents 7e6fd64 + 4c94a01 commit f121014
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 f121014

Please sign in to comment.