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

[fix] Address verification details lost during deserialization #289

Merged
merged 2 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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;
}
nwithan8 marked this conversation as resolved.
Show resolved Hide resolved
}
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;
}
nwithan8 marked this conversation as resolved.
Show resolved Hide resolved
}
6 changes: 5 additions & 1 deletion src/test/java/com/easypost/AddressTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ 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());
assertNotNull(address.getVerifications().getZip4().getErrors()); // Should have a error due to second line
nwithan8 marked this conversation as resolved.
Show resolved Hide resolved
nwithan8 marked this conversation as resolved.
Show resolved Hide resolved
assertNotNull(address.getVerifications().getDelivery().getErrors());
}

/**
Expand Down Expand Up @@ -222,7 +226,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