Skip to content

Commit

Permalink
[fix] Address verification details lost during deserialization (#289)
Browse files Browse the repository at this point in the history
 - Add custom deserializer for AddressVerification object
- Add unit test to verify AddressVerification deserialization works properly
  • Loading branch information
nwithan8 authored Nov 16, 2023
1 parent f43f879 commit 710381d
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/main/java/com/easypost/Constants.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.easypost;

import com.easypost.http.HashMapSerializer;
import com.easypost.model.AddressVerification;
import com.easypost.model.AddressVerificationDeserializer;
import com.easypost.model.Error;
import com.easypost.model.ErrorDeserializer;
import com.easypost.model.SmartrateCollection;
Expand Down Expand Up @@ -72,6 +74,7 @@ public abstract static class Http {
.registerTypeAdapter(HashMap.class, new HashMapSerializer())
.registerTypeAdapter(SmartrateCollection.class, new SmartrateCollectionDeserializer())
.registerTypeAdapter(Error.class, new ErrorDeserializer())
.registerTypeAdapter(AddressVerification.class, new AddressVerificationDeserializer())
.registerTypeAdapter(StatelessRate[].class, new StatelessRateDeserializer()).create();
public static final Gson PRETTY_PRINT_GSON = new GsonBuilder().setPrettyPrinting().serializeNulls()
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create();
Expand Down
27 changes: 27 additions & 0 deletions src/main/java/com/easypost/model/AddressVerification.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,31 @@ public final class AddressVerification {
private Boolean success;
private List<Error> errors;
private AddressDetail details;

/**
* Set the success of this address verification object.
*
* @param success The success message.
*/
void setSuccess(final boolean success) {
this.success = success;
}

/**
* Set the errors of this address verification object.
*
* @param errors The errors.
*/
void setErrors(final List<Error> errors) {
this.errors = errors;
}

/**
* Set the details of this address verification object.
*
* @param details The details.
*/
void setDetails(final AddressDetail details) {
this.details = details;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.easypost.model;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;

import java.lang.reflect.Type;
import java.util.ArrayList;

public final class AddressVerificationDeserializer implements JsonDeserializer<AddressVerification> {
/**
* Deserialize an AddressVerification from a JSON object.
*
* @param json JSON object to deserialize.
* @param typeOfT Type of the object to deserialize.
* @param context Deserialization context.
* @return Deserialized AddressVerification object.
* @throws JsonParseException if the JSON object is not a valid SmartrateCollection.
*/
@Override
public AddressVerification deserialize(final JsonElement json, final Type typeOfT,
final JsonDeserializationContext context) throws JsonParseException {
JsonObject jo = json.getAsJsonObject();

AddressVerification addressVerification = new AddressVerification();

boolean success = jo.get("success").getAsBoolean();
addressVerification.setSuccess(success);

AddressDetail details = context.deserialize(jo.get("details"), AddressDetail.class);
addressVerification.setDetails(details);

JsonElement errorsAsJson = jo.get("errors");
Gson gson = new Gson();

if (errorsAsJson != null) {
JsonArray errorsAsArray = errorsAsJson.getAsJsonArray();
ArrayList<Error> errors = new ArrayList<>();
for (JsonElement errorAsJson : errorsAsArray) {
JsonObject errorAsJsonObject = errorAsJson.getAsJsonObject();

Error error = new Error();

JsonElement code = errorAsJsonObject.get("code");
if (code != null) {
error.setCode(code.getAsString());
}

JsonElement message = errorAsJsonObject.get("message");
if (message != null) {
error.setMessage(message.getAsString());
}

JsonElement field = errorAsJsonObject.get("field");
if (field != null) {
error.setField(field.getAsString());
}

JsonElement suggestion = errorAsJsonObject.get("suggestion");
if (suggestion != null && !suggestion.isJsonNull()) {
error.setSuggestion(suggestion.getAsString());
}

errors.add(error);
}
addressVerification.setErrors(errors);
}

return addressVerification;
}
}
27 changes: 27 additions & 0 deletions src/main/java/com/easypost/model/Error.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,31 @@ void setMessage(final String message) {
void setCode(final String code) {
this.code = code;
}

/**
* Set the errors of this error object.
*
* @param errors The errors.
*/
void setErrors(final List<Error> errors) {
this.errors = errors;
}

/**
* Set the suggestion of this error object.
*
* @param suggestion The suggestion.
*/
void setSuggestion(final String suggestion) {
this.suggestion = suggestion;
}

/**
* Set the field of this error object.
*
* @param field The field.
*/
void setField(final String field) {
this.field = field;
}
}
14 changes: 13 additions & 1 deletion src/test/java/com/easypost/AddressTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.easypost.exception.General.EndOfPaginationError;
import com.easypost.model.Address;
import com.easypost.model.AddressCollection;
import com.easypost.model.Error;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

Expand All @@ -14,6 +15,7 @@
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
Expand Down Expand Up @@ -79,6 +81,16 @@ public void testCreateVerify() throws EasyPostException {
assertInstanceOf(Address.class, address);
assertTrue(address.getId().startsWith("adr_"));
assertEquals("417 MONTGOMERY ST FL 5", address.getStreet1());

assertNotNull(address.getVerifications());

assertFalse(address.getVerifications().getDelivery().getErrors().isEmpty()); // should have at least one error
Error error = address.getVerifications().getDelivery().getErrors().get(0);
assertEquals("E.SECONDARY_INFORMATION.INVALID", error.getCode());

assertFalse(address.getVerifications().getZip4().getErrors().isEmpty()); // should have at least one error
error = address.getVerifications().getZip4().getErrors().get(0);
assertEquals("E.SECONDARY_INFORMATION.INVALID", error.getCode());
}

/**
Expand Down Expand Up @@ -222,7 +234,7 @@ public void testVerify() throws EasyPostException {

Address verifiedAddress = vcr.client.address.verify(address.getId());

assertInstanceOf(Address.class, address);
assertInstanceOf(Address.class, verifiedAddress);
assertTrue(verifiedAddress.getId().startsWith("adr_"));
assertEquals("388 TOWNSEND ST APT 20", verifiedAddress.getStreet1());
}
Expand Down

0 comments on commit 710381d

Please sign in to comment.