Skip to content

Commit aa4c316

Browse files
authored
Merge pull request #4279 from woocommerce/issue/4205-addon-crash
Guard against invalid json objects when parsing product add-ons
2 parents 5c90b8c + 92a6260 commit aa4c316

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

Networking/Networking/Model/Product/ProductAddOnEnvelope.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,21 @@ internal struct ProductAddOnEnvelope: Decodable {
7171
/// Converts an addOnJsonObject(`Dictionary`) to a `ProductAddOn` entity.
7272
///
7373
private func decode(addOnJsonObject: AnyDictionary, using decoder: JSONDecoder) throws -> ProductAddOn {
74+
// It appears to be unexpected crashes when parsing some JSON objects that are not handled inside subsequent `try JSONSerialization.data` call.
75+
// https://github.com/woocommerce/woocommerce-ios/issues/4205
76+
guard JSONSerialization.isValidJSONObject(addOnJsonObject) else {
77+
throw ProductAddOnEnvelopeError.invalidJsonObject(addOnJsonObject)
78+
}
79+
7480
let jsonData = try JSONSerialization.data(withJSONObject: addOnJsonObject, options: .fragmentsAllowed)
7581
return try decoder.decode(ProductAddOn.self, from: jsonData)
7682
}
7783
}
84+
85+
/// Custom errors that can happen during the `ProductAddOnEnvelope` decoding.
86+
///
87+
public enum ProductAddOnEnvelopeError: Error {
88+
/// Represents an error when a `add-on JSON object` can't be converted to `JSON data`.
89+
///
90+
case invalidJsonObject([String: Any])
91+
}

0 commit comments

Comments
 (0)