Skip to content

Commit 10efc2f

Browse files
Refactor auth result adapter to take camel case into account (#200)
* refactor: Change how mapping is done to take camelCase usage into account * chore: update version * refactor: use hashmap isntead of conditionals to map to snake case property name
1 parent 57c4876 commit 10efc2f

3 files changed

Lines changed: 53 additions & 27 deletions

File tree

riskified-sdk/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<modelVersion>4.0.0</modelVersion>
55
<groupId>com.riskified</groupId>
66
<artifactId>riskified-sdk</artifactId>
7-
<version>5.0.1-rc.2</version>
7+
<version>5.0.1-rc.3</version>
88
<name>Riskified SDK</name>
99
<description>Riskified rest api SDK for java</description>
1010
<url>https://www.riskified.com</url>

riskified-sdk/src/main/java/com/riskified/adapters/AuthenticationResultAdapterFactory.java

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,8 @@
77
import com.riskified.models.AuthenticationResult;
88

99
import java.io.IOException;
10-
import java.util.Arrays;
11-
import java.util.HashSet;
10+
import java.util.HashMap;
1211
import java.util.Map;
13-
import java.util.Set;
1412

1513
/**
1614
* Gson TypeAdapterFactory for AuthenticationResult that handles backward
@@ -26,7 +24,7 @@
2624
* <p>
2725
* Handles field name changes between legacy and current formats:
2826
* <ul>
29-
* <li><b>Legacy:</b> tran_status, tran_status_reason, tra_exemption</li>
27+
* <li><b>Legacy:</b> tran_status, tran_status_reason, tra_exemption (or their camelCase equivalents)</li>
3028
* <li><b>Current:</b> trans_status, trans_status_reason, TRA_exemption</li>
3129
* </ul>
3230
*
@@ -54,13 +52,32 @@ public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
5452
*/
5553
private static class AuthenticationResultTypeAdapter extends TypeAdapter<AuthenticationResult> {
5654
private final TypeAdapter<AuthenticationResult> delegateAdapter;
57-
private final Set<String> manuallyHandledKeys = new HashSet<String>(Arrays.asList(new String[] { "tran_status",
58-
"trans_status", "tran_status_reason", "trans_status_reason", "tra_exemption", "TRA_exemption" }));
55+
private final Map<String, String> propertyMap = new HashMap<>();
5956

6057
AuthenticationResultTypeAdapter(Gson gson, TypeAdapterFactory skipPast) {
6158
// Get delegate adapter to avoid infinite recursion
6259
this.delegateAdapter = gson.getDelegateAdapter(skipPast,
6360
TypeToken.get(AuthenticationResult.class));
61+
62+
this.populateMap();
63+
64+
}
65+
66+
private void populateMap() {
67+
this.propertyMap.put("tranStatus", "trans_status");
68+
this.propertyMap.put("tran_status", "trans_status");
69+
70+
this.propertyMap.put("tranStatusReason", "trans_status_reason");
71+
this.propertyMap.put("tran_status_reason", "trans_status_reason");
72+
73+
this.propertyMap.put("threeDChallenge", "three_d_challenge");
74+
75+
this.propertyMap.put("tra_exemption", "TRA_exemption");
76+
this.propertyMap.put("traExemption", "TRA_exemption");
77+
78+
this.propertyMap.put("liabilityShift", "liability_shift");
79+
80+
this.propertyMap.put("createdAt", "created_at");
6481
}
6582

6683
@Override
@@ -84,28 +101,14 @@ public AuthenticationResult read(JsonReader in) throws IOException {
84101
JsonObject original = element.getAsJsonObject();
85102
JsonObject transformed = new JsonObject();
86103

87-
if (original.has("tran_status")) {
88-
transformed.add("trans_status", original.get("tran_status"));
89-
} else if (original.has("trans_status")) {
90-
transformed.add("trans_status", original.get("trans_status"));
91-
}
92-
93-
if (original.has("tran_status_reason")) {
94-
transformed.add("trans_status_reason", original.get("tran_status_reason"));
95-
} else if (original.has("trans_status_reason")) {
96-
transformed.add("trans_status_reason", original.get("trans_status_reason"));
97-
}
98-
99-
if (original.has("tra_exemption")) {
100-
transformed.add("TRA_exemption", original.get("tra_exemption"));
101-
} else if (original.has("TRA_exemption")) {
102-
transformed.add("TRA_exemption", original.get("TRA_exemption"));
103-
}
104-
105104
for (Map.Entry<String, JsonElement> entry : original.entrySet()) {
106105
String key = entry.getKey();
107-
if (!manuallyHandledKeys.contains(key)) {
108-
transformed.add(key, entry.getValue());
106+
JsonElement value = entry.getValue();
107+
108+
if (this.propertyMap.containsKey(key)) {
109+
transformed.add(this.propertyMap.get(key), value);
110+
} else {
111+
transformed.add(key, value);
109112
}
110113
}
111114

riskified-sdk/src/test/java/com/riskified/adapters/AuthenticationResultAdapterTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,4 +271,27 @@ public void testPaypalPaymentDetails() {
271271
assertFalse("3D challenge should be false", result.get3DChallenge());
272272
assertTrue("TRA exemption should be true", result.getTRAExemption());
273273
}
274+
275+
@Test
276+
public void testLegacyFormatCamelCase() {
277+
String json = "{\"credit_card_bin\":\"123456\",\"credit_card_number\":\"****1234\"," +
278+
"\"credit_card_company\":\"Visa\",\"authentication_result\":{\"eci\":\"05\"," +
279+
"\"cavv\":\"AAABCZIhcQAAAABZlyFxAAAAAAA=\",\"tranStatus\":\"Y\"," +
280+
"\"tranStatusReason\":\"01\",\"liability_shift\":true," +
281+
"\"threeDChallenge\":false,\"traExemption\":true}}";
282+
283+
CreditCardPaymentDetails details = gson.fromJson(json, CreditCardPaymentDetails.class);
284+
285+
assertNotNull("Payment details should not be null", details);
286+
assertNotNull("Authentication result should not be null", details.getAuthenticationResults());
287+
288+
AuthenticationResult result = details.getAuthenticationResults();
289+
assertEquals("ECI should match", "05", result.getEci());
290+
assertEquals("CAVV should match", "AAABCZIhcQAAAABZlyFxAAAAAAA=", result.getCavv());
291+
assertEquals("TransStatus should be Y", TransStatus.Y, result.getTransStatus());
292+
assertEquals("TransStatusReason should be Zero_One", TransStatusReason.Zero_One, result.getTransStatusReason());
293+
assertTrue("Liability shift should be true", result.getLiabilityShift());
294+
assertFalse("3D challenge should be false", result.get3DChallenge());
295+
assertTrue("TRA exemption should be true", result.getTRAExemption());
296+
}
274297
}

0 commit comments

Comments
 (0)