Skip to content

Commit a5285cc

Browse files
authored
Merge pull request #9847 from woocommerce/issue/9838-product-variation-decoding-errors
[Reliability] Add fallbacks for known product variation decoding errors
2 parents 892858e + e6fe097 commit a5285cc

File tree

4 files changed

+17
-4
lines changed

4 files changed

+17
-4
lines changed

Networking/Networking/Model/Product/ProductVariation.swift

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,12 @@ public struct ProductVariation: Codable, GeneratedCopiable, Equatable, Generated
206206
let statusKey = try container.decode(String.self, forKey: .statusKey)
207207
let status = ProductStatus(rawValue: statusKey)
208208
let description = try container.decodeIfPresent(String.self, forKey: .description)
209-
let sku = try container.decodeIfPresent(String.self, forKey: .sku)
209+
210+
// Even though a plain install of WooCommerce Core provides String values,
211+
// some plugins alter the field value from String to Int or Decimal.
212+
let sku = container.failsafeDecodeIfPresent(targetType: String.self,
213+
forKey: .sku,
214+
alternativeTypes: [.decimal(transform: { NSDecimalNumber(decimal: $0).stringValue })])
210215

211216
// Even though a plain install of WooCommerce Core provides string values,
212217
// some plugins alter the field value from String to Int or Decimal.
@@ -270,7 +275,12 @@ public struct ProductVariation: Codable, GeneratedCopiable, Equatable, Generated
270275
let backordersKey = try container.decode(String.self, forKey: .backordersKey)
271276
let backordersAllowed = try container.decode(Bool.self, forKey: .backordersAllowed)
272277
let backordered = try container.decode(Bool.self, forKey: .backordered)
273-
let weight = try container.decodeIfPresent(String.self, forKey: .weight)
278+
279+
// Even though a plain install of WooCommerce Core provides String values,
280+
// some plugins alter the field value from String to Int or Decimal.
281+
let weight = container.failsafeDecodeIfPresent(targetType: String.self,
282+
forKey: .weight,
283+
alternativeTypes: [.decimal(transform: { NSDecimalNumber(decimal: $0).stringValue })])
274284
let dimensions = try container.decode(ProductDimensions.self, forKey: .dimensions)
275285
let shippingClass = try container.decodeIfPresent(String.self, forKey: .shippingClass)
276286
let shippingClassID = try container.decode(Int64.self, forKey: .shippingClassID)

Networking/NetworkingTests/Mapper/ProductVariationMapperTests.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ final class ProductVariationMapperTests: XCTestCase {
4040
XCTAssertFalse(productVariation.manageStock)
4141
XCTAssertTrue(productVariation.purchasable)
4242
XCTAssertEqual(productVariation.permalink, "")
43+
XCTAssertEqual(productVariation.sku, "12345")
44+
XCTAssertEqual(productVariation.weight, "2.5")
4345
}
4446

4547
/// Test that the fields for variations of a subscription product are properly parsed.

Networking/NetworkingTests/Responses/product-variation-alternative-types.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"date_modified_gmt": "2019-11-14T13:06:42",
99
"description": "<p>Nutty chocolate marble, 99% and organic.</p>\n",
1010
"permalink": 0,
11-
"sku": "99%-nuts-marble",
11+
"sku": 12345,
1212
"price": 13.99,
1313
"regular_price": 16,
1414
"sale_price": 9.99,
@@ -32,7 +32,7 @@
3232
"backorders": "notify",
3333
"backorders_allowed": true,
3434
"backordered": false,
35-
"weight": "2.5",
35+
"weight": 2.5,
3636
"dimensions": {
3737
"length": "10",
3838
"width": "2.5",

RELEASE-NOTES.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
-----
55
- [*] Orders: Allow alternative types for the `taxID` in `ShippingLineTax` or `sku` in `OrderItem`, as some third-party plugins alter the type in the API. This helps with the order list not loading due to order decoding errors. [https://github.com/woocommerce/woocommerce-ios/pull/9844]
66
- [*] Payments: Location permissions request is not shown to TTP users who grant "Allow once" permission on first foregrounding the app any more [https://github.com/woocommerce/woocommerce-ios/pull/9821]
7+
- [*] Products: Allow alternative types for the `sku` and `weight` in `ProductVariation`, as some third-party plugins alter the types in the API. This helps with the product variation list not loading due to product variation decoding errors. [https://github.com/woocommerce/woocommerce-ios/pull/9847]
78
- [*] Products: Allow alternative types for the `sku` and `weight` in `Product`, the dimensions in `ProductDimensions`, and the `downloadID` in `ProductDownload`, as some third-party plugins alter the types in the API. This helps with the product list not loading due to product decoding errors. [https://github.com/woocommerce/woocommerce-ios/pull/9846]
89

910
13.8

0 commit comments

Comments
 (0)