Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resilient strip product variation #2758

Merged
merged 2 commits into from
Jun 21, 2023

Conversation

atorresveiga
Copy link
Contributor

Description

This PR prevents the StripProductVariationMetaData from crashing when it receives unexpected data.
The line gson.fromJson(metadata, JsonArray::class.java) will crash when the metadata value is empty ""
Calling jsonObject[WCMetaData.VALUE]?.asString will crash when the jsonObject value is an array with more than one element

Testing instructions

  1. Apply the patch and check that unit tests fail.
  2. Revert the changes, run unit tests again, and check that everything pass.
Patch with the old version
Index: plugins/woocommerce/src/main/kotlin/org/wordpress/android/fluxc/model/StripProductVariationMetaData.kt
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/plugins/woocommerce/src/main/kotlin/org/wordpress/android/fluxc/model/StripProductVariationMetaData.kt b/plugins/woocommerce/src/main/kotlin/org/wordpress/android/fluxc/model/StripProductVariationMetaData.kt
--- a/plugins/woocommerce/src/main/kotlin/org/wordpress/android/fluxc/model/StripProductVariationMetaData.kt	(revision fdec197d8f5ffe04d2141f32071a4d0e6045a754)
+++ b/plugins/woocommerce/src/main/kotlin/org/wordpress/android/fluxc/model/StripProductVariationMetaData.kt	(date 1687280251643)
@@ -3,19 +3,18 @@
 import com.google.gson.Gson
 import com.google.gson.JsonArray
 import com.google.gson.JsonObject
-import org.wordpress.android.fluxc.utils.isElementNullOrEmpty
 import javax.inject.Inject
 
 class StripProductVariationMetaData @Inject internal constructor(private val gson: Gson) {
     operator fun invoke(metadata: String?): String? {
-        if (metadata.isNullOrEmpty()) return null
+        if (metadata == null) return null
 
         return gson.fromJson(metadata, JsonArray::class.java)
             .mapNotNull { it as? JsonObject }
             .asSequence()
             .filter { jsonObject ->
-                val isNullOrEmpty = jsonObject[WCMetaData.VALUE].isElementNullOrEmpty()
-                jsonObject[WCMetaData.KEY]?.asString.orEmpty() in SUPPORTED_KEYS && isNullOrEmpty.not()
+                jsonObject[WCMetaData.KEY]?.asString.orEmpty() in SUPPORTED_KEYS &&
+                    jsonObject[WCMetaData.VALUE]?.asString.orEmpty().isNotBlank()
             }.toList()
             .takeIf { it.isNotEmpty() }
             ?.let { gson.toJson(it) }

Copy link
Contributor

@ThomazFB ThomazFB left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch and nice work adding test coverage to the crash scenario! 😄

Base automatically changed from issue/9218-strip-product-metadata to trunk June 21, 2023 03:47
@atorresveiga atorresveiga changed the title Issue/resilient strip product variation Resilient strip product variation Jun 21, 2023
@atorresveiga atorresveiga merged commit d0a1aa0 into trunk Jun 21, 2023
@atorresveiga atorresveiga deleted the issue/resilient-strip-product-variation branch June 21, 2023 03:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants