Skip to content
This repository was archived by the owner on Feb 4, 2025. It is now read-only.

Commit dbb16f6

Browse files
authored
Merge pull request #3077 from wordpress-mobile/fix/metadata-no-id-npe
FromJson returns null when json doesn't contain required fields
2 parents 06355b8 + c89a68d commit dbb16f6

File tree

2 files changed

+86
-9
lines changed

2 files changed

+86
-9
lines changed

plugins/woocommerce/src/main/kotlin/org/wordpress/android/fluxc/model/WCMetaData.kt

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ data class WCMetaData(
5252
}
5353

5454
companion object {
55-
private const val ID = "id"
55+
const val ID = "id"
5656
const val KEY = "key"
5757
const val VALUE = "value"
5858
private const val DISPLAY_KEY = "display_key"
@@ -68,14 +68,16 @@ data class WCMetaData(
6868
addAll(OrderAttributionInfoKeys.ALL_KEYS)
6969
}
7070

71-
internal fun fromJson(json: JsonObject): WCMetaData {
72-
return WCMetaData(
73-
id = json[ID].asLong,
74-
key = json[KEY].asString,
75-
value = WCMetaDataValue.fromJsonElement(json[VALUE]),
76-
displayKey = json[DISPLAY_KEY]?.asString,
77-
displayValue = json[DISPLAY_VALUE]?.let { WCMetaDataValue.fromJsonElement(it) }
78-
)
71+
internal fun fromJson(json: JsonObject): WCMetaData? {
72+
return if (json.has(ID) && json.has(KEY) && json.has(VALUE)) {
73+
WCMetaData(
74+
id = json[ID].asLong,
75+
key = json[KEY].asString,
76+
value = WCMetaDataValue.fromJsonElement(json[VALUE]),
77+
displayKey = json[DISPLAY_KEY]?.asString,
78+
displayValue = json[DISPLAY_VALUE]?.let { WCMetaDataValue.fromJsonElement(it) }
79+
)
80+
} else null
7981
}
8082

8183
fun addAsMetadata(metadata: JsonArray, key: String, value: String) {
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package org.wordpress.android.fluxc.model
2+
3+
import com.google.gson.JsonObject
4+
import org.assertj.core.api.Assertions.assertThat
5+
import org.junit.Test
6+
import org.wordpress.android.fluxc.model.WCMetaData.Companion.ID
7+
import org.wordpress.android.fluxc.model.WCMetaData.Companion.KEY
8+
import org.wordpress.android.fluxc.model.WCMetaData.Companion.VALUE
9+
10+
class WCMetaDataTest {
11+
@Test
12+
fun `when all required fields are present, should return a WCMetaData`() {
13+
val id = 1234L
14+
val key = "this_is_the_key"
15+
val value = "random value"
16+
17+
val metadataJson = JsonObject().apply {
18+
addProperty(ID, id)
19+
addProperty(KEY, key)
20+
addProperty(VALUE, value)
21+
}
22+
23+
val metadata = WCMetaData.fromJson(metadataJson)
24+
25+
assertThat(metadata).isNotNull
26+
assertThat(metadata!!.id).isEqualTo(id)
27+
assertThat(metadata.key).isEqualTo(key)
28+
assertThat(metadata.value.stringValue).isEqualTo(value)
29+
}
30+
31+
@Test
32+
fun `when ID is missing, should return null`() {
33+
val key = "this_is_the_key"
34+
val value = "random value"
35+
36+
val metadataJson = JsonObject().apply {
37+
addProperty(KEY, key)
38+
addProperty(VALUE, value)
39+
}
40+
41+
val metadata = WCMetaData.fromJson(metadataJson)
42+
43+
assertThat(metadata).isNull()
44+
}
45+
46+
@Test
47+
fun `when Key is missing, should return null`() {
48+
val id = 1234L
49+
val value = "random value"
50+
51+
val metadataJson = JsonObject().apply {
52+
addProperty(ID, id)
53+
addProperty(VALUE, value)
54+
}
55+
56+
val metadata = WCMetaData.fromJson(metadataJson)
57+
58+
assertThat(metadata).isNull()
59+
}
60+
61+
@Test
62+
fun `when Value is missing, should return null`() {
63+
val id = 1234L
64+
val key = "this_is_the_key"
65+
66+
val metadataJson = JsonObject().apply {
67+
addProperty(ID, id)
68+
addProperty(KEY, key)
69+
}
70+
71+
val metadata = WCMetaData.fromJson(metadataJson)
72+
73+
assertThat(metadata).isNull()
74+
}
75+
}

0 commit comments

Comments
 (0)