Skip to content

Commit 719157c

Browse files
Serialize v2 int64's as String
1 parent eeef1e2 commit 719157c

2 files changed

Lines changed: 64 additions & 0 deletions

File tree

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.stripe.model;
2+
3+
import com.google.gson.TypeAdapter;
4+
import com.google.gson.stream.JsonReader;
5+
import com.google.gson.stream.JsonToken;
6+
import com.google.gson.stream.JsonWriter;
7+
import java.io.IOException;
8+
9+
public class StringInt64TypeAdapter extends TypeAdapter<Long> {
10+
/** Serializes Long values as JSON strings and deserializes string-encoded integers. */
11+
@Override
12+
public void write(JsonWriter out, Long value) throws IOException {
13+
if (value == null) {
14+
out.nullValue();
15+
return;
16+
}
17+
18+
out.value(value.toString());
19+
}
20+
21+
@Override
22+
public Long read(JsonReader in) throws IOException {
23+
JsonToken token = in.peek();
24+
if (token == JsonToken.NULL) {
25+
in.nextNull();
26+
return null;
27+
}
28+
29+
return Long.valueOf(in.nextString());
30+
}
31+
}

src/test/java/com/stripe/net/ApiRequestParamsConverterTest.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@
55
import static org.junit.jupiter.api.Assertions.assertThrows;
66
import static org.junit.jupiter.api.Assertions.assertTrue;
77

8+
import com.google.gson.Gson;
9+
import com.google.gson.GsonBuilder;
10+
import com.google.gson.annotations.JsonAdapter;
811
import com.google.gson.annotations.SerializedName;
12+
import com.stripe.model.StringInt64TypeAdapter;
913
import com.stripe.param.common.EmptyParam;
1014
import java.time.Instant;
1115
import java.util.Arrays;
@@ -122,6 +126,13 @@ private static class HasInstantParam extends ApiRequestParams {
122126
public Instant instantParam;
123127
}
124128

129+
private static class HasStringInt64Param extends ApiRequestParams {
130+
@SerializedName("divide_by")
131+
@JsonAdapter(StringInt64TypeAdapter.class)
132+
public Long divideBy;
133+
}
134+
135+
125136
@Test
126137
public void testHasExtraParams() {
127138
ModelHasExtraParams params = new ModelHasExtraParams(ParamCode.ENUM_FOO);
@@ -288,6 +299,28 @@ public void testObjectMaps() {
288299
assertEquals(objBar.get("hello"), "world");
289300
}
290301

302+
303+
@Test
304+
public void testToMapWithStringInt64Params() {
305+
HasStringInt64Param params = new HasStringInt64Param();
306+
params.divideBy = 123L;
307+
308+
Map<String, Object> paramMap = toMap(params);
309+
310+
TestCase.assertEquals(1, paramMap.size());
311+
TestCase.assertTrue(paramMap.containsKey("divide_by"));
312+
TestCase.assertEquals("123", paramMap.get("divide_by"));
313+
}
314+
315+
@Test
316+
public void testFromJsonWithStringInt64ResourceField() {
317+
Gson gson = new GsonBuilder().create();
318+
319+
HasStringInt64Param resource = gson.fromJson("{\"divide_by\":\"123\"}", HasStringInt64Param.class);
320+
321+
TestCase.assertEquals(Long.valueOf(123L), resource.divideBy);
322+
}
323+
291324
@Test
292325
public void testToMapWithInstantParams() {
293326
HasInstantParam params = new HasInstantParam();

0 commit comments

Comments
 (0)