Skip to content

Commit ae63bca

Browse files
committed
Add fallback when onSale field value is a string
1 parent cc75fb5 commit ae63bca

File tree

6 files changed

+15
-4
lines changed

6 files changed

+15
-4
lines changed

Networking/Networking/Model/Product/Product.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,11 @@ public struct Product: Codable, GeneratedCopiable, Equatable, GeneratedFakeable
370370
alternativeTypes: [.decimal(transform: { NSDecimalNumber(decimal: $0).stringValue })])
371371
?? ""
372372

373-
let onSale = try container.decode(Bool.self, forKey: .onSale)
373+
// Even though WooCommerce Core returns Bool values,
374+
// some plugins alter the field value from Bool to String.
375+
let onSale = container.failsafeDecodeIfPresent(targetType: Bool.self,
376+
forKey: .onSale,
377+
alternativeTypes: [ .string(transform: { NSString(string: $0).boolValue })]) ?? false
374378

375379
// Even though a plain install of WooCommerce Core provides string values,
376380
// some plugins alter the field value from String to Int or Decimal.

Networking/Networking/Model/Product/ProductVariation.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,12 @@ public struct ProductVariation: Codable, GeneratedCopiable, Equatable, Generated
219219
forKey: .regularPrice,
220220
alternativeTypes: [.decimal(transform: { NSDecimalNumber(decimal: $0).stringValue })])
221221
?? ""
222-
let onSale = try container.decode(Bool.self, forKey: .onSale)
222+
223+
// Even though WooCommerce Core returns Bool values,
224+
// some plugins alter the field value from Bool to String.
225+
let onSale = container.failsafeDecodeIfPresent(targetType: Bool.self,
226+
forKey: .onSale,
227+
alternativeTypes: [ .string(transform: { NSString(string: $0).boolValue })]) ?? false
223228

224229
// Even though a plain install of WooCommerce Core provides string values,
225230
// some plugins alter the field value from String to Int or Decimal.

Networking/NetworkingTests/Mapper/ProductMapperTests.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ final class ProductMapperTests: XCTestCase {
118118
XCTAssertTrue(product.purchasable)
119119
XCTAssertEqual(product.permalink, "")
120120
XCTAssertEqual(product.backordersAllowed, true)
121+
XCTAssertEqual(product.onSale, false)
121122
}
122123

123124
/// 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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ final class ProductVariationMapperTests: XCTestCase {
4141
XCTAssertTrue(productVariation.purchasable)
4242
XCTAssertEqual(productVariation.permalink, "")
4343
XCTAssertEqual(productVariation.backordersAllowed, true)
44+
XCTAssertEqual(productVariation.onSale, false)
4445
}
4546

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

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

Lines changed: 1 addition & 1 deletion
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,

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

Lines changed: 1 addition & 1 deletion
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,

0 commit comments

Comments
 (0)