Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions Networking/Networking/Model/Product/Product.swift
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,11 @@ public struct Product: Codable, GeneratedCopiable, Equatable, GeneratedFakeable
alternativeTypes: [.decimal(transform: { NSDecimalNumber(decimal: $0).stringValue })])
?? ""

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

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

let backordersKey = try container.decode(String.self, forKey: .backordersKey)
let backordersAllowed = try container.decode(Bool.self, forKey: .backordersAllowed)

// Even though WooCommerce Core returns Bool values,
// some plugins alter the field value from Bool to String.
let backordersAllowed = container.failsafeDecodeIfPresent(targetType: Bool.self,
forKey: .backordersAllowed,
alternativeTypes: [ .string(transform: { NSString(string: $0).boolValue })]) ?? false

let backordered = try container.decode(Bool.self, forKey: .backordered)

let soldIndividually = try container.decodeIfPresent(Bool.self, forKey: .soldIndividually) ?? false
Expand Down
15 changes: 13 additions & 2 deletions Networking/Networking/Model/Product/ProductVariation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,12 @@ public struct ProductVariation: Codable, GeneratedCopiable, Equatable, Generated
forKey: .regularPrice,
alternativeTypes: [.decimal(transform: { NSDecimalNumber(decimal: $0).stringValue })])
?? ""
let onSale = try container.decode(Bool.self, forKey: .onSale)

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

// Even though a plain install of WooCommerce Core provides string values,
// some plugins alter the field value from String to Int or Decimal.
Expand Down Expand Up @@ -273,7 +278,13 @@ public struct ProductVariation: Codable, GeneratedCopiable, Equatable, Generated
let stockStatusKey = try container.decode(String.self, forKey: .stockStatusKey)
let stockStatus = ProductStockStatus(rawValue: stockStatusKey)
let backordersKey = try container.decode(String.self, forKey: .backordersKey)
let backordersAllowed = try container.decode(Bool.self, forKey: .backordersAllowed)

// Even though WooCommerce Core returns Bool values,
// some plugins alter the field value from Bool to String.
let backordersAllowed = container.failsafeDecodeIfPresent(targetType: Bool.self,
forKey: .backordersAllowed,
alternativeTypes: [ .string(transform: { NSString(string: $0).boolValue })]) ?? false

let backordered = try container.decode(Bool.self, forKey: .backordered)

// Even though a plain install of WooCommerce Core provides String values,
Expand Down
2 changes: 2 additions & 0 deletions Networking/NetworkingTests/Mapper/ProductMapperTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ final class ProductMapperTests: XCTestCase {
XCTAssertEqual(product.dimensions.width, "33")
XCTAssertEqual(product.dimensions.height, "54")
XCTAssertEqual(product.downloads.first?.downloadID, "12345")
XCTAssertEqual(product.backordersAllowed, true)
XCTAssertEqual(product.onSale, false)
}

/// 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ final class ProductVariationMapperTests: XCTestCase {
XCTAssertEqual(productVariation.permalink, "")
XCTAssertEqual(productVariation.sku, "12345")
XCTAssertEqual(productVariation.weight, "2.5")
XCTAssertEqual(productVariation.backordersAllowed, true)
XCTAssertEqual(productVariation.onSale, false)
}

/// Test that the fields for variations of a subscription product are properly parsed.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"date_on_sale_to": null,
"date_on_sale_to_gmt": "2019-10-27T21:29:59",
"price_html": "Free",
"on_sale": false,
"on_sale": "",
"purchasable": 1,
"total_sales": 0,
"virtual": true,
Expand All @@ -43,7 +43,7 @@
"stock_quantity": null,
"stock_status": "instock",
"backorders": "no",
"backorders_allowed": false,
"backorders_allowed": "1",
"backordered": false,
"sold_individually": null,
"weight": 213,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"date_on_sale_from_gmt": "2019-10-15T21:30:00",
"date_on_sale_to": "2019-10-27T23:59:59",
"date_on_sale_to_gmt": "2019-10-27T21:29:59",
"on_sale": false,
"on_sale": "0",
"status": "publish",
"purchasable": 1,
"virtual": false,
Expand All @@ -30,7 +30,7 @@
"stock_quantity": 16,
"stock_status": "instock",
"backorders": "notify",
"backorders_allowed": true,
"backorders_allowed": "1",
"backordered": false,
"weight": 2.5,
"dimensions": {
Expand Down
1 change: 1 addition & 0 deletions RELEASE-NOTES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
-----
- [*] 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]
- [*] 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]
- [*] 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]
- [*] 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]
- [*] 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]

Expand Down