Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CODEGEN_VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
49b1e23eef1a5004ed00987c41d5ec8447a19e27
c2fdb8b70a4520e54d461041362bece6a6df54e9
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
}
final String discriminator = "object";
final TypeAdapter<JsonElement> jsonElementAdapter = gson.getAdapter(JsonElement.class);
final TypeAdapter<com.stripe.model.BalanceTransactionSource> balanceTransactionSourceAdapter =
gson.getDelegateAdapter(
this, TypeToken.get(com.stripe.model.BalanceTransactionSource.class));
final TypeAdapter<com.stripe.model.ApplicationFee> applicationFeeAdapter =
gson.getDelegateAdapter(this, TypeToken.get(com.stripe.model.ApplicationFee.class));
final TypeAdapter<com.stripe.model.Charge> chargeAdapter =
Expand Down Expand Up @@ -68,7 +65,13 @@ public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
new TypeAdapter<BalanceTransactionSource>() {
@Override
public void write(JsonWriter out, BalanceTransactionSource value) throws IOException {
balanceTransactionSourceAdapter.write(out, value);
@SuppressWarnings("unchecked")
TypeAdapter<BalanceTransactionSource> adapter =
(TypeAdapter<BalanceTransactionSource>)
gson.getDelegateAdapter(
BalanceTransactionSourceTypeAdapterFactory.this,
TypeToken.get(value.getClass()));
adapter.write(out, value);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
}
final String discriminator = "object";
final TypeAdapter<JsonElement> jsonElementAdapter = gson.getAdapter(JsonElement.class);
final TypeAdapter<com.stripe.model.ExternalAccount> externalAccountAdapter =
gson.getDelegateAdapter(this, TypeToken.get(com.stripe.model.ExternalAccount.class));
final TypeAdapter<com.stripe.model.BankAccount> bankAccountAdapter =
gson.getDelegateAdapter(this, TypeToken.get(com.stripe.model.BankAccount.class));
final TypeAdapter<com.stripe.model.Card> cardAdapter =
Expand All @@ -39,7 +37,12 @@ public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
new TypeAdapter<ExternalAccount>() {
@Override
public void write(JsonWriter out, ExternalAccount value) throws IOException {
externalAccountAdapter.write(out, value);
@SuppressWarnings("unchecked")
TypeAdapter<ExternalAccount> adapter =
(TypeAdapter<ExternalAccount>)
gson.getDelegateAdapter(
ExternalAccountTypeAdapterFactory.this, TypeToken.get(value.getClass()));
adapter.write(out, value);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
}
final String discriminator = "object";
final TypeAdapter<JsonElement> jsonElementAdapter = gson.getAdapter(JsonElement.class);
final TypeAdapter<com.stripe.model.PaymentSource> paymentSourceAdapter =
gson.getDelegateAdapter(this, TypeToken.get(com.stripe.model.PaymentSource.class));
final TypeAdapter<com.stripe.model.Account> accountAdapter =
gson.getDelegateAdapter(this, TypeToken.get(com.stripe.model.Account.class));
final TypeAdapter<com.stripe.model.BankAccount> bankAccountAdapter =
Expand All @@ -40,7 +38,12 @@ public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
new TypeAdapter<PaymentSource>() {
@Override
public void write(JsonWriter out, PaymentSource value) throws IOException {
paymentSourceAdapter.write(out, value);
@SuppressWarnings("unchecked")
TypeAdapter<PaymentSource> adapter =
(TypeAdapter<PaymentSource>)
gson.getDelegateAdapter(
PaymentSourceTypeAdapterFactory.this, TypeToken.get(value.getClass()));
adapter.write(out, value);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.stripe.model;

import com.google.gson.JsonElement;
import com.google.gson.JsonNull;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import java.lang.reflect.Type;

public class StripeRawJsonObjectSerializer implements JsonSerializer<StripeRawJsonObject> {
@Override
public JsonElement serialize(
StripeRawJsonObject src, Type typeOfSrc, JsonSerializationContext context) {
if (src.json != null) {
return src.json;
}
return JsonNull.INSTANCE;
}
}
7 changes: 5 additions & 2 deletions src/main/java/com/stripe/model/v2/core/Event.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,15 @@ protected StripeObject fetchRelatedObject(RelatedObject relatedObject) throws St
objectClass = StripeRawJsonObject.class;
}

RequestOptions opts = null;
RequestOptions.RequestOptionsBuilder optsBuilder = new RequestOptions.RequestOptionsBuilder();
// optsBuilder.setStripeRequestTrigger("event=" + id); // TODO https://go/j/DEVSDK-3018

if (context != null) {
opts = new RequestOptions.RequestOptionsBuilder().setStripeAccount(context).build();
optsBuilder.setStripeAccount(context);
}

RequestOptions opts = optsBuilder.build();

return this.responseGetter.request(
new ApiRequest(
BaseAddress.API, ApiResource.RequestMethod.GET, relatedObject.getUrl(), null, opts),
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/stripe/net/ApiResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,11 @@ private static Gson createGson(boolean shouldSetResponseGetter) {
.registerTypeAdapter(Event.Request.class, new EventRequestDeserializer())
.registerTypeAdapter(StripeContext.class, new StripeContextDeserializer())
.registerTypeAdapter(ExpandableField.class, new ExpandableFieldDeserializer())
.registerTypeAdapter(ExpandableField.class, new ExpandableFieldSerializer())
.registerTypeAdapter(Instant.class, new InstantDeserializer())
.registerTypeAdapterFactory(new EventTypeAdapterFactory())
.registerTypeAdapter(StripeRawJsonObject.class, new StripeRawJsonObjectDeserializer())
.registerTypeAdapter(StripeRawJsonObject.class, new StripeRawJsonObjectSerializer())
.registerTypeAdapterFactory(new StripeCollectionItemTypeSettingFactory())
.addReflectionAccessFilter(
new ReflectionAccessFilter() {
Expand Down
144 changes: 144 additions & 0 deletions src/test/java/com/stripe/model/GsonRoundTripTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
package com.stripe.model;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.stripe.BaseStripeTest;
import com.stripe.net.ApiResource;
import org.junit.jupiter.api.Test;

public class GsonRoundTripTest extends BaseStripeTest {

@Test
public void testUnexpandedExpandableField() {
String json = "{\"id\":\"in_123\",\"object\":\"invoice\",\"customer\":\"cus_456\"}";
Invoice invoice = ApiResource.GSON.fromJson(json, Invoice.class);

assertEquals("cus_456", invoice.getCustomer());
assertNull(invoice.getCustomerObject());

String serialized = ApiResource.GSON.toJson(invoice);
Invoice roundTripped = ApiResource.GSON.fromJson(serialized, Invoice.class);

assertEquals("cus_456", roundTripped.getCustomer());
assertNull(roundTripped.getCustomerObject());
}

@Test
public void testExpandedExpandableField() {
String json =
"{\"id\":\"in_123\",\"object\":\"invoice\","
+ "\"customer\":{\"id\":\"cus_456\",\"object\":\"customer\","
+ "\"name\":\"John Doe\",\"metadata\":{\"key\":\"value\"}}}";
Invoice invoice = ApiResource.GSON.fromJson(json, Invoice.class);

assertEquals("cus_456", invoice.getCustomer());
Customer customer = invoice.getCustomerObject();
assertNotNull(customer);
assertEquals("cus_456", customer.getId());
assertEquals("John Doe", customer.getName());

String serialized = ApiResource.GSON.toJson(invoice);
Invoice roundTripped = ApiResource.GSON.fromJson(serialized, Invoice.class);

assertEquals("cus_456", roundTripped.getCustomer());
Customer rtCustomer = roundTripped.getCustomerObject();
assertNotNull(rtCustomer);
assertEquals("cus_456", rtCustomer.getId());
assertEquals("John Doe", rtCustomer.getName());
assertEquals("value", rtCustomer.getMetadata().get("key"));
}

@Test
public void testNullExpandableField() {
String json = "{\"id\":\"in_123\",\"object\":\"invoice\",\"customer\":null}";
Invoice invoice = ApiResource.GSON.fromJson(json, Invoice.class);

assertNull(invoice.getCustomer());
assertNull(invoice.getCustomerObject());

String serialized = ApiResource.GSON.toJson(invoice);
Invoice roundTripped = ApiResource.GSON.fromJson(serialized, Invoice.class);

assertNull(roundTripped.getCustomer());
assertNull(roundTripped.getCustomerObject());
}

@Test
public void testPaymentSourceDirectField() {
// Charge.source is a direct PaymentSource field (not ExpandableField)
String json =
"{\"id\":\"ch_123\",\"object\":\"charge\","
+ "\"source\":{\"id\":\"card_789\",\"object\":\"card\","
+ "\"brand\":\"Visa\",\"last4\":\"4242\"}}";
Charge charge = ApiResource.GSON.fromJson(json, Charge.class);

assertNotNull(charge.getSource());
assertTrue(charge.getSource() instanceof Card);
assertEquals("card_789", charge.getSource().getId());

String serialized = ApiResource.GSON.toJson(charge);
Charge roundTripped = ApiResource.GSON.fromJson(serialized, Charge.class);

assertNotNull(roundTripped.getSource());
assertTrue(roundTripped.getSource() instanceof Card);
assertEquals("card_789", roundTripped.getSource().getId());
assertEquals("Visa", ((Card) roundTripped.getSource()).getBrand());
assertEquals("4242", ((Card) roundTripped.getSource()).getLast4());
}

@Test
public void testStripeRawJsonObjectRoundTrip() {
String innerJson = "{\"id\":\"unknown_123\",\"object\":\"unknown_type\",\"foo\":\"bar\"}";
StripeRawJsonObject raw = new StripeRawJsonObject();
raw.json = JsonParser.parseString(innerJson).getAsJsonObject();

String serialized = ApiResource.GSON.toJson(raw);
// Should serialize as the raw JSON, not wrapped in {"json":{...}}
JsonObject parsed = JsonParser.parseString(serialized).getAsJsonObject();
assertEquals("unknown_123", parsed.get("id").getAsString());
assertEquals("bar", parsed.get("foo").getAsString());

StripeRawJsonObject roundTripped =
ApiResource.GSON.fromJson(serialized, StripeRawJsonObject.class);
assertNotNull(roundTripped.json);
assertEquals("unknown_123", roundTripped.json.get("id").getAsString());
assertEquals("bar", roundTripped.json.get("foo").getAsString());
}

@Test
public void testInvoiceWithExpandedCustomerRoundTrip() throws Exception {
// Realistic scenario from RUN_DEVSDK-2253
final String[] expansions = {"customer"};
final String data = getFixture("/v1/invoices/in_123", expansions);
final Invoice original = ApiResource.GSON.fromJson(data, Invoice.class);

assertNotNull(original.getCustomerObject());
assertEquals(original.getCustomer(), original.getCustomerObject().getId());

String serialized = ApiResource.GSON.toJson(original);
Invoice roundTripped = ApiResource.GSON.fromJson(serialized, Invoice.class);

assertEquals(original.getId(), roundTripped.getId());
assertEquals(original.getCustomer(), roundTripped.getCustomer());
assertNotNull(roundTripped.getCustomerObject());
assertEquals(original.getCustomerObject().getId(), roundTripped.getCustomerObject().getId());
}

@Test
public void testSubscriptionWithDefaultSourceRoundTrip() throws Exception {
// Realistic scenario from DEVSDK-2319
final String[] expansions = {"default_source"};
final String data = getFixture("/v1/subscriptions/sub_123", expansions);
final Subscription original = ApiResource.GSON.fromJson(data, Subscription.class);

String serialized = ApiResource.GSON.toJson(original);
Subscription roundTripped = ApiResource.GSON.fromJson(serialized, Subscription.class);

assertEquals(original.getId(), roundTripped.getId());
}
}
17 changes: 17 additions & 0 deletions src/test/java/com/stripe/model/InvoiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,23 @@ public void testDeserializeWithUnexpandedArrayExpansions() throws Exception {
assertEquals(2, invoice.getDiscountObjects().size());
}

@Test
public void testRoundTripWithExpandedCustomer() throws Exception {
final String[] expansions = {"charge", "customer"};
final String data = getFixture("/v1/invoices/in_123", expansions);
final Invoice original = ApiResource.GSON.fromJson(data, Invoice.class);

assertNotNull(original.getCustomerObject());

String serialized = ApiResource.GSON.toJson(original);
Invoice roundTripped = ApiResource.GSON.fromJson(serialized, Invoice.class);

assertEquals(original.getId(), roundTripped.getId());
assertEquals(original.getCustomer(), roundTripped.getCustomer());
assertNotNull(roundTripped.getCustomerObject());
assertEquals(original.getCustomerObject().getId(), roundTripped.getCustomerObject().getId());
}

@Test
public void testDeserializeWithArrayExpansions() throws Exception {
final Invoice invoice =
Expand Down