diff --git a/src/main/java/com/stripe/model/StringInt64TypeAdapter.java b/src/main/java/com/stripe/model/StringInt64TypeAdapter.java new file mode 100644 index 00000000000..a742d945a68 --- /dev/null +++ b/src/main/java/com/stripe/model/StringInt64TypeAdapter.java @@ -0,0 +1,31 @@ +package com.stripe.model; + +import com.google.gson.TypeAdapter; +import com.google.gson.stream.JsonReader; +import com.google.gson.stream.JsonToken; +import com.google.gson.stream.JsonWriter; +import java.io.IOException; + +public class StringInt64TypeAdapter extends TypeAdapter { + /** Serializes Long values as JSON strings and deserializes string-encoded integers. */ + @Override + public void write(JsonWriter out, Long value) throws IOException { + if (value == null) { + out.nullValue(); + return; + } + + out.value(value.toString()); + } + + @Override + public Long read(JsonReader in) throws IOException { + JsonToken token = in.peek(); + if (token == JsonToken.NULL) { + in.nextNull(); + return null; + } + + return Long.valueOf(in.nextString()); + } +} diff --git a/src/test/java/com/stripe/net/ApiRequestParamsConverterTest.java b/src/test/java/com/stripe/net/ApiRequestParamsConverterTest.java index 88a9f532212..13686d759b8 100644 --- a/src/test/java/com/stripe/net/ApiRequestParamsConverterTest.java +++ b/src/test/java/com/stripe/net/ApiRequestParamsConverterTest.java @@ -5,7 +5,11 @@ import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.annotations.JsonAdapter; import com.google.gson.annotations.SerializedName; +import com.stripe.model.StringInt64TypeAdapter; import com.stripe.param.common.EmptyParam; import java.time.Instant; import java.util.Arrays; @@ -122,6 +126,12 @@ private static class HasInstantParam extends ApiRequestParams { public Instant instantParam; } + private static class HasStringInt64Param extends ApiRequestParams { + @SerializedName("divide_by") + @JsonAdapter(StringInt64TypeAdapter.class) + public Long divideBy; + } + @Test public void testHasExtraParams() { ModelHasExtraParams params = new ModelHasExtraParams(ParamCode.ENUM_FOO); @@ -288,6 +298,28 @@ public void testObjectMaps() { assertEquals(objBar.get("hello"), "world"); } + @Test + public void testToMapWithStringInt64Params() { + HasStringInt64Param params = new HasStringInt64Param(); + params.divideBy = 123L; + + Map paramMap = toMap(params); + + TestCase.assertEquals(1, paramMap.size()); + TestCase.assertTrue(paramMap.containsKey("divide_by")); + TestCase.assertEquals("123", paramMap.get("divide_by")); + } + + @Test + public void testFromJsonWithStringInt64ResourceField() { + Gson gson = new GsonBuilder().create(); + + HasStringInt64Param resource = + gson.fromJson("{\"divide_by\":\"123\"}", HasStringInt64Param.class); + + TestCase.assertEquals(Long.valueOf(123L), resource.divideBy); + } + @Test public void testToMapWithInstantParams() { HasInstantParam params = new HasInstantParam();