diff --git a/Networking/Networking/Model/Product/ProductVariation.swift b/Networking/Networking/Model/Product/ProductVariation.swift index d45c05265f4..826dedd3f36 100644 --- a/Networking/Networking/Model/Product/ProductVariation.swift +++ b/Networking/Networking/Model/Product/ProductVariation.swift @@ -206,7 +206,12 @@ public struct ProductVariation: Codable, GeneratedCopiable, Equatable, Generated let statusKey = try container.decode(String.self, forKey: .statusKey) let status = ProductStatus(rawValue: statusKey) let description = try container.decodeIfPresent(String.self, forKey: .description) - let sku = try container.decodeIfPresent(String.self, forKey: .sku) + + // Even though a plain install of WooCommerce Core provides String values, + // some plugins alter the field value from String to Int or Decimal. + let sku = container.failsafeDecodeIfPresent(targetType: String.self, + forKey: .sku, + alternativeTypes: [.decimal(transform: { NSDecimalNumber(decimal: $0).stringValue })]) // Even though a plain install of WooCommerce Core provides string values, // some plugins alter the field value from String to Int or Decimal. @@ -270,7 +275,12 @@ public struct ProductVariation: Codable, GeneratedCopiable, Equatable, Generated let backordersKey = try container.decode(String.self, forKey: .backordersKey) let backordersAllowed = try container.decode(Bool.self, forKey: .backordersAllowed) let backordered = try container.decode(Bool.self, forKey: .backordered) - let weight = try container.decodeIfPresent(String.self, forKey: .weight) + + // Even though a plain install of WooCommerce Core provides String values, + // some plugins alter the field value from String to Int or Decimal. + let weight = container.failsafeDecodeIfPresent(targetType: String.self, + forKey: .weight, + alternativeTypes: [.decimal(transform: { NSDecimalNumber(decimal: $0).stringValue })]) let dimensions = try container.decode(ProductDimensions.self, forKey: .dimensions) let shippingClass = try container.decodeIfPresent(String.self, forKey: .shippingClass) let shippingClassID = try container.decode(Int64.self, forKey: .shippingClassID) diff --git a/Networking/NetworkingTests/Mapper/ProductVariationMapperTests.swift b/Networking/NetworkingTests/Mapper/ProductVariationMapperTests.swift index 64640924e1a..cc08e8648bc 100644 --- a/Networking/NetworkingTests/Mapper/ProductVariationMapperTests.swift +++ b/Networking/NetworkingTests/Mapper/ProductVariationMapperTests.swift @@ -40,6 +40,8 @@ final class ProductVariationMapperTests: XCTestCase { XCTAssertFalse(productVariation.manageStock) XCTAssertTrue(productVariation.purchasable) XCTAssertEqual(productVariation.permalink, "") + XCTAssertEqual(productVariation.sku, "12345") + XCTAssertEqual(productVariation.weight, "2.5") } /// Test that the fields for variations of a subscription product are properly parsed. diff --git a/Networking/NetworkingTests/Responses/product-variation-alternative-types.json b/Networking/NetworkingTests/Responses/product-variation-alternative-types.json index 638b186d876..66ac40b194d 100644 --- a/Networking/NetworkingTests/Responses/product-variation-alternative-types.json +++ b/Networking/NetworkingTests/Responses/product-variation-alternative-types.json @@ -8,7 +8,7 @@ "date_modified_gmt": "2019-11-14T13:06:42", "description": "
Nutty chocolate marble, 99% and organic.
\n", "permalink": 0, - "sku": "99%-nuts-marble", + "sku": 12345, "price": 13.99, "regular_price": 16, "sale_price": 9.99, @@ -32,7 +32,7 @@ "backorders": "notify", "backorders_allowed": true, "backordered": false, - "weight": "2.5", + "weight": 2.5, "dimensions": { "length": "10", "width": "2.5", diff --git a/RELEASE-NOTES.txt b/RELEASE-NOTES.txt index 4ba5f89980d..b3766e0a7f4 100644 --- a/RELEASE-NOTES.txt +++ b/RELEASE-NOTES.txt @@ -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 `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] 13.8