Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed attribute deserialization #141

Merged
merged 5 commits into from
Jan 16, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,24 @@ public class WMTOperationFormData: Codable {
let c = try decoder.container(keyedBy: Keys.self)
title = try c.decode(String.self, forKey: .title)
message = try c.decode(String.self, forKey: .message)
// attributes are optional
attributes = (try? c.decode([WMTOperationAttributeDecodable].self, forKey: .attributes).map { $0.attrObject }) ?? []

var operationAttributes: [WMTOperationAttribute] = []
do {
var container = try c.nestedUnkeyedContainer(forKey: .attributes)
// If decoding fails log it and continue decoding until the end of container
while container.isAtEnd == false {
do {
let attribute = try WMTOperationAttributeDecodable(from: container.superDecoder())
operationAttributes.append(attribute.attrObject)
} catch {
D.error("Error decoding WMTOperationFormData attribute: \(error)")
}
}
} catch {
D.print("No attributes in WMTOperationFormData: \(error)")
}

attributes = operationAttributes
}

public init(title: String, message: String, attributes: [WMTOperationAttribute]) {
Expand Down
20 changes: 20 additions & 0 deletions WultraMobileTokenSDKTests/NetworkingObjectsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,26 @@ class NetworkingObjectsTests: XCTestCase {
XCTAssert(obj.filter({ $0.status == val }).count == 1)
}
}

func testAttributesDeserializationNotInterupted() {
// Non-optional amount removed from AMOUNT Attribute
let response = """
{"status":"OK", "currentTimestamp":"2023-02-10T12:30:42+0000", "responseObject":[{"id":"930febe7-f350-419a-8bc0-c8883e7f71e3", "name":"authorize_payment", "data":"A1*A100CZK*Q238400856/0300**D20170629*NUtility Bill Payment - 05/2017", "operationCreated":"2018-08-08T12:30:42+0000", "operationExpires":"2018-08-08T12:35:43+0000", "allowedSignatureType": {"type":"2FA", "variants": ["possession_knowledge", "possession_biometry"]}, "formData": {"title":"Potvrzení platby", "message":"Dobrý den,prosíme o potvrzení následující platby:", "attributes": [{"type":"AMOUNT", "id":"operation.amount", "label":"Částka", "currency":"CZK"}, { "type": "AMOUNT_CONVERSION", "id": "operation.conversion", "label": "Conversion", "dynamic": true, "sourceAmount": 1.26, "sourceCurrency": "ETC", "targetAmount": 1710.98, "targetCurrency": "USD"}]}}]}
"""

guard let result = try? jsonDecoder.decode(WPNResponseArray<WMTUserOperation>.self, from: response.data(using: .utf8)!) else {
XCTFail("Failed to parse JSON data")
return
}

guard let attributes = result.responseObject?[0].formData.attributes else {
XCTFail("Failed to get attributes")
return
}

XCTAssertTrue(attributes.count == 1, "There should be one Conversion Attribute but the count is: \(attributes.count)")
XCTAssert(attributes.first is WMTOperationAttributeAmountConversion, "The First attribute should be a Conversion Attribute.")
}
}

extension WPNRequestBase {
Expand Down
Loading