-
Notifications
You must be signed in to change notification settings - Fork 406
Add discriminated union serialization tests #2260
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jar-stripe
wants to merge
6
commits into
master
Choose a base branch
from
jar/discriminated-unions
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+351
−0
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
fa54bc3
Add discriminated union serialization tests
jar-stripe 8835e0c
Merge branch 'master' into jar/discriminated-unions
jar-stripe fd02878
Add null assertions for non-selected DU variants
jar-stripe 7451fd3
Merge remote-tracking branch 'origin/jar/discriminated-unions' into j…
jar-stripe 1f60866
Fix response standalone DU test to use proper class hierarchy
jar-stripe 4d253ad
fixed formatting
jar-stripe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
351 changes: 351 additions & 0 deletions
351
src/test/java/com/stripe/net/DiscriminatedUnionSerializationTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,351 @@ | ||
| package com.stripe.net; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| import com.google.gson.FieldNamingPolicy; | ||
| import com.google.gson.Gson; | ||
| import com.google.gson.GsonBuilder; | ||
| import com.google.gson.JsonObject; | ||
| import com.google.gson.JsonParser; | ||
| import com.google.gson.TypeAdapter; | ||
| import com.google.gson.TypeAdapterFactory; | ||
| import com.google.gson.annotations.SerializedName; | ||
| import com.google.gson.reflect.TypeToken; | ||
| import com.google.gson.stream.JsonReader; | ||
| import com.google.gson.stream.JsonWriter; | ||
| import com.stripe.model.StripeObject; | ||
| import java.io.IOException; | ||
| import java.util.Map; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| public class DiscriminatedUnionSerializationTest { | ||
| private final ApiRequestParamsConverter converter = new ApiRequestParamsConverter(); | ||
|
|
||
| private final Gson testGson = | ||
| new GsonBuilder() | ||
| .registerTypeAdapterFactory(new TestColorTypeAdapterFactory()) | ||
| .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES) | ||
| .create(); | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
| // Request-side fixtures — standalone union | ||
| // The parent params hold an Object-typed field that can hold any variant. | ||
| // --------------------------------------------------------------------------- | ||
|
|
||
| @SuppressWarnings("UnusedVariable") | ||
| private static class TestCreateParams extends ApiRequestParams { | ||
| @SerializedName("color") | ||
| Object color; | ||
|
|
||
| @SerializedName("name") | ||
| String name; | ||
| } | ||
|
|
||
| @SuppressWarnings("UnusedVariable") | ||
| private static class TestRgbColorParams extends ApiRequestParams { | ||
| @SerializedName("model") | ||
| String model = "rgb"; | ||
|
|
||
| @SerializedName("r") | ||
| Long r; | ||
|
|
||
| @SerializedName("g") | ||
| Long g; | ||
|
|
||
| @SerializedName("b") | ||
| Long b; | ||
| } | ||
|
|
||
| @SuppressWarnings("UnusedVariable") | ||
| private static class TestHsvColorParams extends ApiRequestParams { | ||
| @SerializedName("model") | ||
| String model = "hsv"; | ||
|
|
||
| @SerializedName("h") | ||
| Long h; | ||
|
|
||
| @SerializedName("s") | ||
| Long s; | ||
|
|
||
| @SerializedName("v") | ||
| Long v; | ||
| } | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
| // Request-side fixtures — inline union | ||
| // The parent params hold the discriminator and each variant's fields directly. | ||
| // --------------------------------------------------------------------------- | ||
|
|
||
| @SuppressWarnings("UnusedVariable") | ||
| private static class TestInlineParams extends ApiRequestParams { | ||
| @SerializedName("type") | ||
| String type; | ||
|
|
||
| @SerializedName("card") | ||
| TestCardParams card; | ||
|
|
||
| @SerializedName("bank") | ||
| TestBankParams bank; | ||
| } | ||
|
|
||
| @SuppressWarnings("UnusedVariable") | ||
| private static class TestCardParams extends ApiRequestParams { | ||
| @SerializedName("number") | ||
| String number; | ||
|
|
||
| @SerializedName("exp_month") | ||
| Long expMonth; | ||
| } | ||
|
|
||
| @SuppressWarnings("UnusedVariable") | ||
| private static class TestBankParams extends ApiRequestParams { | ||
| @SerializedName("routing_number") | ||
| String routingNumber; | ||
|
|
||
| @SerializedName("account_number") | ||
| String accountNumber; | ||
| } | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
| // Response-side fixtures | ||
| // --------------------------------------------------------------------------- | ||
|
|
||
| private static class TestColorEntity extends StripeObject { | ||
| @SerializedName("model") | ||
| String model; | ||
| } | ||
|
|
||
| private static class TestRgbColorEntity extends TestColorEntity { | ||
| @SerializedName("r") | ||
| Long r; | ||
|
|
||
| @SerializedName("g") | ||
| Long g; | ||
|
|
||
| @SerializedName("b") | ||
| Long b; | ||
| } | ||
|
|
||
| private static class TestHsvColorEntity extends TestColorEntity { | ||
| @SerializedName("h") | ||
| Long h; | ||
|
|
||
| @SerializedName("s") | ||
| Long s; | ||
|
|
||
| @SerializedName("v") | ||
| Long v; | ||
| } | ||
|
|
||
| private static class TestColorTypeAdapterFactory implements TypeAdapterFactory { | ||
| @Override | ||
| @SuppressWarnings("unchecked") | ||
| public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) { | ||
| if (!TestColorEntity.class.isAssignableFrom(type.getRawType())) { | ||
| return null; | ||
| } | ||
| return (TypeAdapter<T>) | ||
| new TypeAdapter<TestColorEntity>() { | ||
| @Override | ||
| @SuppressWarnings("unchecked") | ||
| public void write(JsonWriter out, TestColorEntity value) throws IOException { | ||
| ((TypeAdapter<TestColorEntity>) gson.getAdapter(value.getClass())).write(out, value); | ||
| } | ||
|
|
||
| @Override | ||
| public TestColorEntity read(JsonReader in) throws IOException { | ||
| JsonObject obj = JsonParser.parseReader(in).getAsJsonObject(); | ||
| String model = obj.has("model") ? obj.get("model").getAsString() : null; | ||
| if ("rgb".equals(model)) { | ||
| return gson.getDelegateAdapter( | ||
| TestColorTypeAdapterFactory.this, TypeToken.get(TestRgbColorEntity.class)) | ||
| .fromJsonTree(obj); | ||
| } else if ("hsv".equals(model)) { | ||
| return gson.getDelegateAdapter( | ||
| TestColorTypeAdapterFactory.this, TypeToken.get(TestHsvColorEntity.class)) | ||
| .fromJsonTree(obj); | ||
| } | ||
| return gson.getDelegateAdapter( | ||
| TestColorTypeAdapterFactory.this, TypeToken.get(TestColorEntity.class)) | ||
| .fromJsonTree(obj); | ||
| } | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| private static class TestColorContainer extends StripeObject { | ||
| @SerializedName("color") | ||
| TestColorEntity color; | ||
|
|
||
| @SerializedName("name") | ||
| String name; | ||
| } | ||
|
|
||
| private static class TestPaymentEntity extends StripeObject { | ||
| @SerializedName("type") | ||
| String type; | ||
|
|
||
| @SerializedName("card") | ||
| TestCardEntity card; | ||
|
|
||
| @SerializedName("bank") | ||
| TestBankEntity bank; | ||
| } | ||
|
|
||
| private static class TestCardEntity extends StripeObject { | ||
| @SerializedName("number") | ||
| String number; | ||
|
|
||
| @SerializedName("exp_month") | ||
| Long expMonth; | ||
| } | ||
|
|
||
| private static class TestBankEntity extends StripeObject { | ||
| @SerializedName("routing_number") | ||
| String routingNumber; | ||
|
|
||
| @SerializedName("account_number") | ||
| String accountNumber; | ||
| } | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
| // Tests — request side (params → map) | ||
| // --------------------------------------------------------------------------- | ||
|
|
||
| @Test | ||
| @SuppressWarnings("unchecked") | ||
| public void testStandaloneUnion_RgbVariant_Serialization() { | ||
| TestRgbColorParams rgb = new TestRgbColorParams(); | ||
| rgb.r = 255L; | ||
| rgb.g = 128L; | ||
| rgb.b = 0L; | ||
|
|
||
| TestCreateParams params = new TestCreateParams(); | ||
| params.color = rgb; | ||
| params.name = "sunset"; | ||
|
|
||
| Map<String, Object> map = converter.convert(params); | ||
| assertEquals("sunset", map.get("name")); | ||
|
|
||
| Map<String, Object> colorMap = (Map<String, Object>) map.get("color"); | ||
| assertEquals("rgb", colorMap.get("model")); | ||
| assertEquals(255L, colorMap.get("r")); | ||
| assertEquals(128L, colorMap.get("g")); | ||
| assertEquals(0L, colorMap.get("b")); | ||
| } | ||
|
|
||
| @Test | ||
| @SuppressWarnings("unchecked") | ||
| public void testStandaloneUnion_HsvVariant_Serialization() { | ||
| TestHsvColorParams hsv = new TestHsvColorParams(); | ||
| hsv.h = 30L; | ||
| hsv.s = 100L; | ||
| hsv.v = 100L; | ||
|
|
||
| TestCreateParams params = new TestCreateParams(); | ||
| params.color = hsv; | ||
| params.name = "orange"; | ||
|
|
||
| Map<String, Object> map = converter.convert(params); | ||
| assertEquals("orange", map.get("name")); | ||
|
|
||
| Map<String, Object> colorMap = (Map<String, Object>) map.get("color"); | ||
| assertEquals("hsv", colorMap.get("model")); | ||
| assertEquals(30L, colorMap.get("h")); | ||
| assertEquals(100L, colorMap.get("s")); | ||
| assertEquals(100L, colorMap.get("v")); | ||
| } | ||
|
|
||
| @Test | ||
| @SuppressWarnings("unchecked") | ||
| public void testInlineUnion_CardVariant_Serialization() { | ||
| TestCardParams card = new TestCardParams(); | ||
| card.number = "4242424242424242"; | ||
| card.expMonth = 12L; | ||
|
|
||
| TestInlineParams params = new TestInlineParams(); | ||
| params.type = "card"; | ||
| params.card = card; | ||
|
|
||
| Map<String, Object> map = converter.convert(params); | ||
| assertEquals("card", map.get("type")); | ||
|
|
||
| Map<String, Object> cardMap = (Map<String, Object>) map.get("card"); | ||
| assertEquals("4242424242424242", cardMap.get("number")); | ||
| assertEquals(12L, cardMap.get("exp_month")); | ||
|
|
||
| // Non-selected variant is not present in serialized output. | ||
| assertEquals(null, map.get("bank")); | ||
| } | ||
|
|
||
| @Test | ||
| @SuppressWarnings("unchecked") | ||
| public void testInlineUnion_BankVariant_Serialization() { | ||
| TestBankParams bank = new TestBankParams(); | ||
| bank.routingNumber = "110000000"; | ||
| bank.accountNumber = "000123456789"; | ||
|
|
||
| TestInlineParams params = new TestInlineParams(); | ||
| params.type = "bank"; | ||
| params.bank = bank; | ||
|
|
||
| Map<String, Object> map = converter.convert(params); | ||
| assertEquals("bank", map.get("type")); | ||
|
|
||
| Map<String, Object> bankMap = (Map<String, Object>) map.get("bank"); | ||
| assertEquals("110000000", bankMap.get("routing_number")); | ||
| assertEquals("000123456789", bankMap.get("account_number")); | ||
| } | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
| // Tests — response side (JSON → object) | ||
| // --------------------------------------------------------------------------- | ||
|
|
||
| @Test | ||
| public void testStandaloneUnion_RgbVariant_Deserialization() { | ||
| String json = | ||
| "{\"color\": {\"model\": \"rgb\", \"r\": 255, \"g\": 128, \"b\": 0}, \"name\": \"sunset\"}"; | ||
|
|
||
| TestColorContainer container = testGson.fromJson(json, TestColorContainer.class); | ||
|
|
||
| assertEquals("sunset", container.name); | ||
| assertTrue(container.color instanceof TestRgbColorEntity); | ||
| TestRgbColorEntity rgb = (TestRgbColorEntity) container.color; | ||
| assertEquals("rgb", rgb.model); | ||
| assertEquals(Long.valueOf(255L), rgb.r); | ||
| assertEquals(Long.valueOf(128L), rgb.g); | ||
| assertEquals(Long.valueOf(0L), rgb.b); | ||
| } | ||
|
|
||
| @Test | ||
| public void testStandaloneUnion_HsvVariant_Deserialization() { | ||
| String json = | ||
| "{\"color\": {\"model\": \"hsv\", \"h\": 30, \"s\": 100, \"v\": 50}, \"name\": \"orange\"}"; | ||
|
|
||
| TestColorContainer container = testGson.fromJson(json, TestColorContainer.class); | ||
|
|
||
| assertEquals("orange", container.name); | ||
| assertTrue(container.color instanceof TestHsvColorEntity); | ||
| TestHsvColorEntity hsv = (TestHsvColorEntity) container.color; | ||
| assertEquals("hsv", hsv.model); | ||
| assertEquals(Long.valueOf(30L), hsv.h); | ||
| assertEquals(Long.valueOf(100L), hsv.s); | ||
| assertEquals(Long.valueOf(50L), hsv.v); | ||
| } | ||
|
|
||
| @Test | ||
| public void testInlineUnion_CardVariant_Deserialization() { | ||
| String json = | ||
| "{\"type\": \"card\", \"card\": {\"number\": \"4242424242424242\", \"exp_month\": 12}}"; | ||
|
|
||
| TestPaymentEntity entity = ApiResource.GSON.fromJson(json, TestPaymentEntity.class); | ||
|
|
||
| assertEquals("card", entity.type); | ||
| assertEquals("4242424242424242", entity.card.number); | ||
| assertEquals(Long.valueOf(12L), entity.card.expMonth); | ||
|
|
||
| // Non-selected variant remains null. | ||
| assertEquals(null, entity.bank); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added — the card serialization test now asserts that bank is absent from the output map.