Skip to content
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

Fixed raw request deserialization for v1 and v2 Api endpoints #1883

Merged
Merged
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
4 changes: 2 additions & 2 deletions src/main/java/com/stripe/StripeClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -731,7 +731,7 @@ public StripeResponse rawRequest(
}

/** Deserializes StripeResponse returned by rawRequest into a similar class. */
public StripeObject deserialize(String rawJson) throws StripeException {
return StripeObject.deserializeStripeObject(rawJson, this.getResponseGetter());
public StripeObject deserialize(String rawJson, ApiMode apiMode) throws StripeException {
return StripeObject.deserializeStripeObject(rawJson, this.getResponseGetter(), apiMode);
}
}
3 changes: 2 additions & 1 deletion src/main/java/com/stripe/model/Event.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.google.gson.JsonParseException;
import com.google.gson.annotations.SerializedName;
import com.stripe.exception.StripeException;
import com.stripe.net.ApiMode;
import com.stripe.net.ApiRequest;
import com.stripe.net.ApiRequestParams;
import com.stripe.net.ApiResource;
Expand Down Expand Up @@ -363,7 +364,7 @@ public static class Data extends StripeObject implements StripeActiveObject {
*/
@Deprecated
public StripeObject getObject() {
return StripeObject.deserializeStripeObject(object, this.responseGetter);
return StripeObject.deserializeStripeObject(object, this.responseGetter, ApiMode.V1);
prathmesh-stripe marked this conversation as resolved.
Show resolved Hide resolved
}

@Override
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/com/stripe/model/EventDataClassLookup.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
/**
* Event data class look up used in event deserialization. The key to look up is `object` string of
* the model.
*
* <p>For internal use by Stripe SDK only.
*/
final class EventDataClassLookup {
public final class EventDataClassLookup {
prathmesh-stripe marked this conversation as resolved.
Show resolved Hide resolved
public static final Map<String, Class<? extends StripeObject>> classLookup = new HashMap<>();

static {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.google.gson.JsonParseException;
import com.stripe.Stripe;
import com.stripe.exception.EventDataObjectDeserializationException;
import com.stripe.net.ApiMode;
import com.stripe.net.StripeResponseGetter;
import java.util.Map;
import java.util.Optional;
Expand Down Expand Up @@ -121,7 +122,8 @@ private boolean deserialize() {
return true;
} else {
try {
object = StripeObject.deserializeStripeObject(rawJsonObject, this.responseGetter);
object =
StripeObject.deserializeStripeObject(rawJsonObject, this.responseGetter, ApiMode.V1);
return true;
} catch (JsonParseException e) {
// intentionally ignore exception to fulfill simply whether deserialization succeeds
Expand All @@ -146,7 +148,7 @@ private boolean deserialize() {
*/
public StripeObject deserializeUnsafe() throws EventDataObjectDeserializationException {
try {
return StripeObject.deserializeStripeObject(rawJsonObject, this.responseGetter);
return StripeObject.deserializeStripeObject(rawJsonObject, this.responseGetter, ApiMode.V1);
} catch (JsonParseException e) {
String errorMessage;
if (!apiVersionMatch()) {
Expand Down Expand Up @@ -190,7 +192,8 @@ public StripeObject deserializeUnsafe() throws EventDataObjectDeserializationExc
public StripeObject deserializeUnsafeWith(CompatibilityTransformer transformer) {
return StripeObject.deserializeStripeObject(
transformer.transform(rawJsonObject.deepCopy(), apiVersion, eventType),
this.responseGetter);
this.responseGetter,
ApiMode.V1);
prathmesh-stripe marked this conversation as resolved.
Show resolved Hide resolved
}

private boolean apiVersionMatch() {
Expand Down
18 changes: 10 additions & 8 deletions src/main/java/com/stripe/model/StripeObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import com.stripe.net.ApiMode;
import com.stripe.net.ApiResource;
import com.stripe.net.StripeResponse;
import com.stripe.net.StripeResponseGetter;
Expand Down Expand Up @@ -99,19 +100,20 @@ protected static boolean equals(Object a, Object b) {
* @return JSON data to be deserialized to super class {@code StripeObject}
*/
static StripeObject deserializeStripeObject(
JsonObject eventDataObjectJson, StripeResponseGetter responseGetter) {
JsonObject eventDataObjectJson, StripeResponseGetter responseGetter, ApiMode apiMode) {
String type = eventDataObjectJson.getAsJsonObject().get("object").getAsString();
Class<? extends StripeObject> cl = EventDataClassLookup.classLookup.get(type);
StripeObject object =
StripeObject.deserializeStripeObject(
eventDataObjectJson, cl != null ? cl : StripeRawJsonObject.class, responseGetter);
return object;
Class<? extends StripeObject> cl =
(apiMode == ApiMode.V1)
? com.stripe.model.EventDataClassLookup.classLookup.get(type)
: com.stripe.model.v2.EventDataClassLookup.classLookup.get(type);
return StripeObject.deserializeStripeObject(
eventDataObjectJson, cl != null ? cl : StripeRawJsonObject.class, responseGetter);
}

public static StripeObject deserializeStripeObject(
String payload, StripeResponseGetter responseGetter) {
String payload, StripeResponseGetter responseGetter, ApiMode apiMode) {
JsonObject jsonObject = ApiResource.GSON.fromJson(payload, JsonObject.class).getAsJsonObject();
return deserializeStripeObject(jsonObject, responseGetter);
return deserializeStripeObject(jsonObject, responseGetter, apiMode);
}

public static StripeObject deserializeStripeObject(
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/com/stripe/model/v2/EventDataClassLookup.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
/**
* Event data class look up used in event deserialization. The key to look up is `object` string of
* the model.
*
* <p>For internal use by Stripe SDK only.
*/
final class EventDataClassLookup {
public final class EventDataClassLookup {
public static final Map<String, Class<? extends StripeObject>> classLookup = new HashMap<>();
public static final Map<String, Class<? extends Event>> eventClassLookup = new HashMap<>();

Expand Down
37 changes: 29 additions & 8 deletions src/test/java/com/stripe/RawRequestTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,9 @@

import com.stripe.exception.StripeException;
import com.stripe.model.Customer;
import com.stripe.model.v2.billing.MeterEvent;
import com.stripe.net.*;
import com.stripe.net.ApiResource.RequestMethod;
import com.stripe.net.HttpURLConnectionClient;
import com.stripe.net.LiveStripeResponseGetter;
import com.stripe.net.RawRequestOptions;
import com.stripe.net.StripeResponse;
import com.stripe.net.StripeResponseGetter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -166,7 +163,7 @@ public void testDeserializeGlobal() throws StripeException, InterruptedException
assertEquals(200, response.code());
assertTrue(response.body().length() > 0);

Customer customer = (Customer) client.deserialize(response.body());
Customer customer = (Customer) client.deserialize(response.body(), ApiMode.V1);
assertTrue(customer.getId().startsWith("cus_"));
assertEquals("test customer", customer.getDescription());
}
Expand Down Expand Up @@ -281,7 +278,7 @@ public void testAdditionalHeadersClient() throws StripeException, InterruptedExc
}

@Test
public void testDeserializeClient() throws StripeException, InterruptedException {
public void testDeserializeClientV1Api() throws StripeException, InterruptedException {
server.enqueue(
new MockResponse()
.setBody(
Expand All @@ -297,12 +294,36 @@ public void testDeserializeClient() throws StripeException, InterruptedException
assertEquals(200, response.code());
assertTrue(response.body().length() > 0);

Customer customer = (Customer) client.deserialize(response.body());
Customer customer = (Customer) client.deserialize(response.body(), ApiMode.V1);
assertTrue(customer.getId().startsWith("cus_"));
assertEquals("test customer", customer.getDescription());
assertTrue(Mockito.mockingDetails(responseGetter).getInvocations().stream().count() > 0);
}

@Test
public void testDeserializeClientV2Api() throws StripeException, InterruptedException {
server.enqueue(
new MockResponse()
.setBody(
"{\"object\":\"billing.meter_event\",\"created\":\"2024-10-01T04:46:22.861Z\",\"event_name\":\"new_meter\",\"identifier\":\"d8a5ab2e-81ec-4bdf-acbf-48bf346\",\"livemode\":false,\"payload\":{\"stripe_customer_id\":\"cus_QvF3b2W6\",\"value\":\"25\"},\"timestamp\":\"2024-10-01T04:46:22.836Z\"}"));

final RawRequestOptions options = RawRequestOptions.builder().setApiKey("sk_123").build();
String param =
"{\"event_name\":\"new_meter\",\"payload\":{\"stripe_customer_id\":\"cus_QvF3b2W6\",\"value\":\"25\"}}";

final StripeResponse response =
client.rawRequest(RequestMethod.POST, "/v2/billing/meter_event", param, options);

assertNotNull(response);
assertEquals(200, response.code());
assertTrue(response.body().length() > 0);

MeterEvent meterEvent = (MeterEvent) client.deserialize(response.body(), ApiMode.V2);
assertEquals("new_meter", meterEvent.getEventName());
assertEquals("d8a5ab2e-81ec-4bdf-acbf-48bf346", meterEvent.getIdentifier());
assertTrue(Mockito.mockingDetails(responseGetter).getInvocations().stream().count() > 0);
}

@Test
public void testRaisesErrorWhenGetRequestAndContentIsNonNullClient() throws StripeException {
try {
Expand Down
Loading