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

Serializing a Mail object to/from JSON does not result in equivalent objects. #189

Closed
efenderbosch opened this issue Mar 24, 2017 · 3 comments
Labels
difficulty: hard fix is hard in difficulty status: help wanted requesting help from the community type: community enhancement feature request not on Twilio's roadmap

Comments

@efenderbosch
Copy link

Issue Summary

Serializing a Mail object to/from JSON does not result in equivalent objects.

Steps to Reproduce

Email to = new Email("foo@bar.com");
Content content = new Content(MIMETYPE_HTML, "test");
Email from = new Email("no-reply@bar.com");
Mail mail = new Mail(from, "subject", to, content);

ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(mail);
Mail deserialized = mapper.readValue(json, Mail.class);

System.out.println(ReflectionToStringBuilder.toString(deserialized));
assertThat(deserialized, sameBeanAs(mail));

sameBeanAs is in the shazamcrest library and performs a recursive deep equality check.

The first issue is that the from attribute is not deserialized correctly:
com.sendgrid.Mail@401e7803[from=<null>,subject=subject,personalization=[com.sendgrid.Personalization@43a0cee9],content=[com.sendgrid.Content@eb21112],attachments=<null>,templateId=<null>,sections=<null>,headers=<null>,categories=<null>,customArgs=<null>,sendAt=0,batchId=<null>,asm=<null>,ipPoolId=<null>,mailSettings=<null>,trackingSettings=<null>,replyTo=<null>]

This seems to be because the Mail object has public attributes and non-standard getters:

public class Mail {
    @JsonProperty("from")
    public Email from;

    //...

    @JsonProperty("from")
    public Email getFrom(Email from) {
        return from;
    }

    public void setFrom(Email from) {
        this.from = from;
    }

    // ...
}

The goal is to write a Mail object to a queue so that we can have a process that manages a circuit breaker, rate limiting, retries and DLQ behavior. Right now, I can write the JSON to the queue, but it doesn't deserialize properly when being read.

Technical details:

  • sendgrid-java Version: 3.2.0 from Maven Central
  • Java Version: 1.8
  • Jackson Version: 2.8.7
@thinkingserious thinkingserious added status: help wanted requesting help from the community type: community enhancement feature request not on Twilio's roadmap labels Mar 27, 2017
@thinkingserious
Copy link
Contributor

Thanks for alerting us to this issue @efenderbosch!

I have added this to our backlog for implementation.

This issue can rise in our queue via additional +1's or comments on this issue or it can move up our queue the fastest with a PR.

Thanks!

Elmer

@sean-shou
Copy link

3.2.1 still have this issue. My template solution is create my own 'mail' object.

public class MyMail extends Mail{
public MyDesktopMail() {
}

public MyMail(Email from, String subject, Email to, Content content) {
    super(from, subject, to, content);
}

@Override
@JsonProperty("from")
public Email getFrom(Email from) {
    super.from = from;
    return from;
}

@thinkingserious
Copy link
Contributor

Thats awesome @sean-shou :)

Would you be open to making a PR to add this to our library as a helper?

@thinkingserious thinkingserious added difficulty: hard fix is hard in difficulty hacktoberfest labels Oct 1, 2017
@mbernier mbernier removed difficulty: very hard fix is very hard in difficulty difficulty: medium fix is medium in difficulty labels Oct 27, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
difficulty: hard fix is hard in difficulty status: help wanted requesting help from the community type: community enhancement feature request not on Twilio's roadmap
Projects
None yet
Development

No branches or pull requests

4 participants