Skip to content

Commit 409eb1e

Browse files
Add runtime support for V2 int64 string-encoded fields (#2179)
* Serialize v2 int64's as String * Fix formatting
1 parent a9979f8 commit 409eb1e

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed
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: 32 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,12 @@ 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+
125135
@Test
126136
public void testHasExtraParams() {
127137
ModelHasExtraParams params = new ModelHasExtraParams(ParamCode.ENUM_FOO);
@@ -288,6 +298,28 @@ public void testObjectMaps() {
288298
assertEquals(objBar.get("hello"), "world");
289299
}
290300

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

0 commit comments

Comments
 (0)