Skip to content

Commit 2f27cc7

Browse files
authored
Merge pull request #9849 from woocommerce/issue/9839-bool-decoding-fallbacks
[Reliability] Add failsafe decoding for Bool when string/data is received
2 parents a5285cc + c06d9fe commit 2f27cc7

File tree

7 files changed

+34
-8
lines changed

7 files changed

+34
-8
lines changed

Networking/Networking/Model/Product/Product.swift

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,11 @@ public struct Product: Codable, GeneratedCopiable, Equatable, GeneratedFakeable
375375
alternativeTypes: [.decimal(transform: { NSDecimalNumber(decimal: $0).stringValue })])
376376
?? ""
377377

378-
let onSale = try container.decode(Bool.self, forKey: .onSale)
378+
// Even though WooCommerce Core returns Bool values,
379+
// some plugins alter the field value from Bool to String.
380+
let onSale = container.failsafeDecodeIfPresent(targetType: Bool.self,
381+
forKey: .onSale,
382+
alternativeTypes: [ .string(transform: { NSString(string: $0).boolValue })]) ?? false
379383

380384
// Even though a plain install of WooCommerce Core provides string values,
381385
// some plugins alter the field value from String to Int or Decimal.
@@ -425,7 +429,13 @@ public struct Product: Codable, GeneratedCopiable, Equatable, GeneratedFakeable
425429
let stockStatusKey = try container.decode(String.self, forKey: .stockStatusKey)
426430

427431
let backordersKey = try container.decode(String.self, forKey: .backordersKey)
428-
let backordersAllowed = try container.decode(Bool.self, forKey: .backordersAllowed)
432+
433+
// Even though WooCommerce Core returns Bool values,
434+
// some plugins alter the field value from Bool to String.
435+
let backordersAllowed = container.failsafeDecodeIfPresent(targetType: Bool.self,
436+
forKey: .backordersAllowed,
437+
alternativeTypes: [ .string(transform: { NSString(string: $0).boolValue })]) ?? false
438+
429439
let backordered = try container.decode(Bool.self, forKey: .backordered)
430440

431441
let soldIndividually = try container.decodeIfPresent(Bool.self, forKey: .soldIndividually) ?? false

Networking/Networking/Model/Product/ProductVariation.swift

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,12 @@ public struct ProductVariation: Codable, GeneratedCopiable, Equatable, Generated
224224
forKey: .regularPrice,
225225
alternativeTypes: [.decimal(transform: { NSDecimalNumber(decimal: $0).stringValue })])
226226
?? ""
227-
let onSale = try container.decode(Bool.self, forKey: .onSale)
227+
228+
// Even though WooCommerce Core returns Bool values,
229+
// some plugins alter the field value from Bool to String.
230+
let onSale = container.failsafeDecodeIfPresent(targetType: Bool.self,
231+
forKey: .onSale,
232+
alternativeTypes: [ .string(transform: { NSString(string: $0).boolValue })]) ?? false
228233

229234
// Even though a plain install of WooCommerce Core provides string values,
230235
// some plugins alter the field value from String to Int or Decimal.
@@ -273,7 +278,13 @@ public struct ProductVariation: Codable, GeneratedCopiable, Equatable, Generated
273278
let stockStatusKey = try container.decode(String.self, forKey: .stockStatusKey)
274279
let stockStatus = ProductStockStatus(rawValue: stockStatusKey)
275280
let backordersKey = try container.decode(String.self, forKey: .backordersKey)
276-
let backordersAllowed = try container.decode(Bool.self, forKey: .backordersAllowed)
281+
282+
// Even though WooCommerce Core returns Bool values,
283+
// some plugins alter the field value from Bool to String.
284+
let backordersAllowed = container.failsafeDecodeIfPresent(targetType: Bool.self,
285+
forKey: .backordersAllowed,
286+
alternativeTypes: [ .string(transform: { NSString(string: $0).boolValue })]) ?? false
287+
277288
let backordered = try container.decode(Bool.self, forKey: .backordered)
278289

279290
// Even though a plain install of WooCommerce Core provides String values,

Networking/NetworkingTests/Mapper/ProductMapperTests.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ final class ProductMapperTests: XCTestCase {
123123
XCTAssertEqual(product.dimensions.width, "33")
124124
XCTAssertEqual(product.dimensions.height, "54")
125125
XCTAssertEqual(product.downloads.first?.downloadID, "12345")
126+
XCTAssertEqual(product.backordersAllowed, true)
127+
XCTAssertEqual(product.onSale, false)
126128
}
127129

128130
/// Verifies that the `salePrice` field of the Product are parsed correctly when the product is on sale, and the sale price is an empty string

Networking/NetworkingTests/Mapper/ProductVariationMapperTests.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ final class ProductVariationMapperTests: XCTestCase {
4242
XCTAssertEqual(productVariation.permalink, "")
4343
XCTAssertEqual(productVariation.sku, "12345")
4444
XCTAssertEqual(productVariation.weight, "2.5")
45+
XCTAssertEqual(productVariation.backordersAllowed, true)
46+
XCTAssertEqual(productVariation.onSale, false)
4547
}
4648

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

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"date_on_sale_to": null,
2424
"date_on_sale_to_gmt": "2019-10-27T21:29:59",
2525
"price_html": "Free",
26-
"on_sale": false,
26+
"on_sale": "",
2727
"purchasable": 1,
2828
"total_sales": 0,
2929
"virtual": true,
@@ -43,7 +43,7 @@
4343
"stock_quantity": null,
4444
"stock_status": "instock",
4545
"backorders": "no",
46-
"backorders_allowed": false,
46+
"backorders_allowed": "1",
4747
"backordered": false,
4848
"sold_individually": null,
4949
"weight": 213,

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"date_on_sale_from_gmt": "2019-10-15T21:30:00",
1717
"date_on_sale_to": "2019-10-27T23:59:59",
1818
"date_on_sale_to_gmt": "2019-10-27T21:29:59",
19-
"on_sale": false,
19+
"on_sale": "0",
2020
"status": "publish",
2121
"purchasable": 1,
2222
"virtual": false,
@@ -30,7 +30,7 @@
3030
"stock_quantity": 16,
3131
"stock_status": "instock",
3232
"backorders": "notify",
33-
"backorders_allowed": true,
33+
"backorders_allowed": "1",
3434
"backordered": false,
3535
"weight": 2.5,
3636
"dimensions": {

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 `backordersAllowed` and `onSale` in `Product` and `ProductVariation`, 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/9849]
78
- [*] 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]
89
- [*] 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]
910

0 commit comments

Comments
 (0)