woocommerce-ios icon indicating copy to clipboard operation
woocommerce-ios copied to clipboard

Swift.DecodingError on purchasable products. Expected to decode Bool but found a Number.

Open iamgabrielma opened this issue 3 years ago • 1 comments
trafficstars

Ref: p4a5px-2PC-p2

Some plugins seem to return 0 or 1 rather than the expected boolean for the purchasable API response. This leads to problems loading content within the app as data can't be decoded:

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

We can attempt and offer an alternative decoding if the expected one fails. For example, we offer alternative types for salePrice as is known that plugins alter this field:

        // Even though a plain install of WooCommerce Core provides string values,
        // some plugins alter the field value from String to Int or Decimal.
        let salePrice = container.failsafeDecodeIfPresent(targetType: String.self,
                                                          forKey: .salePrice,
                                                          shouldDecodeTargetTypeFirst: false,
                                                          alternativeTypes: [
                                                            .string(transform: { (onSale && $0.isEmpty) ? "0" : $0 }),
                                                            .decimal(transform: { NSDecimalNumber(decimal: $0).stringValue })])
            ?? ""

iamgabrielma avatar Aug 18 '22 06:08 iamgabrielma

Impact: medium – bug with workaround, in a low-priority area. Severity: medium – 2 user reports Priority: medium

joshheald avatar Aug 22 '22 11:08 joshheald

Another report in 5801963-zd-woothemes.

reginabally avatar Dec 22 '22 01:12 reginabally

Another case in 5815022-zd-woothemes. I requested the merchant to contact the plugin developer to change the data type of the purchasable field to boolean.

reginabally avatar Jan 18 '23 03:01 reginabally