Skip to content

Commit b4eaf19

Browse files
Serialize null values in ChangesetEntry (#135)
* cleanup * Serialize null values in ChangesetEntry
1 parent fb264e2 commit b4eaf19

File tree

7 files changed

+48
-8
lines changed

7 files changed

+48
-8
lines changed

src/main/java/io/castle/client/internal/backend/OkHttpFactory.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import okhttp3.logging.HttpLoggingInterceptor;
88

99
import java.io.IOException;
10-
import java.util.Collections;
1110
import java.util.concurrent.TimeUnit;
1211

1312
public class OkHttpFactory implements RestApiFactory {

src/main/java/io/castle/client/internal/backend/OkRestApiBackend.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package io.castle.client.internal.backend;
22

3-
import com.google.common.collect.ImmutableMap;
43
import com.google.gson.*;
54
import io.castle.client.Castle;
65
import io.castle.client.internal.config.CastleConfiguration;

src/main/java/io/castle/client/internal/backend/RestApi.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
package io.castle.client.internal.backend;
22

3-
import com.google.common.collect.ImmutableMap;
43
import com.google.gson.JsonElement;
54
import com.google.gson.JsonObject;
65
import io.castle.client.model.*;
7-
import okhttp3.Response;
86

97
public interface RestApi {
108

src/main/java/io/castle/client/internal/json/CastleGsonModel.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package io.castle.client.internal.json;
22

33
import com.google.gson.*;
4-
import com.google.gson.internal.bind.DateTypeAdapter;
54
import com.google.gson.internal.bind.util.ISO8601Utils;
65
import com.google.gson.stream.JsonReader;
76
import com.google.gson.stream.JsonWriter;
@@ -32,6 +31,7 @@ public CastleGsonModel() {
3231
builder.registerTypeAdapter(CastleHeaders.class, new CastleHeadersDeserializer());
3332
builder.registerTypeAdapter(AuthenticateAction.class, new AuthenticateActionDeserializer());
3433
builder.registerTypeAdapter(RiskPolicyType.class, new RiskPolicyTypeDeserializer());
34+
builder.registerTypeAdapterFactory(ChangesetEntryTypeAdapter.FACTORY);
3535

3636
builder.registerTypeAdapter(Date.class, new DateTypeAdapter());
3737
builder.registerTypeAdapter(java.sql.Date.class, new SqlDateTypeAdapter());
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package io.castle.client.internal.json;
2+
3+
import com.google.gson.reflect.TypeToken;
4+
import com.google.gson.*;
5+
import com.google.gson.stream.JsonReader;
6+
import com.google.gson.stream.JsonWriter;
7+
8+
import io.castle.client.model.generated.ChangesetEntry;
9+
10+
import java.io.IOException;
11+
12+
public class ChangesetEntryTypeAdapter extends TypeAdapter<ChangesetEntry> {
13+
static final TypeAdapterFactory FACTORY = new TypeAdapterFactory() {
14+
@SuppressWarnings("unchecked")
15+
@Override
16+
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
17+
if (type.getRawType() != ChangesetEntry.class) {
18+
return null;
19+
}
20+
TypeAdapter<ChangesetEntry> delegate = (TypeAdapter<ChangesetEntry>) gson.getDelegateAdapter(this, type);
21+
return (TypeAdapter<T>) new ChangesetEntryTypeAdapter(delegate);
22+
}
23+
};
24+
25+
private final TypeAdapter<ChangesetEntry> delegate;
26+
27+
ChangesetEntryTypeAdapter(TypeAdapter<ChangesetEntry> delegate) {
28+
this.delegate = delegate;
29+
}
30+
31+
@Override public void write(JsonWriter out, ChangesetEntry value) throws IOException {
32+
boolean serializeNulls = out.getSerializeNulls();
33+
out.setSerializeNulls(true);
34+
try {
35+
delegate.write(out, value);
36+
} finally {
37+
out.setSerializeNulls(serializeNulls);
38+
}
39+
}
40+
41+
@Override public ChangesetEntry read(JsonReader in) throws IOException {
42+
return delegate.read(in);
43+
}
44+
}

src/test/java/io/castle/client/CastleRiskHttpTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public CastleRiskHttpTest() {
7575

7676
Changeset changeSet = new Changeset()
7777
.email(new ChangesetEntry()
78-
78+
.from(null)
7979
8080
.password(new ChangedChangesetEntry())
8181
.authenticationMethodType(new ChangesetEntry()
@@ -114,7 +114,7 @@ public CastleRiskHttpTest() {
114114

115115
String body = recordedRequest.getBody().readUtf8();
116116

117-
String expected = "{\"context\":{\"headers\":[[\"User-Agent\",\"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1.1 Safari/605.1.15\"]],\"ip\":\"211.96.77.55\"},\"properties\":{\"property2\":{},\"property1\":{}},\"product\":{\"id\":\"1234\"},\"created_at\":\"2022-05-20T09:03:27.468+02:00\",\"request_token\":\"4-ugt5CFooaxt5KbpISi1Kurm5KTpqawlYmFs5PXsqKootPgRB3z12OpvPOWOQ9PkztagtqicAnk9Qowu7FlU9qabyi4k2QR6KUUL5p3gr-A2w8Ju8gWe0XyRi_OkmFj2oZiU9OTPAjijjIK4sA-a7f19GC_xzhYurdkWM-ZY1jR_l4R8JloVdGTfj7IhXY6_pd5SNGThjmM2DoSjWNup74xC3v-l3lI0ZMlDZPGJAyd3jsVnd5JXc6CZlmdxSQMk8UxHPyYbk7Sn24cjMQxHPqZZVvRkypP2Z1VW82eZVLYwD5jxc48Y4vCI4C1gDJWiIVMXssRDTmrPME9aeZPSc-ZelmSpX5T3p1iU9Gb1jnYmCdp7gnJ\",\"user\":{\"id\":\"12345\"},\"skip_request_token_validation\":false,\"skip_context_validation\":false,\"type\":\"$profile_update\",\"status\":\"$succeeded\",\"changeset\":{\"password\":{\"changed\":true},\"email\":{\"from\":\"[email protected]\",\"to\":\"[email protected]\"},\"authentication_method.type\":{\"from\":\"$authenticator\",\"to\":\"$email\"},\"name\":{\"from\":\"Jon Snow\",\"to\":\"King of the North\"}}}";
117+
String expected = "{\"context\":{\"headers\":[[\"User-Agent\",\"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1.1 Safari/605.1.15\"]],\"ip\":\"211.96.77.55\"},\"properties\":{\"property2\":{},\"property1\":{}},\"product\":{\"id\":\"1234\"},\"created_at\":\"2022-05-20T09:03:27.468+02:00\",\"request_token\":\"4-ugt5CFooaxt5KbpISi1Kurm5KTpqawlYmFs5PXsqKootPgRB3z12OpvPOWOQ9PkztagtqicAnk9Qowu7FlU9qabyi4k2QR6KUUL5p3gr-A2w8Ju8gWe0XyRi_OkmFj2oZiU9OTPAjijjIK4sA-a7f19GC_xzhYurdkWM-ZY1jR_l4R8JloVdGTfj7IhXY6_pd5SNGThjmM2DoSjWNup74xC3v-l3lI0ZMlDZPGJAyd3jsVnd5JXc6CZlmdxSQMk8UxHPyYbk7Sn24cjMQxHPqZZVvRkypP2Z1VW82eZVLYwD5jxc48Y4vCI4C1gDJWiIVMXssRDTmrPME9aeZPSc-ZelmSpX5T3p1iU9Gb1jnYmCdp7gnJ\",\"user\":{\"id\":\"12345\"},\"skip_request_token_validation\":false,\"skip_context_validation\":false,\"type\":\"$profile_update\",\"status\":\"$succeeded\",\"changeset\":{\"password\":{\"changed\":true},\"email\":{\"from\":null,\"to\":\"[email protected]\"},\"authentication_method.type\":{\"from\":\"$authenticator\",\"to\":\"$email\"},\"name\":{\"from\":\"Jon Snow\",\"to\":\"King of the North\"}}}";
118118
Assertions.assertThat(JsonParser.parseString(body)).isEqualTo(JsonParser.parseString(expected));
119119
}
120120

src/test/java/io/castle/client/model/CastleMessageTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public void otherProperties() {
101101

102102
Assertions.assertThat(payloadJson).isEqualTo("{\"event\":\"event\",\"key\":\"value\"}");
103103

104-
HashMap other = new HashMap();
104+
HashMap<String, String> other = new HashMap<String, String>();
105105
other.put("key", "value");
106106

107107
message = CastleMessage.builder("event")

0 commit comments

Comments
 (0)