77import com .riskified .models .AuthenticationResult ;
88
99import java .io .IOException ;
10- import java .util .Arrays ;
11- import java .util .HashSet ;
10+ import java .util .HashMap ;
1211import java .util .Map ;
13- import java .util .Set ;
1412
1513/**
1614 * Gson TypeAdapterFactory for AuthenticationResult that handles backward
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
0 commit comments